Skip to content

Commit

Permalink
improve config organization and add tweet data parameterization
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrank-summit committed Jan 1, 2025
1 parent 7154584 commit affc906
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 96 deletions.
73 changes: 30 additions & 43 deletions auto-agents-framework/.env.sample
Original file line number Diff line number Diff line change
@@ -1,51 +1,38 @@
# Twitter API Credentials
TWITTER_USERNAME=your_twitter_username
TWITTER_PASSWORD=your_twitter_password

# LLM Configuration
SMALL_LLM_MODEL=gpt-4o-mini
LARGE_LLM_MODEL=gpt-4o
OPENAI_API_KEY=your_openai_api_key

CHECK_INTERVAL_MINUTES=15

# Server Configuration
PORT=3001

# Environment
NODE_ENV=development

# Chroma Configuration
CHROMA_URL=http://localhost:8000
# Twitter Credentials
TWITTER_USERNAME=<twitter_username>
TWITTER_PASSWORD=<twitter_password>

# Twitter data fetch and post configuration
NUM_TIMELINE_TWEETS=10
NUM_FOLLOWING_RECENT_TWEETS=10
NUM_RANDOM_FOLLOWERS=5
MAX_MENTIONS=20
MAX_THREAD_LENGTH=20
MAX_MY_RECENT_TWEETS=10
POST_TWEETS=false
RESPONSE_INTERVAL_MINUTES=26
POST_INTERVAL_MINUTES=30

# AutoDrive Configuration
DSN_API_KEY=your_dsn_api_key
DSN_UPLOAD=true
DSN_ENCRYPTION_PASSWORD=

# CORS Configuration
CORS_ORIGINS=http://localhost:3000
AUTO_DRIVE_API_KEY=<auto_drive_api_key>
AUTO_DRIVE_ENCRYPTION_PASSWORD=<auto_drive_encryption_password>
AUTO_DRIVE_UPLOAD=false

# SC Configuration
RPC_URL=https://auto-evm.taurus.autonomys.xyz/ws
CONTRACT_ADDRESS=0xc5d3caed3e3600ff42b9f423d4cf281e92d7f008
PRIVATE_KEY=<Private Key>
WALLET_ADDRESS=your_wallet_address
RPC_URL=<rpc_url>
CONTRACT_ADDRESS=<contract_address>
PRIVATE_KEY=<private_key>

# Tweet search configuration
ACCOUNTS_PER_BATCH=5
MAX_SEARCH_TWEETS=20
MAX_TIMELINE_TWEETS=60
MAX_MENTIONS=5
MAX_THREAD_LENGTH=20
# BATCH CONFIG
ENGAGEMENT_BATCH_SIZE=15
# LLM Configuration
SMALL_LLM_MODEL=gpt-4o-mini
LARGE_LLM_MODEL=<large_llm_model>
OPENAI_API_KEY=<openai_api_key>

# RESPONSE CONFIG
RETRY_LIMIT=2
# SerpAPI Configuration
SERPAPI_API_KEY=<serpapi_api_key>

# POSTING TWEETS PERMISSION
POST_TWEETS=true
# Server Configuration
PORT=<port>

