Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto chain agent -> memory enabled + enhanced frontend #21

Merged
merged 19 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion auto-chain-agent/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Thumbs.db
# Database
*.sqlite
*.sqlite3

summary-differences.json
summary-*.json
diffs/
# Test coverage
coverage
35 changes: 31 additions & 4 deletions auto-chain-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,40 @@ A blockchain interaction service that provides natural language processing capab
## Features

- Natural language processing for blockchain interactions
- Balance checking
- Transaction sending
- Transaction history retrieval
- Memory-enabled conversations with context retention
- Balance checking and transaction management
- Wallet management
- Transaction history tracking

## Available Tools

The agent supports the following blockchain operations:

- `get_balance`: Check the balance of a blockchain address
- `send_transaction`: Send tokens to another address
- `send_transaction`: Send tokens to another address (supports AI3 token transfers)
- `get_transaction_history`: Retrieve transaction history and account information

The service consists of three main components:

- **Agents**: Core blockchain interaction and natural language processing
- **Backend**: API service for handling requests
- **Frontend**: User interface for interacting with the agent

## Setup

### Prerequisites

- Node.js (v18 or higher)
- Yarn package manager
- SQLite3

### Environment Variables

Create `.env` files in the respective directories following the `.env.example` file.

### Running the Services

```bash
yarn install
yarn dev
```
3 changes: 2 additions & 1 deletion auto-chain-agent/agents/.env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
OPENAI_API_KEY=<your-openai-api-key>
ANTHROPIC_API_KEY=<your-anthropic-api-key>
TEST_MNEMONIC=<test-mnemonic>
MNEMONIC=<test-mnemonic>
Xm0onh marked this conversation as resolved.
Show resolved Hide resolved
AGENTS_PORT=3000
DSN_API_KEY=<your-dsn-api-key>
14 changes: 10 additions & 4 deletions auto-chain-agent/agents/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import dotenv from 'dotenv';
import path from 'path';

dotenv.config();

export const config = {
SUMMARY_FILE_PATH : path.join(process.cwd(), 'summary-differences.json'),
Xm0onh marked this conversation as resolved.
Show resolved Hide resolved
CHECK_INTERVAL: 20 * 1000,
SUMMARY_DIR: path.join(process.cwd(), 'diffs'),
DIFF_FILE_PREFIX: 'summary-diff',
LLM_MODEL: "gpt-4-turbo-preview",
Xm0onh marked this conversation as resolved.
Show resolved Hide resolved
TEMPERATURE: 0.5,
MNEMONIC: process.env.MNEMONIC || '//Alice',
NETWORK: 'taurus',
port: process.env.AGENTS_PORT || 3000,
anthropicApiKey: process.env.ANTHROPIC_API_KEY,
openaiApiKey: process.env.OPENAI_API_KEY,
environment: process.env.NODE_ENV || 'development',
dsnApiKey: process.env.DSN_API_KEY,
autoConsensus: {
apiKey: process.env.AUTO_CONSENSUS_API_KEY,
},
Xm0onh marked this conversation as resolved.
Show resolved Hide resolved
llmConfig: {
temperature: 0.4,
maxTokens: 1500
}
};
61 changes: 47 additions & 14 deletions auto-chain-agent/agents/src/services/chainAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { ToolNode } from "@langchain/langgraph/prebuilt";
import { blockchainTools } from './tools';
import { config } from "../config/index";
import logger from "../logger";
import { createThreadStorage } from "./threadStorage";
import { startWithHistory } from "./utils";
import { loadThreadSummary, startSummarySystem } from './thread/summarySystem';
import { createThreadStorage } from './thread/threadStorage';

// Define state schema for the graph
const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: (curr, prev) => [...curr, ...prev],
Expand All @@ -22,31 +23,47 @@ const StateAnnotation = Annotation.Root({
};
result?: string;
}>>({
reducer: (_, next) => next, // Only keep current interaction's tool calls
reducer: (_, next) => next,
default: () => [],
})
});

// Initialize core components
const model = new ChatOpenAI({
openAIApiKey: config.openaiApiKey,
modelName: "gpt-4o-mini",
temperature: 0.7,
modelName: config.LLM_MODEL,
temperature: config.TEMPERATURE,
}).bindTools(blockchainTools);

const threadStorage = createThreadStorage();
const checkpointer = new MemorySaver();
const toolNode = new ToolNode(blockchainTools);
let flag = true;
let startWithHistoryFlag = false;
Xm0onh marked this conversation as resolved.
Show resolved Hide resolved

