-
Notifications
You must be signed in to change notification settings - Fork 591
feat: implementing action for twitter post tweet #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
629b643
wip: first pass implementing twitter post text action
stat 00463f3
second pass implementing post text to twitter
stat 5193a01
twiter langchain documentation and example
stat 54aac3f
reverting changes to chatbot
stat cd33606
refactoring post_text to post_tweet and implementing feedback
stat a1d0f1a
Update cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/pos…
stat 19eb8f9
implementing John's feedback
stat 5a2e876
reverting .gitignore, redundant addition
stat 551c52e
implementing John's feedback, removing unnecessairy package
stat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
9 changes: 9 additions & 0 deletions
9
cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from cdp_agentkit_core.actions.social.twitter.post_tweet import ( | ||
| POST_TWEET_PROMPT as POST_TWEET_PROMPT, | ||
| ) | ||
| from cdp_agentkit_core.actions.social.twitter.post_tweet import ( | ||
| PostTweetInput as PostTweetInput, | ||
| ) | ||
| from cdp_agentkit_core.actions.social.twitter.post_tweet import ( | ||
| post_tweet as post_tweet, | ||
| ) |
34 changes: 34 additions & 0 deletions
34
cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/post_tweet.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import tweepy | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| 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.""" | ||
|
|
||
| class PostTweetInput(BaseModel): | ||
| """Input argument schema for twitter post tweet action.""" | ||
|
|
||
| tweet: str = Field( | ||
| ..., | ||
| description="The text of the tweet to post to twitter. Tweets can be maximum 280 characters.", | ||
| ) | ||
|
|
||
| def post_tweet(client: tweepy.Client, tweet: str) -> str: | ||
| """Post tweet 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: | ||
| client.create_tweet(text=tweet) | ||
| message = f"Successfully posted to Twitter:\n{tweet}" | ||
| except tweepy.errors.TweepyException as e: | ||
| message = f"Error posting to Twitter:\n{tweet}\n{e}" | ||
|
|
||
| return message |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| .PHONY: format | ||
| format: | ||
| ruff format . | ||
|
|
||
| .PHONY: lint | ||
| lint: | ||
| ruff check . | ||
|
|
||
| .PHONY: lint-fix | ||
| lint-fix: | ||
| ruff check . --fix | ||
|
|
||
| .PHONY: docs | ||
| docs: | ||
| sphinx-apidoc -f -o ./docs ./twitter_langchain | ||
|
|
||
| .PHONY: local-docs | ||
| local-docs: docs | ||
| cd docs && make html && open ./_build/html/index.html | ||
|
|
||
| .PHONY: test | ||
| test: | ||
| pytest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,111 @@ | ||
| # Twitter (X) Langchain Toolkit | ||
| Twitter integration with Langchain to enable agentic workflows using the core primitives defined in `cdp-agentkit-core`. | ||
|
|
||
| This toolkit contains tools that enable an LLM agent to interact with [Twitter](https://developer.x.com/en/docs/x-api). The toolkit provides a wrapper around the Twitter (X) API, allowing agents to perform social operations like posting text. | ||
|
|
||
| ## Setup | ||
|
|
||
| ### Prerequisites | ||
| - Python 3.10 or higher | ||
| - [OpenAI API Key](https://platform.openai.com/api-keys) | ||
| - [Twitter (X) App Developer Keys](https://developer.x.com/en/portal/dashboard) | ||
|
|
||
| ### Installation | ||
|
|
||
| ```bash | ||
| pip install twitter-langchain | ||
| ``` | ||
|
|
||
| ### Environment Setup | ||
|
|
||
| Set the following environment variables: | ||
|
|
||
| ```bash | ||
| OPENAI_API_KEY=<your-openai-api-key> | ||
| TWITTER_API_KEY=<your-api-key> | ||
| TWITTER_API_SECRET=<your-api-secret> | ||
| TWITTER_ACCESS_TOKEN=<your-access-token> | ||
| TWITTER_ACCESS_TOKEN_SECRET=<your-access-token-secret> | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Setup | ||
|
|
||
| ```python | ||
| 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: | ||
| ```python | ||
| tools = twitter_toolkit.get_tools() | ||
| for tool in tools: | ||
| print(tool.name) | ||
| ``` | ||
|
|
||
| The toolkit provides the following tools: | ||
|
|
||
| 1. **post_tweet** - Post tweet to Twitter | ||
|
|
||
| ### Using with an Agent | ||
|
|
||
| ```python | ||
| import uuid | ||
|
|
||
| from langchain_openai import ChatOpenAI | ||
| from langchain_core.messages import HumanMessage | ||
| from langgraph.prebuilt import create_react_agent | ||
|
|
||
| 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() | ||
| ``` | ||
|
|
||
| Expected output: | ||
| ``` | ||
| ================================ Human Message ================================= | ||
| Please post 'hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa' to twitter | ||
| ================================== Ai Message ================================== | ||
| Tool Calls: | ||
| post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq) | ||
| Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq | ||
| Args: | ||
| tweet: hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa | ||
| ================================= Tool Message ================================= | ||
| Name: post_tweet | ||
| Successfully posted! | ||
| ================================== Ai Message ================================== | ||
| The message "hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa" has been successfully posted to Twitter! | ||
| ``` | ||
|
|
||
| ## Contributing | ||
| See [CONTRIBUTING.md](../CONTRIBUTING.md) for detailed setup instructions and contribution guidelines. | ||
|
|
||
| ## Documentation | ||
| For detailed documentation, please visit: | ||
| - [Agentkit-Core](https://coinbase.github.io/cdp-agentkit-core) | ||
| - [Agentkit-Langchain](https://coinbase.github.io/cdp-langchain) | ||
| - [Agentkit-Twitter-Langchain](https://coinbase.github.io/twitter-langchain) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ifneq (,$(wildcard ./.env)) | ||
| include .env | ||
| endif | ||
|
|
||
| export | ||
|
|
||
| .PHONY: run | ||
| run: | ||
| poetry install --with dev | ||
| poetry run python post_tweet.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import uuid | ||
|
|
||
| from langchain_openai import ChatOpenAI | ||
| from langchain_core.messages import HumanMessage | ||
| 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! c4b8e3744c2e4345be9e0622b4c0a8aa' to twitter | ||
| # ================================== Ai Message ================================== | ||
| # Tool Calls: | ||
| # post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq) | ||
| # Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq | ||
| # Args: | ||
| # text: hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa | ||
| # ================================= Tool Message ================================= | ||
| # Name: post_tweet | ||
| # Successfully posted! | ||
| # ================================== Ai Message ================================== | ||
| # The message "hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa" has been successfully posted to Twitter! |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.