-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass implementing twitter account details action
- Loading branch information
Showing
6 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
14 changes: 8 additions & 6 deletions
14
cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py
This file contains 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,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 |
36 changes: 36 additions & 0 deletions
36
cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_details.py
This file contains 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,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 |
This file contains 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 account_details.py |
63 changes: 63 additions & 0 deletions
63
twitter-langchain/examples/account_details/account_details.py
This file contains 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,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]" | ||
# } |
This file contains 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
This file contains 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