# TOP LEVEL TWEET CONFIG
TOP_LEVEL_TWEET_INTERVAL_HOURS=24
# Environment
NODE_ENV=<node_env>
13 changes: 8 additions & 5 deletions auto-agents-framework/src/agents/tools/fetchMentionsTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export const createFetchMentionsTool = (twitterApi: TwitterApi) =>
new DynamicStructuredTool({
name: 'fetch_mentions',
description: 'Fetch recent mentions',
schema: z.object({}),
func: async () => {
schema: z.object({ maxMentions: z.number(), sinceId: z.string().optional() }),
func: async ({ maxMentions, sinceId }: { maxMentions: number; sinceId?: string }) => {
try {
const recentMentions = await twitterApi.getMyUnrepliedToMentions(10);
const recentMentions = await twitterApi.getMyUnrepliedToMentions(maxMentions, sinceId);

return {
tweets: recentMentions,
Expand All @@ -28,15 +28,18 @@ export const createFetchMentionsTool = (twitterApi: TwitterApi) =>
},
});

export const invokeFetchMentionsTool = async (toolNode: ToolNode) => {
export const invokeFetchMentionsTool = async (
toolNode: ToolNode,
{ maxMentions, sinceId }: { maxMentions: number; sinceId?: string },
) => {
const toolResponse = await toolNode.invoke({
messages: [
new AIMessage({
content: '',
tool_calls: [
{
name: 'fetch_mentions',
args: {},
args: { maxMentions, sinceId },
id: 'fetch_mentions_call',
type: 'tool_call',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export const createFetchMyRecentTweetsTool = (twitterApi: TwitterApi) =>
new DynamicStructuredTool({
name: 'fetch_my_recent_tweets',
description: 'Fetch the agents recent tweets',
schema: z.object({}),
func: async () => {
schema: z.object({ maxMyRecentTweets: z.number() }),
func: async ({ maxMyRecentTweets }: { maxMyRecentTweets: number }) => {
try {
const myRecentTweets = await twitterApi.getMyRecentTweets(10);
const myRecentTweets = await twitterApi.getMyRecentTweets(maxMyRecentTweets);
const repliedToTweetIds = await twitterApi.getMyRepliedToIds();
logger.info('Fetch My Recent Tweets Tool - Result', {
tweets: myRecentTweets.length,
Expand All @@ -35,15 +35,18 @@ export const createFetchMyRecentTweetsTool = (twitterApi: TwitterApi) =>
},
});

export const invokeFetchMyRecentTweetsTool = async (toolNode: ToolNode) => {
export const invokeFetchMyRecentTweetsTool = async (
toolNode: ToolNode,
{ maxMyRecentTweets }: { maxMyRecentTweets: number },
) => {
const toolResponse = await toolNode.invoke({
messages: [
new AIMessage({
content: '',
tool_calls: [
{
name: 'fetch_my_recent_tweets',
args: {},
args: { maxMyRecentTweets },
id: 'fetch_my_recent_tweets_call',
type: 'tool_call',
},
Expand Down
43 changes: 37 additions & 6 deletions auto-agents-framework/src/agents/tools/fetchTimelineTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,29 @@ export const createFetchTimelineTool = (twitterApi: TwitterApi) =>
new DynamicStructuredTool({
name: 'fetch_timeline',
description: 'Fetch the agents timeline to get recent tweets',
schema: z.object({ processedIds: z.array(z.string()) }),
func: async ({ processedIds }: { processedIds: string[] }) => {
schema: z.object({
processedIds: z.array(z.string()),
numTimelineTweets: z.number(),
numFollowingRecentTweets: z.number(),
numRandomFollowers: z.number(),
}),
func: async ({
processedIds,
numTimelineTweets,
numFollowingRecentTweets,
numRandomFollowers,
}: {
processedIds: string[];
numTimelineTweets: number;
numFollowingRecentTweets: number;
numRandomFollowers: number;
}) => {
try {
const myTimelineTweets = await twitterApi.getMyTimeline(10, processedIds);
const followingRecents = await twitterApi.getFollowingRecentTweets(10, 10);
const myTimelineTweets = await twitterApi.getMyTimeline(numTimelineTweets, processedIds);
const followingRecents = await twitterApi.getFollowingRecentTweets(
numFollowingRecentTweets,
numRandomFollowers,
);
const tweets = new Set([...myTimelineTweets, ...followingRecents]);
const sortedTweets = Array.from(tweets).sort(
(a, b) => new Date(b.timeParsed!).getTime() - new Date(a.timeParsed!).getTime(),
Expand All @@ -33,15 +51,28 @@ export const createFetchTimelineTool = (twitterApi: TwitterApi) =>
},
});

export const invokeFetchTimelineTool = async (toolNode: ToolNode, processedIds: string[]) => {
export const invokeFetchTimelineTool = async (
toolNode: ToolNode,
{
processedIds,
numTimelineTweets,
numFollowingRecentTweets,
numRandomFollowers,
}: {
processedIds: string[];
numTimelineTweets: number;
numFollowingRecentTweets: number;
numRandomFollowers: number;
},
) => {
const toolResponse = await toolNode.invoke({
messages: [
new AIMessage({
content: '',
tool_calls: [
{
name: 'fetch_timeline',
args: { processedIds },
args: { processedIds, numTimelineTweets, numFollowingRecentTweets, numRandomFollowers },
id: 'fetch_timeline_call',
type: 'tool_call',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { config } from '../../../config/index.js';
import { wallet } from './agentWallet.js';
import { cidFromBlakeHash, cidToString } from '@autonomys/auto-dag-data';

const CONTRACT_ADDRESS = config.CONTRACT_ADDRESS as `0x${string}`;
const CONTRACT_ADDRESS = config.autoDriveConfig.CONTRACT_ADDRESS as `0x${string}`;

const contract = new ethers.Contract(CONTRACT_ADDRESS, MEMORY_ABI, wallet);

Expand Down
4 changes: 2 additions & 2 deletions auto-agents-framework/src/agents/tools/utils/agentWallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ethers } from 'ethers';
import { config } from '../../../config/index.js';

const provider = new ethers.JsonRpcProvider(config.RPC_URL);
const provider = new ethers.JsonRpcProvider(config.autoDriveConfig.RPC_URL);

export const wallet = new ethers.Wallet(config.PRIVATE_KEY as string, provider);
export const wallet = new ethers.Wallet(config.autoDriveConfig.PRIVATE_KEY as string, provider);

export async function signMessage(data: object): Promise<string> {
const message = JSON.stringify(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { convertMessageContentToTweets } from '../../../tools/utils/twitter.js';
import { invokeFetchTimelineTool } from '../../../tools/fetchTimelineTool.js';
import { invokeFetchMentionsTool } from '../../../tools/fetchMentionsTool.js';
import { invokeFetchMyRecentTweetsTool } from '../../../tools/fetchMyRecentTweetsTool.js';
import { config as globalConfig } from '../../../../config/index.js';

const { twitterConfig } = globalConfig;

const logger = createLogger('collect-data-node');

Expand All @@ -19,17 +22,26 @@ export const createCollectDataNode =
];
logger.info('Processed IDs:', { processedIds: processedIds.length });

const timelineToolResponse = await invokeFetchTimelineTool(config.toolNode, processedIds);
const timelineToolResponse = await invokeFetchTimelineTool(config.toolNode, {
processedIds,
numTimelineTweets: twitterConfig.NUM_TIMELINE_TWEETS,
numFollowingRecentTweets: twitterConfig.NUM_FOLLOWING_RECENT_TWEETS,
numRandomFollowers: twitterConfig.NUM_RANDOM_FOLLOWERS,
});
const timelineContent =
timelineToolResponse.messages[timelineToolResponse.messages.length - 1].content;
const timelineTweets = convertMessageContentToTweets(timelineContent);

const mentionsToolResponse = await invokeFetchMentionsTool(config.toolNode);
const mentionsToolResponse = await invokeFetchMentionsTool(config.toolNode, {
maxMentions: twitterConfig.MAX_MENTIONS,
});
const mentionsContent =
mentionsToolResponse.messages[mentionsToolResponse.messages.length - 1].content;
const mentionsTweets = convertMessageContentToTweets(mentionsContent);

const myRecentTweetsToolResponse = await invokeFetchMyRecentTweetsTool(config.toolNode);
const myRecentTweetsToolResponse = await invokeFetchMyRecentTweetsTool(config.toolNode, {
maxMyRecentTweets: twitterConfig.MAX_MY_RECENT_TWEETS,
});
const myRecentTweetsContent =
myRecentTweetsToolResponse.messages[myRecentTweetsToolResponse.messages.length - 1].content;
const myRecentTweets = convertMessageContentToTweets(myRecentTweetsContent);
Expand Down
42 changes: 13 additions & 29 deletions auto-agents-framework/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ const twitterConfig = {
USERNAME: process.env.TWITTER_USERNAME || '',
PASSWORD: process.env.TWITTER_PASSWORD || '',
COOKIES_PATH: process.env.TWITTER_COOKIES_PATH || 'cookies.json',
NUM_TIMELINE_TWEETS: Number(process.env.NUM_TIMELINE_TWEETS) || 10,
NUM_FOLLOWING_RECENT_TWEETS: Number(process.env.NUM_FOLLOWING_RECENT_TWEETS) || 10,
NUM_RANDOM_FOLLOWERS: Number(process.env.NUM_RANDOM_FOLLOWERS) || 5,
MAX_MENTIONS: Number(process.env.MAX_MENTIONS) || 5,
MAX_THREAD_LENGTH: Number(process.env.MAX_THREAD_LENGTH) || 20,
MAX_MY_RECENT_TWEETS: Number(process.env.MAX_MY_RECENT_TWEETS) || 10,
POST_TWEETS: process.env.POST_TWEETS === 'true',
RESPONSE_INTERVAL_MS: Number(process.env.RESPONSE_INTERVAL_MINUTES) * 60 * 1000 || 26 * 60 * 1000,
POST_INTERVAL_MS: Number(process.env.POST_INTERVAL_MINUTES) * 60 * 1000 || 30 * 60 * 1000,
};

const llmConfig = {
Expand All @@ -26,48 +34,24 @@ const autoDriveConfig = {
AUTO_DRIVE_API_KEY: process.env.AUTO_DRIVE_API_KEY,
AUTO_DRIVE_ENCRYPTION_PASSWORD: process.env.AUTO_DRIVE_ENCRYPTION_PASSWORD,
AUTO_DRIVE_UPLOAD: process.env.AUTO_DRIVE_UPLOAD === 'true',
// SC Configuration
RPC_URL: process.env.RPC_URL,
CONTRACT_ADDRESS: process.env.CONTRACT_ADDRESS,
PRIVATE_KEY: process.env.PRIVATE_KEY,
WALLET_ADDRESS: process.env.WALLET_ADDRESS,
};

export const config = {
twitterConfig,
llmConfig,
autoDriveConfig,

// Agent Configuration
CHECK_INTERVAL: (Number(process.env.CHECK_INTERVAL_MINUTES) || 30) * 60 * 1000,
MEMORY_DIR: path.join(__dirname, '../../data/memory'),

// Server Configuration
PORT: process.env.PORT || 3001,

// Environment
NODE_ENV: process.env.NODE_ENV || 'development',

// Chroma Configuration
CHROMA_DIR: path.join(__dirname, '../../data/chroma'),
CHROMA_URL: process.env.CHROMA_URL || 'http://localhost:8000',

// CORS Configuration
CORS_ORIGINS: process.env.CORS_ORIGINS,

// SC Configuration
RPC_URL: process.env.RPC_URL,
CONTRACT_ADDRESS: process.env.CONTRACT_ADDRESS,
PRIVATE_KEY: process.env.PRIVATE_KEY,
WALLET_ADDRESS: process.env.WALLET_ADDRESS,

// Tweet Search/Fetch Configuration
ACCOUNTS_PER_BATCH: Number(process.env.ACCOUNTS_PER_BATCH) || 10,
MAX_SEARCH_TWEETS: Number(process.env.MAX_SEARCH_TWEETS) || 20,
MAX_MENTIONS: Number(process.env.MAX_MENTIONS) || 5,
MAX_THREAD_LENGTH: Number(process.env.MAX_THREAD_LENGTH) || 20,

// BATCH CONFIG
ENGAGEMENT_BATCH_SIZE: process.env.ENGAGEMENT_BATCH_SIZE || 15,

// RESPONSE CONFIG
RETRY_LIMIT: process.env.RETRY_LIMIT || 2,

// TOP LEVEL TWEET CONFIG
TOP_LEVEL_TWEET_INTERVAL_MINUTES: Number(process.env.TOP_LEVEL_TWEET_INTERVAL_MINUTES) || 120,
};
4 changes: 2 additions & 2 deletions auto-agents-framework/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const startWorkflowPolling = async () => {
const main = async () => {
try {
await startWorkflowPolling();
setInterval(startWorkflowPolling, config.CHECK_INTERVAL);
setInterval(startWorkflowPolling, config.twitterConfig.RESPONSE_INTERVAL_MS);

logger.info('Application started successfully', {
checkInterval: config.CHECK_INTERVAL,
checkInterval: config.twitterConfig.RESPONSE_INTERVAL_MS,
port: config.PORT,
});
} catch (error) {
Expand Down

0 comments on commit affc906

Please sign in to comment.