Skip to content

Commit

Permalink
cdb agent kit
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryPan2718 committed Feb 2, 2025
1 parent 211b2f7 commit 1c54170
Show file tree
Hide file tree
Showing 3 changed files with 1,193 additions and 4 deletions.
72 changes: 72 additions & 0 deletions convex/agent/cdpAgentkit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { AgentKit, CdpWalletProvider, ActionProvider } from "@coinbase/agentkit";
import { cdpApiActionProvider, pythActionProvider } from "@coinbase/agentkit";
import { getLangChainTools } from "@coinbase/agentkit-langchain";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";

interface CdpAgentConfig {
cdpApiKeyName: string;
cdpApiKeyPrivate: string;
networkId?: string;
actionProviders?: ActionProvider<any>[];
}

export class CdpAgent {
private constructor(public agentKit: AgentKit) {}

static async from(config: CdpAgentConfig): Promise<CdpAgent> {
// Configure wallet provider
const walletProvider = await CdpWalletProvider.configureWithWallet({
apiKeyName: config.cdpApiKeyName,
apiKeyPrivateKey: config.cdpApiKeyPrivate,
networkId: config.networkId || "base-mainnet",
});

// Set up default action providers if none provided
const actionProviders = config.actionProviders || [
cdpApiActionProvider({
apiKeyName: config.cdpApiKeyName,
apiKeyPrivateKey: config.cdpApiKeyPrivate,
}),
pythActionProvider(),
];

const agentKit = await AgentKit.from({
walletProvider,
actionProviders,
});

return new CdpAgent(agentKit);
}

async getLangChainTools() {
return getLangChainTools(this.agentKit);
}

async createLangChainAgent() {
const tools = await this.getLangChainTools();
const llm = new ChatOpenAI({
model: "gpt-4o-mini",
});

return createReactAgent({
llm,
tools,
});
}
}

const agent = await CdpAgent.from({
cdpApiKeyName: "your-key-name",
cdpApiKeyPrivate: "your-private-key",
networkId: "base-mainnet",
});

const langChainAgent = await agent.createLangChainAgent();

const response = await langChainAgent.invoke({
input: "What is the current price of BTC?",
});

console.log(response);

Loading

0 comments on commit 1c54170

Please sign in to comment.