Skip to content

Commit

Permalink
second pass implementing the post text to twitter action
Browse files Browse the repository at this point in the history
  • Loading branch information
stat committed Nov 6, 2024
1 parent ad3e9b3 commit 18c1d6e
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 47 deletions.
9 changes: 0 additions & 9 deletions cdp-agentkit-core/cdp_agentkit_core/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@
uniswap_v3_create_pool,
)

from cdp_agentkit_core.actions.social.twitter.post_text import (
TWITTER_POST_TEXT_PROMPT,
TwitterPostTextInput,
twitter_post_text,
)

__all__ = [
"UNISWAP_V3_CREATE_POOL_PROMPT",
"UniswapV3CreatePoolInput",
Expand Down Expand Up @@ -86,7 +80,4 @@
"TRANSFER_PROMPT",
"TransferInput",
"transfer",
"TWITTER_POST_TEXT_PROMPT",
"TwitterPostTextInput",
"twitter_post_text"
]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from cdp_agentkit_core.actions.social.twitter.post_text import (
POST_TEXT_PROMPT,
PostTextInput,
post_text,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

from pydantic import BaseModel, Field

TWITTER_POST_TEXT_PROMPT = """
This tool will post a text on Twitter."""
POST_TEXT_PROMPT = """
This tool will post text on Twitter."""

class TwitterPostTextInput(BaseModel):
class PostTextInput(BaseModel):
"""Input argument schema for twitter post text actions."""

client: Any = Field(
...,
description="The tweepy client used to interface with the Twitter API",
)
# client: Any = Field(
# ...,
# description="The tweepy client used to interface with the Twitter API",
# )

text: str = Field(
...,
description="The text to post to twitter",
)

def twitter_post_text(client: tweepy.Client, text: str) -> str:
def post_text(client: tweepy.Client, text: str) -> str:
"""Post text to Twitter.
Args:
Expand All @@ -35,7 +35,7 @@ def twitter_post_text(client: tweepy.Client, text: str) -> str:
try:
client.create_tweet(text=text)
message = f"Successfully posted!"
except tweepy.error.TweepError as e:
except tweepy.errors.TweepyException as e:
message = f"Error posting: {e}"

return text
8 changes: 5 additions & 3 deletions cdp-langchain/examples/chatbot/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ def initialize_agent():
# Initialize
twitter_api = TwitterApiWrapper(**values)
twitter_toolkit = TwitterToolkit.from_twitter_api_wrapper(twitter_api)
twitter_tools = cdp_toolkit.get_tools()
twitter_tools = twitter_toolkit.get_tools()

tools.extend(twitter_tools)
# tools.extend(twitter_tools)
# tools = twitter_tools

# Store buffered conversation history in memory.
memory = MemorySaver()
Expand All @@ -61,7 +62,8 @@ def initialize_agent():
# Create ReAct Agent using the LLM and CDP Agentkit tools.
return create_react_agent(
llm,
tools=tools,
# tools=tools,
tools=twitter_tools,
checkpointer=memory,
state_modifier="You are a helpful agent that can interact onchain on the Base Layer 2 using the Coinbase Developer Platform Agentkit. You are empowered to interact onchain using your tools. If you ever need funds, you can request them from the faucet. You can also deploy your own ERC-20 tokens, NFTs, and interact with them. If someone asks you to do something you can't do, you can say so, and encourage them to implement it themselves using the CDP SDK + Agentkit, recommend they go to docs.cdp.coinbase.com for more informaton.",
), config
Expand Down
14 changes: 7 additions & 7 deletions cdp-langchain/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions twitter-langchain/Makefile
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 ./cdp_langchain

.PHONY: local-docs
local-docs: docs
cd docs && make html && open ./_build/html/index.html

.PHONY: test
test:
pytest
2 changes: 1 addition & 1 deletion twitter-langchain/twitter_langchain/twitter_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tool allows agents to interact with the Twitter API.
To use this tool, you must first set as environment variables:
# TODO: List ENV VARs required.
TWITTER_BEARER_TOKEN
"""

Expand Down
33 changes: 23 additions & 10 deletions twitter-langchain/twitter_langchain/twitter_api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

from langchain_core.utils import get_from_dict_or_env

# from cdp_agentkit_core.actions import (
# twitter_post_text,
# )
from cdp_agentkit_core.actions.social.twitter import (
post_text,
)

class TwitterApiWrapper(BaseModel):
"""Wrapper for Twitter API."""
Expand All @@ -23,7 +23,10 @@ class TwitterApiWrapper(BaseModel):
def validate_environment(cls, values: dict) -> Any:
"""Validate that Twitter access token, token secret, and tweepy exists in the environment."""

bearer_token = get_from_dict_or_env(values, "twitter_bearer_token", "TWITTER_BEARER_TOKEN")
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")

try:
import tweepy
Expand All @@ -32,8 +35,18 @@ def validate_environment(cls, values: dict) -> Any:
"Tweepy Twitter SDK is not installed. " "Please install it with `pip install tweepy`"
) from None

