python3 -m venv venv
source venv/bin/activate
pip install galadriel
.env
in your project directory.
.env
file:
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
TWITTER_CONSUMER_API_KEY=<YOUR_TWITTER_CONSUMER_API_KEY>
TWITTER_CONSUMER_API_SECRET=<YOUR_TWITTER_CONSUMER_API_SECRET>
TWITTER_ACCESS_TOKEN=<YOUR_TWITTER_ACCESS_TOKEN>
TWITTER_ACCESS_TOKEN_SECRET=<YOUR_TWITTER_ACCESS_TOKEN_SECRET>
DRY_RUN=true
twitter_agent.py
with the following code:
import asyncio
import os
from pathlib import Path
from dotenv import load_dotenv
from galadriel import CodeAgent
from galadriel import AgentRuntime
from galadriel.clients import Cron
from galadriel.core_agent import LiteLLMModel
from galadriel.tools.twitter import TwitterPostTool
load_dotenv(dotenv_path=Path(".") / ".env", override=True)
llm_model = LiteLLMModel(model_id="gpt-4o", api_key=os.getenv("OPENAI_API_KEY"))
POST_INTERVAL_SECONDS = 3 * 60 * 60 # 3 hours
AGENT_PROMPT = """
You are a humorous Twitter user.
Every time you are called:
1. Generate a short tweet (1-2 sentences). About any topic.
2. Post the tweet.
"""
agent = CodeAgent(
prompt_template=AGENT_PROMPT,
model=llm_model,
tools=[TwitterPostTool()],
)
runtime = AgentRuntime(
agent=agent,
inputs=[Cron(POST_INTERVAL_SECONDS)],
outputs=[], # No output, posting happens inside Agent
)
asyncio.run(runtime.run())
LiteLLMModel
: Loads the OpenAI GPT-4o model.TwitterPostTool
: Enables posting tweets to Twitter.CodeAgent
: Creates the agent with the specified prompt and tools.Cron
: Triggers the agent to run every 3 hours.AgentRuntime
: Manages the execution of the agent.DRY_RUN=true
is set, it will print the generated tweetstwitter_agent.py
with the following code:
import asyncio
import os
from pathlib import Path
from dotenv import load_dotenv
from galadriel import CodeAgent
from galadriel import AgentRuntime
from galadriel.clients import Cron
from galadriel.clients.twitter_post_client import TwitterPostClient
from galadriel.core_agent import LiteLLMModel
load_dotenv(dotenv_path=Path(".") / ".env", override=True)
llm_model = LiteLLMModel(model_id="gpt-4o", api_key=os.getenv("OPENAI_API_KEY"))
POST_INTERVAL_SECONDS = 5
AGENT_PROMPT = """
You are a humorous Twitter user.
Generate a short tweet (1-2 sentences). About any topic.
"""
agent = CodeAgent(
prompt_template=AGENT_PROMPT,
model=llm_model,
tools=[],
)
runtime = AgentRuntime(
agent=agent,
inputs=[Cron(POST_INTERVAL_SECONDS)],
outputs=[TwitterPostClient()],
)
asyncio.run(runtime.run())