@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 71 lines 2.57 KB
Raw
1 import { ChatOpenAI } from '@langchain/openai';
2 import { HumanMessage, AIMessage } from '@langchain/core/messages';
3 import { ToolNode } from '@langchain/langgraph/prebuilt';
4 import { StateGraph, MessagesAnnotation } from '@langchain/langgraph';
5 import { Composio } from '@composio/core';
6 import { LangchainProvider } from '@composio/langchain';
7 // initiate composio
8 const composio = new Composio({
9 apiKey: process.env.COMPOSIO_API_KEY,
10 provider: new LangchainProvider(),
11 });
12
13 // fetch the tool
14 console.log(`🔄 Fetching the tool...`);
15 const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER');
16
17 // Define the tools for the agent to use
18 const toolNode = new ToolNode(tools);
19
20 // Create a model and give it access to the tools
21 const model = new ChatOpenAI({
22 model: 'gpt-4o-mini',
23 temperature: 0,
24 }).bindTools(tools);
25
26 // Define the function that determines whether to continue or not
27 function shouldContinue({ messages }: typeof MessagesAnnotation.State) {
28 const lastMessage = messages[messages.length - 1] as AIMessage;
29
30 // If the LLM makes a tool call, then we route to the "tools" node
31 if (lastMessage.tool_calls?.length) {
32 return 'tools';
33 }
34 // Otherwise, we stop (reply to the user) using the special "__end__" node
35 return '__end__';
36 }
37
38 // Define the function that calls the model
39 async function callModel(state: typeof MessagesAnnotation.State) {
40 console.log(`🔄 Calling the model...`);
41 const response = await model.invoke(state.messages);
42
43 // We return a list, because this will get added to the existing list
44 return { messages: [response] };
45 }
46
47 // Define a new graph
48 const workflow = new StateGraph(MessagesAnnotation)
49 .addNode('agent', callModel)
50 .addEdge('__start__', 'agent') // __start__ is a special name for the entrypoint
51 .addNode('tools', toolNode)
52 .addEdge('tools', 'agent')
53 .addConditionalEdges('agent', shouldContinue);
54
55 // Finally, we compile it into a LangChain Runnable.
56 const app = workflow.compile();
57
58 // Use the agent
59 const finalState = await app.invoke({
60 messages: [new HumanMessage('Find the details of the user `pg` on HackerNews')],
61 });
62 console.log(`✅ Message recieved from the model`);
63 console.log(finalState.messages[finalState.messages.length - 1].content);
64
65 const nextState = await app.invoke({
66 // Including the messages from the previous run gives the LLM context.
67 // This way it knows we're asking about the weather in NY
68 messages: [...finalState.messages, new HumanMessage('what about haxzie')],
69 });
70 console.log(`✅ Message recieved from the model`);
71 console.log(nextState.messages[nextState.messages.length - 1].content);