values["bearer_token"] = bearer_token
client = tweepy.Client(bearer_token)
client = tweepy.Client(
consumer_key=api_key,
consumer_secret=api_secret,
access_token=access_token,
access_token_secret=access_token_secret,
)

values["client"] = client
values["api_key"] = api_key
values["api_secret"] = api_secret
values["access_token"] = access_token
values["access_token_secret"] = access_token_secret

return values

Expand All @@ -47,12 +60,12 @@ def post_text_wrapper(self, text: str) -> str:
str: A text containing the result of the post text to twitter response.
"""
# return twitter_post_text(client=self.client, text=text)
return ""
return post_text(client=self.client, text=text)

def run(self, mode: str, **kwargs) -> str:
"""Run the action via the Twitter API."""
if mode == "post_text":
return self.post_text_wrapper()
return self.post_text_wrapper(**kwargs)
else:
raise ValueError("Invalid mode" + mode)
raise ValueError("Invalid mode: " + mode)

16 changes: 8 additions & 8 deletions twitter-langchain/twitter_langchain/twitter_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from langchain_core.tools import BaseTool
from langchain_core.tools.base import BaseToolkit

from cdp_agentkit_core.actions import (
TWITTER_POST_TEXT_PROMPT,
TwitterPostTextInput,
twitter_post_text,
from cdp_agentkit_core.actions.social.twitter import (
POST_TEXT_PROMPT,
PostTextInput,
post_text,
)
from twitter_langchain.twitter_api_wrapper import TwitterApiWrapper
from twitter_langchain.twitter_action import TwitterAction
Expand Down Expand Up @@ -52,7 +52,7 @@ class TwitterToolkit(BaseToolkit):
.. code-block:: none
# TODO: Add list of available tools.
twitter_post_text
Use within an agent:
.. code-block:: python
Expand Down Expand Up @@ -83,7 +83,7 @@ class TwitterToolkit(BaseToolkit):
Post a hello tweet to the world
==================================[1m Ai Message [0m==================================
Tool Calls:
post_tweet (call_iSYJVaM7uchfNHOMJoVPQsOi)
twitter_post_tweet (call_iSYJVaM7uchfNHOMJoVPQsOi)
Call ID: call_iSYJVaM7uchfNHOMJoVPQsOi
Args:
no_input: "hello world"
Expand Down Expand Up @@ -118,8 +118,8 @@ def from_twitter_api_wrapper(cls, twitter_api_wrapper: TwitterApiWrapper) -> "Tw
{
"mode": "post_text",
"name": "post_text",
"description": TWITTER_POST_TEXT_PROMPT,
"args_schema": TwitterPostTextInput,
"description": POST_TEXT_PROMPT,
"args_schema": PostTextInput,
},
]

Expand Down

0 comments on commit 18c1d6e

Please sign in to comment.