Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'server-only';
- import { ChatOpenAI } from '@langchain/openai';
- import {
- AgentKit,
- PrivyEvmWalletProvider,
- wethActionProvider,
- pythActionProvider,
- walletActionProvider,
- erc20ActionProvider,
- cdpApiActionProvider,
- } from '@coinbase/agentkit';
- import { getLangChainTools } from '@coinbase/agentkit-langchain';
- import { MemorySaver } from '@langchain/langgraph';
- import { createReactAgent } from '@langchain/langgraph/prebuilt';
- import { baseSepolia } from 'viem/chains';
- class AgentService {
- static instance: AgentService;
- static getInstance() {
- if (!AgentService.instance) {
- AgentService.instance = new AgentService();
- }
- return AgentService.instance;
- }
- async initAgent() {
- const walletProvider = await PrivyEvmWalletProvider.configureWithWallet({
- appId: process.env.NEXT_PUBLIC_PRIVY_APP_ID as string,
- appSecret: process.env.PRIVY_APP_SECRET as string,
- chainId: baseSepolia.id.toString(),
- });
- const agentKit = await AgentKit.from({
- walletProvider,
- actionProviders: [
- wethActionProvider(),
- pythActionProvider(),
- walletActionProvider(),
- erc20ActionProvider(),
- cdpApiActionProvider(),
- ],
- });
- const tools = await getLangChainTools(agentKit);
- const memory = new MemorySaver();
- const agentConfig = {
- configurable: { thread_id: 'Privy AgentKit Chatbot Example!' },
- };
- const llm = new ChatOpenAI({
- model: 'gpt-4',
- });
- const agent = createReactAgent({
- llm,
- tools,
- checkpointSaver: memory,
- messageModifier: `
- You are a helpful agent with a Privy server wallet that can interact onchain using the Coinbase Developer
- Platform AgentKit. You are empowered to interact onchain using your tools. If you ever need funds, you can
- request them from the faucet if you are on network ID 'base-sepolia'. If not, you can provide your wallet
- details and request funds from the user. Before executing your first action, get the wallet details to see
- what network you're on. If there is a 5XX (internal) HTTP error code, ask the user to try again later. If
- someone asks you to do something you can't do with your currently available tools, you must say so, and
- encourage them to implement it themselves using the CDP SDK + Agentkit, recommend they go to
- docs.cdp.coinbase.com for more information. Be concise and helpful with your responses. Refrain from
- restating your tools' descriptions unless it is explicitly requested.
- `,
- });
- return {
- agent,
- agentConfig,
- };
- }
- }
- export const agentService = AgentService.getInstance();
Advertisement
Add Comment
Please, Sign In to add comment