// Define node functions
const agentNode = async (state: typeof StateAnnotation.State) => {
try {
const systemMessage = new SystemMessage({
content: `You are a friendly and helpful AI assistant.
- Engage naturally in conversation and remember details users share about themselves
- When blockchain operations are needed, you can check balances and perform transactions`
});

if (startWithHistoryFlag) {
await startWithHistory().then(async () => {
const prevMessages = (await loadThreadSummary())
.map((content: string) => new HumanMessage({ content }));
logger.info(`Previous messages: ${prevMessages}`);
state.messages = [...state.messages, ...prevMessages];
flag = false;
});
startWithHistoryFlag = false;
}
if (flag) {
const prevMessages = (await loadThreadSummary())
.map(content => new HumanMessage({ content }));
logger.info(`Previous messages: ${prevMessages}`);
state.messages = [...state.messages, ...prevMessages];
flag = false;
}
Xm0onh marked this conversation as resolved.
Show resolved Hide resolved
const messages = [systemMessage, ...state.messages];
const response = await model.invoke(messages);

Expand Down Expand Up @@ -108,7 +125,6 @@ const shouldContinue = (state: typeof StateAnnotation.State) => {
return 'agent';
};

// Create and initialize the graph
const createBlockchainGraph = async () => {
try {
return new StateGraph(StateAnnotation)
Expand All @@ -124,18 +140,17 @@ const createBlockchainGraph = async () => {
}
};

// Initialize graph
let agentGraph: Awaited<ReturnType<typeof createBlockchainGraph>>;
(async () => {
try {
agentGraph = await createBlockchainGraph();
logger.info('Blockchain agent initialized successfully');
await startSummarySystem();
logger.info('Blockchain agent and summary system initialized successfully');
} catch (error) {
logger.error('Failed to initialize blockchain agent:', error);
}
})();

// Export the agent interface
export const blockchainAgent = {
async handleMessage({ message, threadId }: { message: string; threadId?: string }) {
try {
Expand All @@ -145,14 +160,32 @@ export const blockchainAgent = {

const currentThreadId = threadId || `blockchain_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
const previousState = threadId ? await threadStorage.loadThread(threadId) : null;


let relevantContext: BaseMessage[] = [];
if (!previousState || previousState.messages.length <= 2) {
const summaries = await loadThreadSummary();
if (summaries.length > 0) {
relevantContext = [new SystemMessage({
content: `Previous conversations context: ${
summaries
.slice(0, 100)
.map(summary => summary.trim())
.join(' | ')
}`
})];
}
}
logger.info(`Relevant context: ${relevantContext}`);
const initialState = {
messages: previousState ? [
...previousState.messages,
new HumanMessage({ content: message })
] : [
...relevantContext,
new SystemMessage({
content: `You are a helpful AI assistant. You can engage in general conversation and also help with blockchain operations like checking balances and performing transactions.`
content: `
You are a helpful AI assistant.
You can engage in general conversation and also help with blockchain operations like checking balances and performing transactions.`
}),
new HumanMessage({ content: message })
]
Expand Down
30 changes: 30 additions & 0 deletions auto-chain-agent/agents/src/services/thread/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
import logger from '../../logger';

export const initializeDb = async (dbPath: string) => {
logger.info('Initializing SQLite database at:', dbPath);
const db = await open({
filename: dbPath,
driver: sqlite3.Database
});

await db.exec(`
CREATE TABLE IF NOT EXISTS threads (
thread_id TEXT PRIMARY KEY,
messages TEXT NOT NULL,
tool_calls TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS summary_uploads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
upload_id TEXT NOT NULL,
CID TEXT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);

return db;
};
3 changes: 3 additions & 0 deletions auto-chain-agent/agents/src/services/thread/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createThreadStorage } from './threadStorage';

export type ThreadStorage = ReturnType<typeof createThreadStorage>;
28 changes: 28 additions & 0 deletions auto-chain-agent/agents/src/services/thread/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BaseMessage, MessageContent } from '@langchain/core/messages';

export interface ThreadState {
messages: BaseMessage[];
toolCalls: Array<{
id: string;
type: string;
function: {
name: string;
arguments: string;
};
result?: string;
}>;
}

export interface SummaryDifference {
timestamp: string;
threadId: string;
previousSummary: string | MessageContent;
currentSummary: string | MessageContent;
difference: string | MessageContent;
previousCID?: string;
}

export interface SummaryState {
lastCheck: string;
differences: SummaryDifference[];
}
65 changes: 65 additions & 0 deletions auto-chain-agent/agents/src/services/thread/summaryState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from 'fs';
import logger from '../../logger';
import { SummaryState } from './interface';
import { saveDiffFile } from './utils';
import { config } from '../../config';

export const loadSummaryState = async (): Promise<SummaryState> => {
try {
if (fs.existsSync(config.SUMMARY_FILE_PATH)) {
const data = await fs.promises.readFile(config.SUMMARY_FILE_PATH, 'utf8');
return JSON.parse(data);
}
return {
lastCheck: new Date().toISOString(),
differences: []
};
} catch (error) {
logger.error('Error loading summary state:', error);
return {
lastCheck: new Date().toISOString(),
differences: []
};
}
};

export const saveSummaryState = async (state: SummaryState) => {
try {
let existingState: SummaryState | null = null;
if (fs.existsSync(config.SUMMARY_FILE_PATH)) {
const existingData = await fs.promises.readFile(config.SUMMARY_FILE_PATH, 'utf8');
existingState = JSON.parse(existingData);
}
Xm0onh marked this conversation as resolved.
Show resolved Hide resolved

// Save main summary file
await fs.promises.writeFile(
config.SUMMARY_FILE_PATH,
JSON.stringify(state, null, 2),
'utf8'
);

// Only create and upload diff file if there are actual changes
if (!existingState ||
JSON.stringify(existingState.differences) !== JSON.stringify(state.differences)) {

// Get new differences since last state
const newDifferences = existingState
? state.differences.filter(diff =>
!existingState.differences.some(
existingDiff => existingDiff.timestamp === diff.timestamp
)
)
: state.differences;

if (newDifferences.length > 0) {
await saveDiffFile(newDifferences);
}
} else {
logger.info('No changes detected in summary differences, skipping diff file creation');
}
} catch (error) {
logger.error('Error saving summary state:', error);
throw error;
}
};

Loading