Skip to content

Commit

Permalink
Twitter posting fucntion for Microchain (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Dec 6, 2024
1 parent b74de5d commit 6f65c01
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from prediction_market_agent.utils import APIKeys


class MicrochainAgentKeys(APIKeys):
# Double check to make sure you want to actually post on public social media.
ENABLE_SOCIAL_MEDIA: bool = False
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
from microchain import Function
from prediction_market_agent_tooling.loggers import logger

from prediction_market_agent.agents.microchain_agent.microchain_agent_keys import (
MicrochainAgentKeys,
)
from prediction_market_agent.agents.social_media_agent.social_media.twitter_handler import (
POST_MAX_LENGTH,
TwitterHandler,
)


class SendTweet(Function):
@property
def description(self) -> str:
return "Use this function to post a tweet on Twitter."
return f"Use this function to post a tweet on Twitter. Maximum length of the tweet is {POST_MAX_LENGTH} characters."

@property
def example_args(self) -> list[str]:
return ["This is my message."]
return ["This is my tweet."]

def __call__(
self,
message: str,
) -> str:
# TODO: Complete the logic.
return "Message sent."
def __call__(self, tweet: str) -> str:
if TwitterHandler.does_post_length_exceed_max_length(tweet):
return f"Tweet length exceeds the maximum allowed length of {POST_MAX_LENGTH} characters, because it is {len(tweet)} characters long. Please shorten the tweet."
if MicrochainAgentKeys().ENABLE_SOCIAL_MEDIA:
# Use the raw `post_tweet`, instead of `post`, let the general agent shorten the tweet on its own if it's required.
TwitterHandler().post_tweet(tweet)
else:
# Log as error, so we are notified about it, if we forget to turn it on in production.
logger.error(f"Social media is disabled. Tweeting skipped: {tweet}")
return "Tweet sent."


TWITTER_FUNCTIONS: list[type[Function]] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from prediction_market_agent.agents.social_media_agent.social_media.abstract_handler import (
AbstractSocialMediaHandler,
)
from prediction_market_agent.utils import APIKeys, SocialMediaAPIKeys
from prediction_market_agent.utils import SocialMediaAPIKeys


class TwitterHandler(AbstractSocialMediaHandler):
Expand All @@ -36,7 +36,7 @@ def __init__(
self.llm = ChatOpenAI(
temperature=0,
model=model,
api_key=APIKeys().openai_api_key_secretstr_v1,
api_key=keys.openai_api_key_secretstr_v1,
)

@observe()
Expand All @@ -57,11 +57,12 @@ def make_tweet_more_concise(self, tweet: str) -> str:
def does_post_length_exceed_max_length(tweet: str) -> bool:
return len(tweet) > POST_MAX_LENGTH

def post(self, text: str, reasoning_reply_tweet: str) -> None:
def post(self, text: str, reasoning_reply_tweet: str | None = None) -> None:
quote_tweet_id = self.post_else_retry_with_summarization(text)
self.post_else_retry_with_summarization(
reasoning_reply_tweet, quote_tweet_id=quote_tweet_id
)
if reasoning_reply_tweet is not None:
self.post_else_retry_with_summarization(
reasoning_reply_tweet, quote_tweet_id=quote_tweet_id
)

def post_else_retry_with_summarization(
self, text: str, quote_tweet_id: str | None = None
Expand All @@ -79,6 +80,12 @@ def post_else_retry_with_summarization(
f"Tweet too long. Length: {len(text)}, max length: {POST_MAX_LENGTH}"
)
return None
return self.post_tweet(text, quote_tweet_id)

def post_tweet(self, text: str, quote_tweet_id: str | None = None) -> str | None:
"""
Posts the provided text on Twitter.
"""
posted_tweet = self.client.create_tweet(
text=text, quote_tweet_id=quote_tweet_id
)
Expand Down

0 comments on commit 6f65c01

Please sign in to comment.