From 8d74a21a39b56b3c08be40c03d7a603244d6cf99 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Thu, 14 Nov 2024 12:01:38 -0800 Subject: [PATCH 01/23] first pass implementing twitter account mentions action --- .../actions/social/twitter/__init__.py | 2 + .../social/twitter/account_mentions.py | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py index c8735ce9a..2d688a066 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py @@ -1,4 +1,5 @@ from cdp_agentkit_core.actions.social.twitter.account_details import AccountDetailsAction +from cdp_agentkit_core.actions.social.twitter.account_mentions import AccountMentionsAction from cdp_agentkit_core.actions.social.twitter.action import TwitterAction from cdp_agentkit_core.actions.social.twitter.post_tweet import PostTweetAction @@ -16,6 +17,7 @@ def get_all_twitter_actions() -> list[type[TwitterAction]]: __all__ = [ "TwitterAction", "AccountDetailsAction", + "AccountMentionsAction", "PostTweetAction", "TWITTER_ACTIONS", ] diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py new file mode 100644 index 000000000..e26d62216 --- /dev/null +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py @@ -0,0 +1,52 @@ +from collections.abc import Callable +from json import dumps + +import tweepy +from pydantic import BaseModel, Field + +from cdp_agentkit_core.actions.social.twitter.action import TwitterAction + +ACCOUNT_MENTIONS_PROMPT = """ +This tool will return account mentions for the currently authenticated Twitter (X) user context.""" + + +class AccountMentionsInput(BaseModel): + """Input argument schema for Twitter account mentions action.""" + + account_id: str = Field( + ..., + description="The account id for the Twitter (X) user to get mentions for", + ) + + +def account_mentions(client: tweepy.Client, account_id: str) -> str: + """Get the authenticated Twitter (X) user account mentions. + + Args: + client (tweepy.Client): The Twitter (X) client used to authenticate with. + account_id (str): The Twitter (X) account id to get mentions for. + + Returns: + str: A message containing account mentions for the authenticated user context. + + """ + message = "" + + try: + response = client.get_users_mentions(account_id) + mentions = response.data + + message = f"Successfully retrieved authenticated user account mentions:{dumps(mentions)}" + except tweepy.errors.TweepyException as e: + message = f"Error retrieving authenticated user account mentions: {e}" + + return message + + +class AccountMentionsAction(TwitterAction): + """Twitter (X) account mentions action.""" + + name: str = "account_mentions" + description: str = ACCOUNT_MENTIONS_PROMPT + args_schema: type[BaseModel] | None = AccountMentionsInput + func: Callable[..., str] = account_mentions From 40ceed133da4f4bbcdec0b9f717d42b410124530 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 09:56:40 -0800 Subject: [PATCH 02/23] first pass implementing post reply to twitter --- .../social/twitter/post_tweet_reply.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py new file mode 100644 index 000000000..f7b6d2052 --- /dev/null +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py @@ -0,0 +1,57 @@ +from collections.abc import Callable +from json import dumps + +import tweepy +from pydantic import BaseModel, Field + +from cdp_agentkit_core.actions.social.twitter import TwitterAction + +POST_TWEET_REPLY_REPLY_PROMPT = """ +This tool will post a reply to a tweet on Twitter. The tool takes the text of the tweet as input and the tweet id to reply to. Tweets can be maximum 280 characters.""" + + +class PostTweetReplyInput(BaseModel): + """Input argument schema for twitter post tweet reply action.""" + + tweet_id: str = Field( + ..., + description="The tweet id to post a reply to twitter", + ) + + tweet_reply: str = Field( + ..., + description="The text of the tweet to post in reply to another tweet on twitter. Tweets can be maximum 280 characters.", + ) + + +def post_tweet_reply(client: tweepy.Client, tweet_id: str, tweet_reply: str) -> str: + """post tweet reply to Twitter. + + Args: + client (tweepy.Client): The tweepy client to use. + tweet (str): The text of the tweet to post to twitter. Tweets can be maximum 280 characters. + + Returns: + str: A message containing the result of the post action and the tweet. + + """ + message = "" + + try: + response = client.create_tweet(text=tweet_reply, in_reply_to_tweet_id=tweet_id) + data = response.data + + message = f"Successfully posted reply to Twitter:\n{dumps(data)}" + except tweepy.errors.TweepyException as e: + message = f"Error posting reply to Twitter: {e}" + + return message + + +class PostTweetReplyAction(TwitterAction): + """Twitter (X) post tweet reply action.""" + + name: str = "post_tweet_reply" + description: str = POST_TWEET_REPLY_REPLY_PROMPT + args_schema: type[BaseModel] | None = PostTweetReplyInput + func: Callable[..., str] = post_tweet_reply From 30000b8bb1a2c99055a6fa8a70bca12ba61633f6 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 10:04:16 -0800 Subject: [PATCH 03/23] adjusting tool prompt --- .../actions/social/twitter/post_tweet_reply.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py index f7b6d2052..499469dec 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py @@ -6,8 +6,8 @@ from cdp_agentkit_core.actions.social.twitter import TwitterAction -POST_TWEET_REPLY_REPLY_PROMPT = """ -This tool will post a reply to a tweet on Twitter. The tool takes the text of the tweet as input and the tweet id to reply to. Tweets can be maximum 280 characters.""" +POST_TWEET_REPLY_PROMPT = """ +This tool will post a reply to a tweet on Twitter. The tool takes the text of the reply and the tweet id to reply to as input. Tweets can be maximum 280 characters.""" class PostTweetReplyInput(BaseModel): @@ -52,6 +52,6 @@ class PostTweetReplyAction(TwitterAction): """Twitter (X) post tweet reply action.""" name: str = "post_tweet_reply" - description: str = POST_TWEET_REPLY_REPLY_PROMPT + description: str = POST_TWEET_REPLY_PROMPT args_schema: type[BaseModel] | None = PostTweetReplyInput func: Callable[..., str] = post_tweet_reply From 09994f53ca894b3fbcb45176fc3f97014ad34e1f Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 10:06:28 -0800 Subject: [PATCH 04/23] formatting --- .../cdp_agentkit_core/actions/social/twitter/__init__.py | 2 ++ .../actions/social/twitter/account_mentions.py | 2 ++ .../actions/social/twitter/post_tweet_reply.py | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py index 2d688a066..410a3d7e2 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py @@ -2,6 +2,7 @@ from cdp_agentkit_core.actions.social.twitter.account_mentions import AccountMentionsAction from cdp_agentkit_core.actions.social.twitter.action import TwitterAction from cdp_agentkit_core.actions.social.twitter.post_tweet import PostTweetAction +from cdp_agentkit_core.actions.social.twitter.post_tweet_reply import PostTweetReplyAction def get_all_twitter_actions() -> list[type[TwitterAction]]: @@ -19,5 +20,6 @@ def get_all_twitter_actions() -> list[type[TwitterAction]]: "AccountDetailsAction", "AccountMentionsAction", "PostTweetAction", + "PostTweetReplyAction", "TWITTER_ACTIONS", ] diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py index e26d62216..16f652d83 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py @@ -32,6 +32,8 @@ def account_mentions(client: tweepy.Client, account_id: str) -> str: """ message = "" + print(f"attempting to get mentions for account_id: {account_id}") + try: response = client.get_users_mentions(account_id) mentions = response.data diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py index 499469dec..f5ca91e75 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py @@ -25,7 +25,7 @@ class PostTweetReplyInput(BaseModel): def post_tweet_reply(client: tweepy.Client, tweet_id: str, tweet_reply: str) -> str: - """post tweet reply to Twitter. + """Post tweet reply to Twitter. Args: client (tweepy.Client): The tweepy client to use. From e8a9ee182b873ba4506e8f8a1fa41e91ca2077fd Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 10:20:38 -0800 Subject: [PATCH 05/23] adding bearer token in for twitter client --- .../twitter_langchain/twitter_api_wrapper.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/twitter-langchain/twitter_langchain/twitter_api_wrapper.py b/twitter-langchain/twitter_langchain/twitter_api_wrapper.py index 7e57a6ac3..8f84dc4f0 100644 --- a/twitter-langchain/twitter_langchain/twitter_api_wrapper.py +++ b/twitter-langchain/twitter_langchain/twitter_api_wrapper.py @@ -21,9 +21,8 @@ def validate_environment(cls, values: dict) -> Any: api_key = get_from_dict_or_env(values, "twitter_api_key", "TWITTER_API_KEY") api_secret = get_from_dict_or_env(values, "twitter_api_secret", "TWITTER_API_SECRET") access_token = get_from_dict_or_env(values, "twitter_access_token", "TWITTER_ACCESS_TOKEN") - access_token_secret = get_from_dict_or_env( - values, "twitter_access_token_secret", "TWITTER_ACCESS_TOKEN_SECRET" - ) + access_token_secret = get_from_dict_or_env(values, "twitter_access_token_secret", "TWITTER_ACCESS_TOKEN_SECRET") + bearer_token = get_from_dict_or_env(values, "twitter_bearer_token", "TWITTER_BEARER_TOKEN") try: import tweepy @@ -31,7 +30,7 @@ def validate_environment(cls, values: dict) -> Any: raise ImportError( "Tweepy Twitter SDK is not installed. " -"Please install it with `pip install tweepy`" + "Please install it with `pip install tweepy`" ) from None client = tweepy.Client( @@ -39,6 +38,7 @@ def validate_environment(cls, values: dict) -> Any: consumer_secret=api_secret, access_token=access_token, access_token_secret=access_token_secret, + bearer_token=bearer_token, ) values["client"] = client @@ -46,6 +46,7 @@ def validate_environment(cls, values: dict) -> Any: values["api_secret"] = api_secret values["access_token"] = access_token values["access_token_secret"] = access_token_secret + values["bearer_token"] = bearer_token return values From dc1c1cc13de9c423400b2e4c2d0e46b1303f6acb Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 10:24:01 -0800 Subject: [PATCH 06/23] annotations --- .../actions/social/twitter/post_tweet_reply.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py index f5ca91e75..1404b1b25 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py @@ -29,10 +29,11 @@ def post_tweet_reply(client: tweepy.Client, tweet_id: str, tweet_reply: str) -> Args: client (tweepy.Client): The tweepy client to use. - tweet (str): The text of the tweet to post to twitter. Tweets can be maximum 280 characters. + tweet_id (str): The id of the tweet to reply to in twitter. + tweet_reply (str): The text of the reply to post in reponse to a tweet on twitter. Returns: - str: A message containing the result of the post action and the tweet. + str: A message containing the result of the reply action and any associated data. """ message = "" From 77141897c9c04724e7e3cac680b7dda9e3c355b8 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 10:51:14 -0800 Subject: [PATCH 07/23] swaping tweepy to dict responses and adding success/failure response samples --- .../actions/social/twitter/account_details.py | 20 +++++++++++-------- .../social/twitter/account_mentions.py | 16 ++++++++++----- .../actions/social/twitter/post_tweet.py | 17 ++++++++++++---- .../social/twitter/post_tweet_reply.py | 16 ++++++++++----- .../twitter_langchain/twitter_api_wrapper.py | 1 + 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py index 49d48fdba..ea9ec2a6e 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py @@ -1,4 +1,5 @@ from collections.abc import Callable +from json import dumps import tweepy from pydantic import BaseModel @@ -6,7 +7,16 @@ from cdp_agentkit_core.actions.social.twitter.action import TwitterAction ACCOUNT_DETAILS_PROMPT = """ -This tool will return account details for the currently authenticated Twitter (X) user context.""" +This tool will return account details for the currently authenticated Twitter (X) user context. + +A successful response will return a message with the api response as a json payload: + {"data": {"id": "1853889445319331840", "name": "CDP AgentKit", "username": "CDPAgentKit"}} + +A failure response will return a message with the tweepy client api request error: + Error retrieving authenticated user account: 429 Too Many Requests + + +""" class AccountDetailsInput(BaseModel): @@ -27,13 +37,7 @@ def account_details(client: tweepy.Client) -> str: try: response = client.get_me() - user = response.data - - message = f"""Successfully retrieved authenticated user account details. Please present the following as json and not markdown: - id: {user.id} - name: {user.name} - username: {user.username} - link: https://x.com/{user.username}""" + message = f"""Successfully retrieved authenticated user account details:\n{dumps(response)}""" except tweepy.errors.TweepyException as e: message = f"Error retrieving authenticated user account details: {e}" diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py index 16f652d83..ff8578692 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py @@ -7,7 +7,15 @@ from cdp_agentkit_core.actions.social.twitter.action import TwitterAction ACCOUNT_MENTIONS_PROMPT = """ -This tool will return account mentions for the currently authenticated Twitter (X) user context.""" +This tool will return account mentions for the currently authenticated Twitter (X) user context. + +A successful response will return a message with the api response as a json payload: + + +A failure response will return a message with the tweepy client api request error: + Error retrieving authenticated user account mentions: 429 Too Many Requests + +""" class AccountMentionsInput(BaseModel): @@ -36,11 +44,9 @@ def account_mentions(client: tweepy.Client, account_id: str) -> str: try: response = client.get_users_mentions(account_id) - mentions = response.data - - message = f"Successfully retrieved authenticated user account mentions:{dumps(mentions)}" + message = f"Successfully retrieved authenticated user account mentions:\n{dumps(response)}" except tweepy.errors.TweepyException as e: - message = f"Error retrieving authenticated user account mentions: {e}" + message = f"Error retrieving authenticated user account mentions:\n{e}" return message diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet.py index 3a05740f1..e984f2673 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet.py @@ -1,4 +1,5 @@ from collections.abc import Callable +from json import dumps import tweepy from pydantic import BaseModel, Field @@ -6,7 +7,15 @@ from cdp_agentkit_core.actions.social.twitter import TwitterAction POST_TWEET_PROMPT = """ -This tool will post a tweet on Twitter. The tool takes the text of the tweet as input. Tweets can be maximum 280 characters.""" +This tool will post a tweet on Twitter. The tool takes the text of the tweet as input. Tweets can be maximum 280 characters. + +A successful response will return a message with the api response as a json payload: + {"data": {"text": "hello, world!", "id": "0123456789012345678", "edit_history_tweet_ids": ["0123456789012345678"]}} + +A failure response will return a message with the tweepy client api request error: + You are not allowed to create a Tweet with duplicate content. + +""" class PostTweetInput(BaseModel): @@ -32,10 +41,10 @@ def post_tweet(client: tweepy.Client, tweet: str) -> str: message = "" try: - client.create_tweet(text=tweet) - message = f"Successfully posted to Twitter:\n{tweet}" + response = client.create_tweet(text=tweet) + message = f"Successfully posted to Twitter:\n{dumps(response)}" except tweepy.errors.TweepyException as e: - message = f"Error posting to Twitter:\n{tweet}\n{e}" + message = f"Error posting to Twitter:\n{e}" return message diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py index 1404b1b25..52e5c9f88 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet_reply.py @@ -7,7 +7,15 @@ from cdp_agentkit_core.actions.social.twitter import TwitterAction POST_TWEET_REPLY_PROMPT = """ -This tool will post a reply to a tweet on Twitter. The tool takes the text of the reply and the tweet id to reply to as input. Tweets can be maximum 280 characters.""" +This tool will post a reply to a tweet on Twitter. The tool takes the text of the reply and the tweet id to reply to as input. Tweets can be maximum 280 characters. + +A successful response will return a message with the api response as a json payload: + {"data": {"id": "0123456789012345678", "text": "hellllloooo!", "edit_history_tweet_ids": ["1234567890123456789"]}} + +A failure response will return a message with the tweepy client api request error: + You are not allowed to create a Tweet with duplicate content. + +""" class PostTweetReplyInput(BaseModel): @@ -40,11 +48,9 @@ def post_tweet_reply(client: tweepy.Client, tweet_id: str, tweet_reply: str) -> try: response = client.create_tweet(text=tweet_reply, in_reply_to_tweet_id=tweet_id) - data = response.data - - message = f"Successfully posted reply to Twitter:\n{dumps(data)}" + message = f"Successfully posted reply to Twitter:\n{dumps(response)}" except tweepy.errors.TweepyException as e: - message = f"Error posting reply to Twitter: {e}" + message = f"Error posting reply to Twitter:\n{e}" return message diff --git a/twitter-langchain/twitter_langchain/twitter_api_wrapper.py b/twitter-langchain/twitter_langchain/twitter_api_wrapper.py index 8f84dc4f0..2e823c01b 100644 --- a/twitter-langchain/twitter_langchain/twitter_api_wrapper.py +++ b/twitter-langchain/twitter_langchain/twitter_api_wrapper.py @@ -39,6 +39,7 @@ def validate_environment(cls, values: dict) -> Any: access_token=access_token, access_token_secret=access_token_secret, bearer_token=bearer_token, + return_type=dict, ) values["client"] = client From 3fae91e27fc002541e4de194561ece9eea84e49a Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 10:55:50 -0800 Subject: [PATCH 08/23] adding success json response for twitter account mentions --- .../actions/social/twitter/account_mentions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py index ff8578692..6cc173a12 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py @@ -8,8 +8,10 @@ ACCOUNT_MENTIONS_PROMPT = """ This tool will return account mentions for the currently authenticated Twitter (X) user context. +Please note that this may only be called once every 15 minutes under the free api tier. A successful response will return a message with the api response as a json payload: + {"data": [{"id": "1857479287504584856", "edit_history_tweet_ids": ["1857479287504584856"], "text": "@CDPAgentKit reply"}], "meta": {"result_count": 1, "newest_id": "1857479287504584856", "oldest_id": "1857479287504584856"}} A failure response will return a message with the tweepy client api request error: From 9f67f117c216d155120cd29b6343bde85951a241 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 11:16:20 -0800 Subject: [PATCH 09/23] prep: copying chatbot preserving histories --- {cdp-langchain => twitter-langchain}/examples/chatbot/README.md | 0 {cdp-langchain => twitter-langchain}/examples/chatbot/__init__.py | 0 {cdp-langchain => twitter-langchain}/examples/chatbot/chatbot.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {cdp-langchain => twitter-langchain}/examples/chatbot/README.md (100%) rename {cdp-langchain => twitter-langchain}/examples/chatbot/__init__.py (100%) rename {cdp-langchain => twitter-langchain}/examples/chatbot/chatbot.py (100%) diff --git a/cdp-langchain/examples/chatbot/README.md b/twitter-langchain/examples/chatbot/README.md similarity index 100% rename from cdp-langchain/examples/chatbot/README.md rename to twitter-langchain/examples/chatbot/README.md diff --git a/cdp-langchain/examples/chatbot/__init__.py b/twitter-langchain/examples/chatbot/__init__.py similarity index 100% rename from cdp-langchain/examples/chatbot/__init__.py rename to twitter-langchain/examples/chatbot/__init__.py diff --git a/cdp-langchain/examples/chatbot/chatbot.py b/twitter-langchain/examples/chatbot/chatbot.py similarity index 100% rename from cdp-langchain/examples/chatbot/chatbot.py rename to twitter-langchain/examples/chatbot/chatbot.py From 3597d49e68a2ff40fd4fa5211e849041d1e6971b Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 11:17:11 -0800 Subject: [PATCH 10/23] prep: copying chatbot preserving histories --- cdp-langchain/examples/{chatbot => chatbot.copy}/README.md | 0 cdp-langchain/examples/{chatbot => chatbot.copy}/__init__.py | 0 cdp-langchain/examples/{chatbot => chatbot.copy}/chatbot.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename cdp-langchain/examples/{chatbot => chatbot.copy}/README.md (100%) rename cdp-langchain/examples/{chatbot => chatbot.copy}/__init__.py (100%) rename cdp-langchain/examples/{chatbot => chatbot.copy}/chatbot.py (100%) diff --git a/cdp-langchain/examples/chatbot/README.md b/cdp-langchain/examples/chatbot.copy/README.md similarity index 100% rename from cdp-langchain/examples/chatbot/README.md rename to cdp-langchain/examples/chatbot.copy/README.md diff --git a/cdp-langchain/examples/chatbot/__init__.py b/cdp-langchain/examples/chatbot.copy/__init__.py similarity index 100% rename from cdp-langchain/examples/chatbot/__init__.py rename to cdp-langchain/examples/chatbot.copy/__init__.py diff --git a/cdp-langchain/examples/chatbot/chatbot.py b/cdp-langchain/examples/chatbot.copy/chatbot.py similarity index 100% rename from cdp-langchain/examples/chatbot/chatbot.py rename to cdp-langchain/examples/chatbot.copy/chatbot.py From 573858b5288ab1944879ad0b8c40d138ba275aa4 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 11:17:48 -0800 Subject: [PATCH 11/23] prep: copying chatbot preserving histories --- cdp-langchain/examples/{chatbot.copy => chatbot}/README.md | 0 cdp-langchain/examples/{chatbot.copy => chatbot}/__init__.py | 0 cdp-langchain/examples/{chatbot.copy => chatbot}/chatbot.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename cdp-langchain/examples/{chatbot.copy => chatbot}/README.md (100%) rename cdp-langchain/examples/{chatbot.copy => chatbot}/__init__.py (100%) rename cdp-langchain/examples/{chatbot.copy => chatbot}/chatbot.py (100%) diff --git a/cdp-langchain/examples/chatbot.copy/README.md b/cdp-langchain/examples/chatbot/README.md similarity index 100% rename from cdp-langchain/examples/chatbot.copy/README.md rename to cdp-langchain/examples/chatbot/README.md diff --git a/cdp-langchain/examples/chatbot.copy/__init__.py b/cdp-langchain/examples/chatbot/__init__.py similarity index 100% rename from cdp-langchain/examples/chatbot.copy/__init__.py rename to cdp-langchain/examples/chatbot/__init__.py diff --git a/cdp-langchain/examples/chatbot.copy/chatbot.py b/cdp-langchain/examples/chatbot/chatbot.py similarity index 100% rename from cdp-langchain/examples/chatbot.copy/chatbot.py rename to cdp-langchain/examples/chatbot/chatbot.py From 929aa68ddebadc376b4d9536fdc0001dd24acec6 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 11:29:45 -0800 Subject: [PATCH 12/23] finalizing: copying chatbot with histories to twitter langchain --- twitter-langchain/examples/chatbot/.env-local | 6 +++ twitter-langchain/examples/chatbot/.gitignore | 1 + twitter-langchain/examples/chatbot/Makefile | 10 +++++ twitter-langchain/examples/chatbot/chatbot.py | 37 +++++++------------ .../examples/chatbot/pyproject.toml | 26 +++++++++++++ 5 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 twitter-langchain/examples/chatbot/.env-local create mode 100644 twitter-langchain/examples/chatbot/.gitignore create mode 100644 twitter-langchain/examples/chatbot/Makefile create mode 100644 twitter-langchain/examples/chatbot/pyproject.toml diff --git a/twitter-langchain/examples/chatbot/.env-local b/twitter-langchain/examples/chatbot/.env-local new file mode 100644 index 000000000..b6dd036d7 --- /dev/null +++ b/twitter-langchain/examples/chatbot/.env-local @@ -0,0 +1,6 @@ + OPENAI_API_KEY= + TWITTER_ACCESS_TOKEN= + TWITTER_ACCESS_TOKEN_SECRET= + TWITTER_API_KEY= + TWITTER_API_SECRET= + TWITTER_BEARER_TOKEN= diff --git a/twitter-langchain/examples/chatbot/.gitignore b/twitter-langchain/examples/chatbot/.gitignore new file mode 100644 index 000000000..c04bc49f7 --- /dev/null +++ b/twitter-langchain/examples/chatbot/.gitignore @@ -0,0 +1 @@ +poetry.lock diff --git a/twitter-langchain/examples/chatbot/Makefile b/twitter-langchain/examples/chatbot/Makefile new file mode 100644 index 000000000..df87b8036 --- /dev/null +++ b/twitter-langchain/examples/chatbot/Makefile @@ -0,0 +1,10 @@ +ifneq (,$(wildcard ./.env-local)) + include .env-local +endif + +export + +.PHONY: run +run: + poetry install && \ + poetry run python chatbot.py diff --git a/twitter-langchain/examples/chatbot/chatbot.py b/twitter-langchain/examples/chatbot/chatbot.py index 2afd41eaf..8bcb6ae27 100644 --- a/twitter-langchain/examples/chatbot/chatbot.py +++ b/twitter-langchain/examples/chatbot/chatbot.py @@ -1,3 +1,5 @@ +import asyncio +import multiprocessing import os import sys import time @@ -7,41 +9,28 @@ from langgraph.checkpoint.memory import MemorySaver from langgraph.prebuilt import create_react_agent -# Import CDP Agentkit Langchain Extension. -from cdp_langchain.agent_toolkits import CdpToolkit -from cdp_langchain.utils import CdpAgentkitWrapper +# Import CDP Agentkit Twitter Langchain Extension. +from twitter_langchain import ( + TwitterApiWrapper, + TwitterToolkit, +) # Configure a file to persist the agent's CDP MPC Wallet Data. wallet_data_file = "wallet_data.txt" def initialize_agent(): - """Initialize the agent with CDP Agentkit.""" + """Initialize the agent with CDP Agentkit Twitter Langchain.""" # Initialize LLM. llm = ChatOpenAI(model="gpt-4o-mini") - wallet_data = None - - if os.path.exists(wallet_data_file): - with open(wallet_data_file) as f: - wallet_data = f.read() - - # Configure CDP Agentkit Langchain Extension. + # Configure CDP Agentkit Twitter Langchain Extension. values = {} - if wallet_data is not None: - # If there is a persisted agentic wallet, load it and pass to the CDP Agentkit Wrapper. - values = {"cdp_wallet_data": wallet_data} - - agentkit = CdpAgentkitWrapper(**values) - - # persist the agent's CDP MPC Wallet Data. - wallet_data = agentkit.export_wallet() - with open(wallet_data_file, "w") as f: - f.write(wallet_data) - # Initialize CDP Agentkit Toolkit and get tools. - cdp_toolkit = CdpToolkit.from_cdp_agentkit_wrapper(agentkit) - tools = cdp_toolkit.get_tools() + # Initialize CDP Agentkit Twitter Langchain + wrapper = TwitterApiWrapper(**values) + toolkit = TwitterToolkit.from_twitter_api_wrapper(wrapper) + tools = toolkit.get_tools() # Store buffered conversation history in memory. memory = MemorySaver() diff --git a/twitter-langchain/examples/chatbot/pyproject.toml b/twitter-langchain/examples/chatbot/pyproject.toml new file mode 100644 index 000000000..ca3d97d9e --- /dev/null +++ b/twitter-langchain/examples/chatbot/pyproject.toml @@ -0,0 +1,26 @@ +[tool.poetry] +name = "chat" +version = "0.0.1" +description = "CDP Agentkit Langchain Chat Example" +authors = ["John Peterson "] +readme = "README.md" +license = "Apache-2.0" +keywords = ["coinbase", "sdk", "crypto", "cdp", "agentkit", "ai", "agent", "langchain", "toolkit"] +packages = [] + +[tool.poetry.dependencies] +cdp-sdk = "^0.10.3" +cdp-agentkit-core = {path = "/Users/chris/repositories/cdp-agentkit/cdp-agentkit-core", develop = true} +cdp-langchain = {path = "/Users/chris/repositories/cdp-agentkit/cdp-langchain", develop = true} +twitter-langchain = {path = "/Users/chris/repositories/cdp-agentkit/twitter-langchain", develop = true} +langchain = "^0.3.4" +langchain-openai = "^0.2.4" +langgraph = "^0.2.39" +python = "^3.10" +tweepy = "^4.14.0" + +[tool.poetry.group.dev.dependencies] + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" From 5f18a018661d5a6e1cc33eb3da2835cdb6d6ba88 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 11:37:01 -0800 Subject: [PATCH 13/23] updating twitter chatbot readme --- twitter-langchain/examples/chatbot/README.md | 33 ++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/twitter-langchain/examples/chatbot/README.md b/twitter-langchain/examples/chatbot/README.md index 891714f05..6a9531fe9 100644 --- a/twitter-langchain/examples/chatbot/README.md +++ b/twitter-langchain/examples/chatbot/README.md @@ -1,20 +1,20 @@ -# CDP Agentkit Langchain Extension Examples - Chatbot +# CDP Agentkit Twitter Langchain Extension Examples - Chatbot -This example demonstrates an agent setup as a terminal style chatbot with access to the full set of CDP Agentkit actions. +This example demonstrates an agent setup as a terminal style chatbot with access to Twitter (X) API actions. -## Ask the chatbot to engage in the Web3 ecosystem! +## Ask the chatbot to engage in the Twitter (X) ecosystem! - "Transfer a portion of your ETH to john2879.base.eth" -- "Deploy an NFT that will go super viral!" -- "Choose a name for yourself and register a Basename for your wallet" -- "Deploy an ERC-20 token with total supply 1 billion" +- "What are my account details?" +- "Please post a message for me to Twitter" +- "Please get my mentions" +- "Please post responses to my mentions" ## Requirements - Python 3.10+ -- [CDP API Key](https://portal.cdp.coinbase.com/access/api) - [OpenAI API Key](https://platform.openai.com/docs/quickstart#create-and-export-an-api-key) +- [Twitter (X) API Key's](https://developer.x.com/en/portal/dashboard) ### Checking Python Version -Before using the example, ensure that you have the correct version of Python installed. The example requires Python 3.10 or higher. You can check your Python version by running the following code: ```bash python --version @@ -23,18 +23,19 @@ pip --version ## Installation ```bash -pip install cdp-langchain ``` ## Run the Chatbot -### Set ENV Vars -- Ensure the following ENV Vars are set: - - "CDP_API_KEY_NAME" - - "CDP_API_KEY_PRIVATE_KEY" - - "OPENAI_API_KEY" - - "NETWORK_ID" (Defaults to `base-sepolia`) +### Env +Ensure the following vars are set in .env-local: +- "OPENAI_API_KEY" +- "TWITTER_ACCESS_TOKEN" +- "TWITTER_ACCESS_TOKEN_SECRET" +- "TWITTER_API_KEY" +- "TWITTER_API_SECRET" +- "TWITTER_BEARER_TOKEN" ```bash -python chatbot.py +make run ``` From 08bd13ba8b7a145b662b40436943f4e3d93fcf93 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 11:41:10 -0800 Subject: [PATCH 14/23] updating dev paths for twitter chatbot --- twitter-langchain/examples/chatbot/.gitignore | 1 + twitter-langchain/examples/chatbot/pyproject.toml | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/twitter-langchain/examples/chatbot/.gitignore b/twitter-langchain/examples/chatbot/.gitignore index c04bc49f7..7ea1dc999 100644 --- a/twitter-langchain/examples/chatbot/.gitignore +++ b/twitter-langchain/examples/chatbot/.gitignore @@ -1 +1,2 @@ +.env-local poetry.lock diff --git a/twitter-langchain/examples/chatbot/pyproject.toml b/twitter-langchain/examples/chatbot/pyproject.toml index ca3d97d9e..f38800ecb 100644 --- a/twitter-langchain/examples/chatbot/pyproject.toml +++ b/twitter-langchain/examples/chatbot/pyproject.toml @@ -10,9 +10,9 @@ packages = [] [tool.poetry.dependencies] cdp-sdk = "^0.10.3" -cdp-agentkit-core = {path = "/Users/chris/repositories/cdp-agentkit/cdp-agentkit-core", develop = true} -cdp-langchain = {path = "/Users/chris/repositories/cdp-agentkit/cdp-langchain", develop = true} -twitter-langchain = {path = "/Users/chris/repositories/cdp-agentkit/twitter-langchain", develop = true} +cdp-agentkit-core = {path = "../../../cdp-agentkit-core", develop = true} +cdp-langchain = {path = "../../../cdp-langchain", develop = true} +twitter-langchain = {path = "../../../twitter-langchain", develop = true} langchain = "^0.3.4" langchain-openai = "^0.2.4" langgraph = "^0.2.39" From 7b6a55085e1f0dc34c3d603050b4f88457a82770 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 12:08:04 -0800 Subject: [PATCH 15/23] updating twitter chatbot readme --- twitter-langchain/examples/chatbot/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/twitter-langchain/examples/chatbot/README.md b/twitter-langchain/examples/chatbot/README.md index 6a9531fe9..803a29e3c 100644 --- a/twitter-langchain/examples/chatbot/README.md +++ b/twitter-langchain/examples/chatbot/README.md @@ -14,6 +14,18 @@ This example demonstrates an agent setup as a terminal style chatbot with access - [OpenAI API Key](https://platform.openai.com/docs/quickstart#create-and-export-an-api-key) - [Twitter (X) API Key's](https://developer.x.com/en/portal/dashboard) +### Twitter Application Setup +1. Visit the Twitter (X) [Developer Portal](https://developer.x.com/en/portal/dashboard) +2. Navigate to your project +3. Navigate to your application +4. Edit "User authentication settings" +5. Set "App permissions" to "Read and write and Direct message" +6. Set "Type of App" to "Web App, Automated app or Bot" +7. Set "App info" urls +8. Save +9. Navigate to "Keys and tokens" +10. Regenerate all keys and tokens + ### Checking Python Version ```bash From 960b27e9a46d404db09ef0f0f930ee9ced48ea9f Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 12:08:19 -0800 Subject: [PATCH 16/23] adding twitter account url back into the response --- .../actions/social/twitter/account_details.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py index ea9ec2a6e..5af2281f2 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py @@ -37,6 +37,9 @@ def account_details(client: tweepy.Client) -> str: try: response = client.get_me() + data = response['data'] + data['url'] = f"https://x.com/{data['username']}" + message = f"""Successfully retrieved authenticated user account details:\n{dumps(response)}""" except tweepy.errors.TweepyException as e: message = f"Error retrieving authenticated user account details: {e}" From 98c623b7ff79f54148e9f087051553a6ae4e571d Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 12:13:45 -0800 Subject: [PATCH 17/23] updating twitter account details example --- .../examples/account_details/.gitignore | 2 ++ .../examples/account_details/Makefile | 2 +- .../account_details/account_details.py | 18 +++++-------- .../examples/account_details/pyproject.toml | 26 +++++++++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 twitter-langchain/examples/account_details/.gitignore create mode 100644 twitter-langchain/examples/account_details/pyproject.toml diff --git a/twitter-langchain/examples/account_details/.gitignore b/twitter-langchain/examples/account_details/.gitignore new file mode 100644 index 000000000..7ea1dc999 --- /dev/null +++ b/twitter-langchain/examples/account_details/.gitignore @@ -0,0 +1,2 @@ +.env-local +poetry.lock diff --git a/twitter-langchain/examples/account_details/Makefile b/twitter-langchain/examples/account_details/Makefile index b2bf7df51..0ab0840ad 100644 --- a/twitter-langchain/examples/account_details/Makefile +++ b/twitter-langchain/examples/account_details/Makefile @@ -6,5 +6,5 @@ export .PHONY: run run: - poetry install --with dev + poetry install --with dev && \ poetry run python account_details.py diff --git a/twitter-langchain/examples/account_details/account_details.py b/twitter-langchain/examples/account_details/account_details.py index 2bd6e5b6f..219c2d266 100644 --- a/twitter-langchain/examples/account_details/account_details.py +++ b/twitter-langchain/examples/account_details/account_details.py @@ -44,15 +44,11 @@ # ================================= Tool Message ================================= # Name: account_details -# Successfully retrieved authenticated user account details. Please present the following as json and not markdown: -# id: 1234567890123456789 -# name: My Twitter Name -# username: MyTwitterUserName -# link: https://x.com/MyTwitterUserName +# Successfully retrieved authenticated user account details: +# {"data": {"id": "1853889445319331840", "name": "CDP AgentKit", "username": "CDPAgentKit", "url": "https://x.com/CDPAgentKit"}} # ================================== Ai Message ================================== -# { -# "id": "1234567890123456789", -# "name": "My Twitter Name", -# "username": "MyTwitterUserName", -# "link": "https://x.com/MyTwitterUserName" -# } +# Here are your Twitter account details: + +# - **Name: ** CDP AgentKit +# - **Username: ** [@CDPAgentKit](https: // x.com/CDPAgentKit) +# - **Account ID: ** 1853889445319331840 diff --git a/twitter-langchain/examples/account_details/pyproject.toml b/twitter-langchain/examples/account_details/pyproject.toml new file mode 100644 index 000000000..f38800ecb --- /dev/null +++ b/twitter-langchain/examples/account_details/pyproject.toml @@ -0,0 +1,26 @@ +[tool.poetry] +name = "chat" +version = "0.0.1" +description = "CDP Agentkit Langchain Chat Example" +authors = ["John Peterson "] +readme = "README.md" +license = "Apache-2.0" +keywords = ["coinbase", "sdk", "crypto", "cdp", "agentkit", "ai", "agent", "langchain", "toolkit"] +packages = [] + +[tool.poetry.dependencies] +cdp-sdk = "^0.10.3" +cdp-agentkit-core = {path = "../../../cdp-agentkit-core", develop = true} +cdp-langchain = {path = "../../../cdp-langchain", develop = true} +twitter-langchain = {path = "../../../twitter-langchain", develop = true} +langchain = "^0.3.4" +langchain-openai = "^0.2.4" +langgraph = "^0.2.39" +python = "^3.10" +tweepy = "^4.14.0" + +[tool.poetry.group.dev.dependencies] + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" From 3f0da664aa0cbadef42993d5285683730d4544cb Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 12:24:26 -0800 Subject: [PATCH 18/23] updating twitter langchain examples --- .../examples/account_details/.env-local | 6 +++++ .../examples/account_details/.gitignore | 2 +- twitter-langchain/examples/chatbot/.gitignore | 2 +- twitter-langchain/examples/chatbot/Makefile | 4 +-- twitter-langchain/examples/chatbot/README.md | 6 ++--- .../examples/post_tweet/.env-local | 6 +++++ .../examples/post_tweet/.gitignore | 2 ++ .../examples/post_tweet/Makefile | 2 +- .../examples/post_tweet/post_tweet.py | 10 ++++--- .../examples/post_tweet/pyproject.toml | 26 +++++++++++++++++++ 10 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 twitter-langchain/examples/account_details/.env-local create mode 100644 twitter-langchain/examples/post_tweet/.env-local create mode 100644 twitter-langchain/examples/post_tweet/.gitignore create mode 100644 twitter-langchain/examples/post_tweet/pyproject.toml diff --git a/twitter-langchain/examples/account_details/.env-local b/twitter-langchain/examples/account_details/.env-local new file mode 100644 index 000000000..b6dd036d7 --- /dev/null +++ b/twitter-langchain/examples/account_details/.env-local @@ -0,0 +1,6 @@ + OPENAI_API_KEY= + TWITTER_ACCESS_TOKEN= + TWITTER_ACCESS_TOKEN_SECRET= + TWITTER_API_KEY= + TWITTER_API_SECRET= + TWITTER_BEARER_TOKEN= diff --git a/twitter-langchain/examples/account_details/.gitignore b/twitter-langchain/examples/account_details/.gitignore index 7ea1dc999..ff3ea062f 100644 --- a/twitter-langchain/examples/account_details/.gitignore +++ b/twitter-langchain/examples/account_details/.gitignore @@ -1,2 +1,2 @@ -.env-local +.env poetry.lock diff --git a/twitter-langchain/examples/chatbot/.gitignore b/twitter-langchain/examples/chatbot/.gitignore index 7ea1dc999..ff3ea062f 100644 --- a/twitter-langchain/examples/chatbot/.gitignore +++ b/twitter-langchain/examples/chatbot/.gitignore @@ -1,2 +1,2 @@ -.env-local +.env poetry.lock diff --git a/twitter-langchain/examples/chatbot/Makefile b/twitter-langchain/examples/chatbot/Makefile index df87b8036..139e13de6 100644 --- a/twitter-langchain/examples/chatbot/Makefile +++ b/twitter-langchain/examples/chatbot/Makefile @@ -1,5 +1,5 @@ -ifneq (,$(wildcard ./.env-local)) - include .env-local +ifneq (,$(wildcard ./.env)) + include .env endif export diff --git a/twitter-langchain/examples/chatbot/README.md b/twitter-langchain/examples/chatbot/README.md index 803a29e3c..d21cc3538 100644 --- a/twitter-langchain/examples/chatbot/README.md +++ b/twitter-langchain/examples/chatbot/README.md @@ -33,10 +33,6 @@ python --version pip --version ``` -## Installation -```bash -``` - ## Run the Chatbot ### Env @@ -48,6 +44,8 @@ Ensure the following vars are set in .env-local: - "TWITTER_API_SECRET" - "TWITTER_BEARER_TOKEN" +Rename .env-local to .env + ```bash make run ``` diff --git a/twitter-langchain/examples/post_tweet/.env-local b/twitter-langchain/examples/post_tweet/.env-local new file mode 100644 index 000000000..b6dd036d7 --- /dev/null +++ b/twitter-langchain/examples/post_tweet/.env-local @@ -0,0 +1,6 @@ + OPENAI_API_KEY= + TWITTER_ACCESS_TOKEN= + TWITTER_ACCESS_TOKEN_SECRET= + TWITTER_API_KEY= + TWITTER_API_SECRET= + TWITTER_BEARER_TOKEN= diff --git a/twitter-langchain/examples/post_tweet/.gitignore b/twitter-langchain/examples/post_tweet/.gitignore new file mode 100644 index 000000000..ff3ea062f --- /dev/null +++ b/twitter-langchain/examples/post_tweet/.gitignore @@ -0,0 +1,2 @@ +.env +poetry.lock diff --git a/twitter-langchain/examples/post_tweet/Makefile b/twitter-langchain/examples/post_tweet/Makefile index 243dee120..220eb3965 100644 --- a/twitter-langchain/examples/post_tweet/Makefile +++ b/twitter-langchain/examples/post_tweet/Makefile @@ -6,5 +6,5 @@ export .PHONY: run run: - poetry install --with dev + poetry install && \ poetry run python post_tweet.py diff --git a/twitter-langchain/examples/post_tweet/post_tweet.py b/twitter-langchain/examples/post_tweet/post_tweet.py index d5faba9cd..5e84e44e2 100644 --- a/twitter-langchain/examples/post_tweet/post_tweet.py +++ b/twitter-langchain/examples/post_tweet/post_tweet.py @@ -38,15 +38,17 @@ # Successful Output # ================================ Human Message ================================= -# Please post 'hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa' to twitter +# Please post 'hello, world! 2f11acf41ab2412cab56ea9a1d447d74' to twitter # ================================== Ai Message ================================== # Tool Calls: # post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq) # Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq # Args: -# text: hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa +# tweet: hello, world! 2f11acf41ab2412cab56ea9a1d447d74 # ================================= Tool Message ================================= # Name: post_tweet -# Successfully posted! + +# Successfully posted to Twitter: +# {"data": {"text": "hello, world! 2f11acf41ab2412cab56ea9a1d447d74", "id": "1857519455640891432", "edit_history_tweet_ids": ["1857519455640891432"]}} # ================================== Ai Message ================================== -# The message "hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa" has been successfully posted to Twitter! +# The tweet "hello, world! 2f11acf41ab2412cab56ea9a1d447d74" has been successfully posted to Twitter. diff --git a/twitter-langchain/examples/post_tweet/pyproject.toml b/twitter-langchain/examples/post_tweet/pyproject.toml new file mode 100644 index 000000000..f38800ecb --- /dev/null +++ b/twitter-langchain/examples/post_tweet/pyproject.toml @@ -0,0 +1,26 @@ +[tool.poetry] +name = "chat" +version = "0.0.1" +description = "CDP Agentkit Langchain Chat Example" +authors = ["John Peterson "] +readme = "README.md" +license = "Apache-2.0" +keywords = ["coinbase", "sdk", "crypto", "cdp", "agentkit", "ai", "agent", "langchain", "toolkit"] +packages = [] + +[tool.poetry.dependencies] +cdp-sdk = "^0.10.3" +cdp-agentkit-core = {path = "../../../cdp-agentkit-core", develop = true} +cdp-langchain = {path = "../../../cdp-langchain", develop = true} +twitter-langchain = {path = "../../../twitter-langchain", develop = true} +langchain = "^0.3.4" +langchain-openai = "^0.2.4" +langgraph = "^0.2.39" +python = "^3.10" +tweepy = "^4.14.0" + +[tool.poetry.group.dev.dependencies] + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" From d697fb9f18c6e72fe575166e71d4395c859a5147 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 12:31:20 -0800 Subject: [PATCH 19/23] adding --with dev to poetry install for twitter examples --- twitter-langchain/examples/chatbot/Makefile | 2 +- twitter-langchain/examples/post_tweet/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/twitter-langchain/examples/chatbot/Makefile b/twitter-langchain/examples/chatbot/Makefile index 139e13de6..44d813633 100644 --- a/twitter-langchain/examples/chatbot/Makefile +++ b/twitter-langchain/examples/chatbot/Makefile @@ -6,5 +6,5 @@ export .PHONY: run run: - poetry install && \ + poetry install --with dev && \ poetry run python chatbot.py diff --git a/twitter-langchain/examples/post_tweet/Makefile b/twitter-langchain/examples/post_tweet/Makefile index 220eb3965..6e3158f59 100644 --- a/twitter-langchain/examples/post_tweet/Makefile +++ b/twitter-langchain/examples/post_tweet/Makefile @@ -6,5 +6,5 @@ export .PHONY: run run: - poetry install && \ + poetry install --with dev && \ poetry run python post_tweet.py From 977c9b89cb3fdeaf2c76dde0798aafcb0013b9e2 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 12:42:19 -0800 Subject: [PATCH 20/23] removing unneeded twitter examples --- .../examples/account_details/.env-local | 6 --- .../examples/account_details/.gitignore | 2 - .../examples/account_details/Makefile | 10 ---- .../account_details/account_details.py | 54 ------------------- .../examples/account_details/pyproject.toml | 26 --------- .../examples/post_tweet/.env-local | 6 --- .../examples/post_tweet/.gitignore | 2 - .../examples/post_tweet/Makefile | 10 ---- .../examples/post_tweet/post_tweet.py | 54 ------------------- .../examples/post_tweet/pyproject.toml | 26 --------- 10 files changed, 196 deletions(-) delete mode 100644 twitter-langchain/examples/account_details/.env-local delete mode 100644 twitter-langchain/examples/account_details/.gitignore delete mode 100644 twitter-langchain/examples/account_details/Makefile delete mode 100644 twitter-langchain/examples/account_details/account_details.py delete mode 100644 twitter-langchain/examples/account_details/pyproject.toml delete mode 100644 twitter-langchain/examples/post_tweet/.env-local delete mode 100644 twitter-langchain/examples/post_tweet/.gitignore delete mode 100644 twitter-langchain/examples/post_tweet/Makefile delete mode 100644 twitter-langchain/examples/post_tweet/post_tweet.py delete mode 100644 twitter-langchain/examples/post_tweet/pyproject.toml diff --git a/twitter-langchain/examples/account_details/.env-local b/twitter-langchain/examples/account_details/.env-local deleted file mode 100644 index b6dd036d7..000000000 --- a/twitter-langchain/examples/account_details/.env-local +++ /dev/null @@ -1,6 +0,0 @@ - OPENAI_API_KEY= - TWITTER_ACCESS_TOKEN= - TWITTER_ACCESS_TOKEN_SECRET= - TWITTER_API_KEY= - TWITTER_API_SECRET= - TWITTER_BEARER_TOKEN= diff --git a/twitter-langchain/examples/account_details/.gitignore b/twitter-langchain/examples/account_details/.gitignore deleted file mode 100644 index ff3ea062f..000000000 --- a/twitter-langchain/examples/account_details/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.env -poetry.lock diff --git a/twitter-langchain/examples/account_details/Makefile b/twitter-langchain/examples/account_details/Makefile deleted file mode 100644 index 0ab0840ad..000000000 --- a/twitter-langchain/examples/account_details/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -ifneq (,$(wildcard ./.env)) - include .env -endif - -export - -.PHONY: run -run: - poetry install --with dev && \ - poetry run python account_details.py diff --git a/twitter-langchain/examples/account_details/account_details.py b/twitter-langchain/examples/account_details/account_details.py deleted file mode 100644 index 219c2d266..000000000 --- a/twitter-langchain/examples/account_details/account_details.py +++ /dev/null @@ -1,54 +0,0 @@ -from langchain_core.messages import HumanMessage -from langchain_openai import ChatOpenAI -from langgraph.prebuilt import create_react_agent - -from twitter_langchain import TwitterApiWrapper, TwitterToolkit - -# Initialize TwitterApiwrapper -twitter_api_wrapper = TwitterApiWrapper() - -# Create TwitterToolkit from the api wrapper -twitter_toolkit = TwitterToolkit.from_twitter_api_wrapper(twitter_api_wrapper) - -# View available tools -tools = twitter_toolkit.get_tools() -for tool in tools: - print(tool.name) - -# Initialize LLM -llm = ChatOpenAI(model="gpt-4o-mini") - -# Create agent -agent_executor = create_react_agent(llm, tools) - -# Example - get account details -events = agent_executor.stream( - { - "messages": [ - HumanMessage(content="Please obtain my twitter account information"), - ], - }, - stream_mode="values", -) - -for event in events: - event["messages"][-1].pretty_print() - -# ================================ Human Message ================================= -# Please obtain my twitter account information -# ================================== Ai Message ================================== -# Tool Calls: -# account_details (call_pYME8H1tHfdMakFZ1FTS0VBX) -# Call ID: call_pYME8H1tHfdMakFZ1FTS0VBX -# Args: -# ================================= Tool Message ================================= -# Name: account_details - -# Successfully retrieved authenticated user account details: -# {"data": {"id": "1853889445319331840", "name": "CDP AgentKit", "username": "CDPAgentKit", "url": "https://x.com/CDPAgentKit"}} -# ================================== Ai Message ================================== -# Here are your Twitter account details: - -# - **Name: ** CDP AgentKit -# - **Username: ** [@CDPAgentKit](https: // x.com/CDPAgentKit) -# - **Account ID: ** 1853889445319331840 diff --git a/twitter-langchain/examples/account_details/pyproject.toml b/twitter-langchain/examples/account_details/pyproject.toml deleted file mode 100644 index f38800ecb..000000000 --- a/twitter-langchain/examples/account_details/pyproject.toml +++ /dev/null @@ -1,26 +0,0 @@ -[tool.poetry] -name = "chat" -version = "0.0.1" -description = "CDP Agentkit Langchain Chat Example" -authors = ["John Peterson "] -readme = "README.md" -license = "Apache-2.0" -keywords = ["coinbase", "sdk", "crypto", "cdp", "agentkit", "ai", "agent", "langchain", "toolkit"] -packages = [] - -[tool.poetry.dependencies] -cdp-sdk = "^0.10.3" -cdp-agentkit-core = {path = "../../../cdp-agentkit-core", develop = true} -cdp-langchain = {path = "../../../cdp-langchain", develop = true} -twitter-langchain = {path = "../../../twitter-langchain", develop = true} -langchain = "^0.3.4" -langchain-openai = "^0.2.4" -langgraph = "^0.2.39" -python = "^3.10" -tweepy = "^4.14.0" - -[tool.poetry.group.dev.dependencies] - -[build-system] -requires = ["poetry-core"] -build-backend = "poetry.core.masonry.api" diff --git a/twitter-langchain/examples/post_tweet/.env-local b/twitter-langchain/examples/post_tweet/.env-local deleted file mode 100644 index b6dd036d7..000000000 --- a/twitter-langchain/examples/post_tweet/.env-local +++ /dev/null @@ -1,6 +0,0 @@ - OPENAI_API_KEY= - TWITTER_ACCESS_TOKEN= - TWITTER_ACCESS_TOKEN_SECRET= - TWITTER_API_KEY= - TWITTER_API_SECRET= - TWITTER_BEARER_TOKEN= diff --git a/twitter-langchain/examples/post_tweet/.gitignore b/twitter-langchain/examples/post_tweet/.gitignore deleted file mode 100644 index ff3ea062f..000000000 --- a/twitter-langchain/examples/post_tweet/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.env -poetry.lock diff --git a/twitter-langchain/examples/post_tweet/Makefile b/twitter-langchain/examples/post_tweet/Makefile deleted file mode 100644 index 6e3158f59..000000000 --- a/twitter-langchain/examples/post_tweet/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -ifneq (,$(wildcard ./.env)) - include .env -endif - -export - -.PHONY: run -run: - poetry install --with dev && \ - poetry run python post_tweet.py diff --git a/twitter-langchain/examples/post_tweet/post_tweet.py b/twitter-langchain/examples/post_tweet/post_tweet.py deleted file mode 100644 index 5e84e44e2..000000000 --- a/twitter-langchain/examples/post_tweet/post_tweet.py +++ /dev/null @@ -1,54 +0,0 @@ -import uuid - -from langchain_core.messages import HumanMessage -from langchain_openai import ChatOpenAI -from langgraph.prebuilt import create_react_agent - -from twitter_langchain import TwitterApiWrapper, TwitterToolkit - -# Initialize TwitterApiwrapper -twitter_api_wrapper = TwitterApiWrapper() - -# Create TwitterToolkit from the api wrapper -twitter_toolkit = TwitterToolkit.from_twitter_api_wrapper(twitter_api_wrapper) - -# View available tools -tools = twitter_toolkit.get_tools() -for tool in tools: - print(tool.name) - -# Initialize LLM -llm = ChatOpenAI(model="gpt-4o-mini") - -# Create agent -agent_executor = create_react_agent(llm, tools) - -# Example - post tweet -events = agent_executor.stream( - { - "messages": [ - HumanMessage(content=f"Please post 'hello, world! {uuid.uuid4().hex}' to twitter"), - ], - }, - stream_mode="values", -) - -for event in events: - event["messages"][-1].pretty_print() - -# Successful Output -# ================================ Human Message ================================= -# Please post 'hello, world! 2f11acf41ab2412cab56ea9a1d447d74' to twitter -# ================================== Ai Message ================================== -# Tool Calls: -# post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq) -# Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq -# Args: -# tweet: hello, world! 2f11acf41ab2412cab56ea9a1d447d74 -# ================================= Tool Message ================================= -# Name: post_tweet - -# Successfully posted to Twitter: -# {"data": {"text": "hello, world! 2f11acf41ab2412cab56ea9a1d447d74", "id": "1857519455640891432", "edit_history_tweet_ids": ["1857519455640891432"]}} -# ================================== Ai Message ================================== -# The tweet "hello, world! 2f11acf41ab2412cab56ea9a1d447d74" has been successfully posted to Twitter. diff --git a/twitter-langchain/examples/post_tweet/pyproject.toml b/twitter-langchain/examples/post_tweet/pyproject.toml deleted file mode 100644 index f38800ecb..000000000 --- a/twitter-langchain/examples/post_tweet/pyproject.toml +++ /dev/null @@ -1,26 +0,0 @@ -[tool.poetry] -name = "chat" -version = "0.0.1" -description = "CDP Agentkit Langchain Chat Example" -authors = ["John Peterson "] -readme = "README.md" -license = "Apache-2.0" -keywords = ["coinbase", "sdk", "crypto", "cdp", "agentkit", "ai", "agent", "langchain", "toolkit"] -packages = [] - -[tool.poetry.dependencies] -cdp-sdk = "^0.10.3" -cdp-agentkit-core = {path = "../../../cdp-agentkit-core", develop = true} -cdp-langchain = {path = "../../../cdp-langchain", develop = true} -twitter-langchain = {path = "../../../twitter-langchain", develop = true} -langchain = "^0.3.4" -langchain-openai = "^0.2.4" -langgraph = "^0.2.39" -python = "^3.10" -tweepy = "^4.14.0" - -[tool.poetry.group.dev.dependencies] - -[build-system] -requires = ["poetry-core"] -build-backend = "poetry.core.masonry.api" From bacd1d2f45cbb97e59299e9326ecde8f363126a2 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 12:46:49 -0800 Subject: [PATCH 21/23] implementing Rohan's feedback --- twitter-langchain/examples/chatbot/.env-local | 12 ++++++------ twitter-langchain/examples/chatbot/README.md | 1 - twitter-langchain/examples/chatbot/chatbot.py | 2 -- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/twitter-langchain/examples/chatbot/.env-local b/twitter-langchain/examples/chatbot/.env-local index b6dd036d7..a810998e2 100644 --- a/twitter-langchain/examples/chatbot/.env-local +++ b/twitter-langchain/examples/chatbot/.env-local @@ -1,6 +1,6 @@ - OPENAI_API_KEY= - TWITTER_ACCESS_TOKEN= - TWITTER_ACCESS_TOKEN_SECRET= - TWITTER_API_KEY= - TWITTER_API_SECRET= - TWITTER_BEARER_TOKEN= +OPENAI_API_KEY= +TWITTER_ACCESS_TOKEN= +TWITTER_ACCESS_TOKEN_SECRET= +TWITTER_API_KEY= +TWITTER_API_SECRET= +TWITTER_BEARER_TOKEN= diff --git a/twitter-langchain/examples/chatbot/README.md b/twitter-langchain/examples/chatbot/README.md index d21cc3538..8c102a105 100644 --- a/twitter-langchain/examples/chatbot/README.md +++ b/twitter-langchain/examples/chatbot/README.md @@ -3,7 +3,6 @@ This example demonstrates an agent setup as a terminal style chatbot with access to Twitter (X) API actions. ## Ask the chatbot to engage in the Twitter (X) ecosystem! -- "Transfer a portion of your ETH to john2879.base.eth" - "What are my account details?" - "Please post a message for me to Twitter" - "Please get my mentions" diff --git a/twitter-langchain/examples/chatbot/chatbot.py b/twitter-langchain/examples/chatbot/chatbot.py index 8bcb6ae27..3220fa351 100644 --- a/twitter-langchain/examples/chatbot/chatbot.py +++ b/twitter-langchain/examples/chatbot/chatbot.py @@ -1,5 +1,3 @@ -import asyncio -import multiprocessing import os import sys import time From 03175d443d4076a6622686913a7bbd3dd7ee291f Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 13:04:59 -0800 Subject: [PATCH 22/23] updating changelog with latest unreleased twitter langchain actions --- cdp-agentkit-core/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cdp-agentkit-core/CHANGELOG.md b/cdp-agentkit-core/CHANGELOG.md index 3a5487c34..243ca7d5d 100644 --- a/cdp-agentkit-core/CHANGELOG.md +++ b/cdp-agentkit-core/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Added `account_mentions` action to `twitter-langchain` +- Added `post_tweet_reply` action to `twitter-langchain` + ## [0.0.4] - 2024-11-15 ### Added From 3da7ca1a8a3bc2f100abe6143db40b8da4a4ba44 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Fri, 15 Nov 2024 13:06:43 -0800 Subject: [PATCH 23/23] formatting --- cdp-agentkit-core/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cdp-agentkit-core/CHANGELOG.md b/cdp-agentkit-core/CHANGELOG.md index 243ca7d5d..e2070421f 100644 --- a/cdp-agentkit-core/CHANGELOG.md +++ b/cdp-agentkit-core/CHANGELOG.md @@ -2,8 +2,8 @@ ## Unreleased -- Added `account_mentions` action to `twitter-langchain` -- Added `post_tweet_reply` action to `twitter-langchain` +- Added `account_mentions` action to `twitter-langchain`. +- Added `post_tweet_reply` action to `twitter-langchain`. ## [0.0.4] - 2024-11-15