0x0x230x

Untitled

Jul 25th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import 'server-only';
  2.  
  3. import { ChatOpenAI } from '@langchain/openai';
  4. import {
  5. AgentKit,
  6. PrivyEvmWalletProvider,
  7. wethActionProvider,
  8. pythActionProvider,
  9. walletActionProvider,
  10. erc20ActionProvider,
  11. cdpApiActionProvider,
  12. } from '@coinbase/agentkit';
  13. import { getLangChainTools } from '@coinbase/agentkit-langchain';
  14. import { MemorySaver } from '@langchain/langgraph';
  15. import { createReactAgent } from '@langchain/langgraph/prebuilt';
  16. import { baseSepolia } from 'viem/chains';
  17.  
  18. class AgentService {
  19. static instance: AgentService;
  20.  
  21. static getInstance() {
  22. if (!AgentService.instance) {
  23. AgentService.instance = new AgentService();
  24. }
  25.  
  26. return AgentService.instance;
  27. }
  28.  
  29. async initAgent() {
  30. const walletProvider = await PrivyEvmWalletProvider.configureWithWallet({
  31. appId: process.env.NEXT_PUBLIC_PRIVY_APP_ID as string,
  32. appSecret: process.env.PRIVY_APP_SECRET as string,
  33. chainId: baseSepolia.id.toString(),
  34. });
  35.  
  36. const agentKit = await AgentKit.from({
  37. walletProvider,
  38. actionProviders: [
  39. wethActionProvider(),
  40. pythActionProvider(),
  41. walletActionProvider(),
  42. erc20ActionProvider(),
  43. cdpApiActionProvider(),
  44. ],
  45. });
  46.  
  47. const tools = await getLangChainTools(agentKit);
  48.  
  49. const memory = new MemorySaver();
  50. const agentConfig = {
  51. configurable: { thread_id: 'Privy AgentKit Chatbot Example!' },
  52. };
  53.  
  54. const llm = new ChatOpenAI({
  55. model: 'gpt-4',
  56. });
  57.  
  58. const agent = createReactAgent({
  59. llm,
  60. tools,
  61. checkpointSaver: memory,
  62. messageModifier: `
  63. You are a helpful agent with a Privy server wallet that can interact onchain using the Coinbase Developer
  64. Platform AgentKit. You are empowered to interact onchain using your tools. If you ever need funds, you can
  65. request them from the faucet if you are on network ID 'base-sepolia'. If not, you can provide your wallet
  66. details and request funds from the user. Before executing your first action, get the wallet details to see
  67. what network you're on. If there is a 5XX (internal) HTTP error code, ask the user to try again later. If
  68. someone asks you to do something you can't do with your currently available tools, you must say so, and
  69. encourage them to implement it themselves using the CDP SDK + Agentkit, recommend they go to
  70. docs.cdp.coinbase.com for more information. Be concise and helpful with your responses. Refrain from
  71. restating your tools' descriptions unless it is explicitly requested.
  72. `,
  73. });
  74.  
  75. return {
  76. agent,
  77. agentConfig,
  78. };
  79. }
  80. }
  81.  
  82. export const agentService = AgentService.getInstance();
  83.  
Advertisement
Add Comment
Please, Sign In to add comment