Skip to content

Commit

Permalink
first pass implementing twitter account details action
Browse files Browse the repository at this point in the history
  • Loading branch information
stat committed Nov 7, 2024
1 parent 0846441 commit 77a2179
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from cdp_agentkit_core.actions.social.twitter.post_tweet import (
POST_TWEET_PROMPT as POST_TWEET_PROMPT,
from cdp_agentkit_core.actions.social.twitter.account_details import (
ACCOUNT_DETAILS_PROMPT as ACCOUNT_DETAILS_PROMPT,
)
from cdp_agentkit_core.actions.social.twitter.account_details import AccountDetailsInput as AccountDetailsInput
from cdp_agentkit_core.actions.social.twitter.account_details import account_details as account_details

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,
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from operator import itemgetter

import tweepy
from pydantic import BaseModel, Field

ACCOUNT_DETAILS_PROMPT = """
This tool will return account details for the authenticated user context."""

class AccountDetailsInput(BaseModel):
"""Input argument schema for twitter account details action."""

def account_details(client: tweepy.Client) -> str:
""".
Args:
Returns:
str: A message containing account details for the authenticated user context.
"""
message = ""

try:
response = client.get_me()
user = response.data
# user_id, user_name, username = itemgetter('id', 'name', 'username')

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}"""
except tweepy.errors.TweepyException as e:
message = f"Error retrieving authenticated user account details: {e}"

return message
10 changes: 10 additions & 0 deletions twitter-langchain/examples/account_details/Makefile
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 account_details.py
63 changes: 63 additions & 0 deletions twitter-langchain/examples/account_details/account_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 - get account details
events = agent_executor.stream(
{
"messages": [
HumanMessage(content=f"Please obtain my twitter account information"),
],
},
stream_mode="values",
)

for event in events:
event["messages"][-1].pretty_print()

# ================================ Human Message =================================
# What is my twitter account profile link?
# ================================== Ai Message ==================================
# Tool Calls:
# account_details (call_pYME8H1tHfdMakFZ1FTS0VBX)
# Call ID: call_pYME8H1tHfdMakFZ1FTS0VBX
# Args:
# ================================= Tool Message =================================
# Name: account_details

# Successfully retrieved authenticated user account details. Please present the following as json and not markdown:
# id: 1853889445319331840
# name: CDP AgentKit
# username: CDPAgentKit
# link: https://x.com/CDPAgentKit
# ================================== Ai Message ==================================
# {
# "id": "[REDACTED]",
# "name": "[REDACTED]",
# "username": "[REDACTED]",
# "link": "[REDACTED]"
# }
18 changes: 16 additions & 2 deletions twitter-langchain/twitter_langchain/twitter_api_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Util that calls Twitter API."""


from typing import Any

from langchain_core.utils import get_from_dict_or_env
from pydantic import BaseModel, model_validator

from cdp_agentkit_core.actions.social.twitter import (
account_details,
post_tweet,
)

Expand Down Expand Up @@ -48,6 +48,18 @@ def validate_environment(cls, values: dict) -> Any:

return values

def account_details_wrapper(self) -> str:
""".
Args:
Returns:
str: A message containing account details for the authenticated user context in JSON format.
"""

return account_details(client=self.client)

def post_tweet_wrapper(self, tweet: str) -> str:
"""Post tweet to Twitter.
Expand All @@ -64,7 +76,9 @@ def post_tweet_wrapper(self, tweet: str) -> str:

def run(self, mode: str, **kwargs) -> str:
"""Run the action via the Twitter API."""
if mode == "post_tweet":
if mode == "account_details":
return self.account_details_wrapper()
elif mode == "post_tweet":
return self.post_tweet_wrapper(**kwargs)
else:
raise ValueError("Invalid mode: " + mode)
Expand Down
9 changes: 9 additions & 0 deletions twitter-langchain/twitter_langchain/twitter_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from langchain_core.tools.base import BaseToolkit

from cdp_agentkit_core.actions.social.twitter import (
ACCOUNT_DETAILS_PROMPT,
AccountDetailsInput,
POST_TWEET_PROMPT,
PostTweetInput,
)
Expand Down Expand Up @@ -55,6 +57,7 @@ class TwitterToolkit(BaseToolkit):
.. code-block:: none
account_details
post_tweet
Use within an agent:
Expand Down Expand Up @@ -120,6 +123,12 @@ def from_twitter_api_wrapper(cls, twitter_api_wrapper: TwitterApiWrapper) -> "Tw
"""
actions: list[dict] = [
{
"mode": "account_details",
"name": "account_details",
"description": ACCOUNT_DETAILS_PROMPT,
"args_schema": AccountDetailsInput,
},
{
"mode": "post_tweet",
"name": "post_tweet",
Expand Down

0 comments on commit 77a2179

Please sign in to comment.