@devries-cameron20 / composio Mirror · read-only / commits / b9accfd69
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 23, 03:00 UTC

Update langchain example

haxzie committed May 16, 2025 at 15:39 UTC b9accfd6942891f7ecf197da9540605f3f51143b
4 files changed +153 -71
examples/langchain/package.json
+5 -1
@@ -14,7 +14,11 @@
14 "packageManager": "pnpm@10.8.0",
15 "dependencies": {
16 "@composio/core": "workspace:*",
17 - "@composio/langchain": "0.1.0-rc.6",
17 + "@composio/langchain": "workspace:*",
18 + "@langchain/core": "^0.3.56",
19 + "@langchain/langgraph": "^0.2.72",
20 + "@langchain/openai": "^0.5.10",
21 + "dotenv": "^16.5.0",
22 "openai": "^4.94.0"
23 },
24 "devDependencies": {
examples/langchain/src/index.ts
+57 -5
@@ -1,16 +1,68 @@
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 { LangchainToolset } from '@composio/langchain';
3 -import process from 'process';
4 -
7 +// initiate composio
8 const composio = new Composio({
9 apiKey: process.env.COMPOSIO_API_KEY,
10 toolset: new LangchainToolset(),
11 });
12
10 -// direct tool access
11 -const tools = await composio.getTools();
13 +// fetch the tool
14 +const tool = await composio.tools.get('default', 'HACKERNEWS_GET_USER');
15 +
16 +// Define the tools for the agent to use
17 +const tools = [tool];
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
13 -console.log(tools);
38 +// Define the function that calls the model
39 +async function callModel(state: typeof MessagesAnnotation.State) {
40 + const response = await model.invoke(state.messages);
41
42 + // We return a list, because this will get added to the existing list
43 + return { messages: [response] };
44 +}
45
46 +// Define a new graph
47 +const workflow = new StateGraph(MessagesAnnotation)
48 + .addNode('agent', callModel)
49 + .addEdge('__start__', 'agent') // __start__ is a special name for the entrypoint
50 + .addNode('tools', toolNode)
51 + .addEdge('tools', 'agent')
52 + .addConditionalEdges('agent', shouldContinue);
53
54 +// Finally, we compile it into a LangChain Runnable.
55 +const app = workflow.compile();
56 +
57 +// Use the agent
58 +const finalState = await app.invoke({
59 + messages: [new HumanMessage('Find the details of the user `pg` on HackerNews')],
60 +});
61 +console.log(finalState.messages[finalState.messages.length - 1].content);
62 +
63 +const nextState = await app.invoke({
64 + // Including the messages from the previous run gives the LLM context.
65 + // This way it knows we're asking about the weather in NY
66 + messages: [...finalState.messages, new HumanMessage('what about haxzie')],
67 +});
68 +console.log(nextState.messages[nextState.messages.length - 1].content);
examples/openai/src/examples.ts deleted
-38
@@ -1,38 +0,0 @@
1 -/**
2 - * Basic Composio Tool Fetching Example
3 - *
4 - * This example demonstrates how to fetch a tool from Composio.
5 - * It retrieves the HACKERNEWS_GET_USER tool and logs its schema.
6 - */
7 -import { Composio } from '@composio/core';
8 -import dotenv from 'dotenv';
9 -
10 -dotenv.config();
11 -
12 -const composio = new Composio({
13 - apiKey: process.env.COMPOSIO_API_KEY,
14 -});
15 -
16 -/**
17 - * Main function to demonstrate fetching a tool from Composio
18 - */
19 -async function main() {
20 - try {
21 - console.log('Fetching HACKERNEWS_GET_USER tool...');
22 - const tool = await composio.getToolBySlug('default', 'HACKERNEWS_GET_USER');
23 -
24 - console.log('Tool details:');
25 - console.log(JSON.stringify(tool, null, 2));
26 -
27 - console.log('\nInput parameters:');
28 - if (tool.function.parameters.properties) {
29 - for (const [key, value] of Object.entries(tool.function.parameters.properties)) {
30 - console.log(`- ${key}: ${JSON.stringify(value)}`);
31 - }
32 - }
33 - } catch (error) {
34 - console.error('Error fetching tool:', error);
35 - }
36 -}
37 -
38 -main();
pnpm-lock.yaml
+91 -27
@@ -64,8 +64,20 @@ importers:
64 specifier: workspace:*
65 version: link:../../packages/core
66 '@composio/langchain':
67 - specifier: 0.1.0-rc.6
68 - version: 0.1.0-rc.6(encoding@0.1.13)(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))
67 + specifier: workspace:*
68 + version: link:../../packages/toolsets/langchain
69 + '@langchain/core':
70 + specifier: ^0.3.56
71 + version: 0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))
72 + '@langchain/langgraph':
73 + specifier: ^0.2.72
74 + version: 0.2.72(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))(react@19.1.0)(zod-to-json-schema@3.24.5(zod@3.24.4))
75 + '@langchain/openai':
76 + specifier: ^0.5.10
77 + version: 0.5.10(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))(encoding@0.1.13)
78 + dotenv:
79 + specifier: ^16.5.0
80 + version: 16.5.0
81 openai:
82 specifier: ^4.94.0
83 version: 4.98.0(encoding@0.1.13)(zod@3.24.4)
@@ -289,12 +301,6 @@ packages:
301 '@composio/client@0.1.0-alpha.9':
302 resolution: {integrity: sha512-uhXW5NxIY+j5IZ7WnzOLrVmOAhSCb1FdQbvDxPCcQqpE7d78uRHS9i/aK4Xn6UNIPv+pt1BT8unR0zEdKjG3Ng==}
303
292 - '@composio/core@0.1.0-rc.5':
293 - resolution: {integrity: sha512-kSkkTjxJGUVABbVThsMXiNiIPZy0qNku77lG3wgDcFMGps7OLrL1vTT0t19cCL3Jl4jwJQm5qROpgCBQveFCbA==}
294 -
295 - '@composio/langchain@0.1.0-rc.6':
296 - resolution: {integrity: sha512-nx/emBOfyltoK1TeGXdqLnwSvirZW3u+Bs0cOkwMnT+zE9FDeX1sfVgl91SrhLqSWQLcaa/mAb6MRQ9LxYv2CA==}
297 -
304 '@emnapi/core@1.4.3':
305 resolution: {integrity: sha512-4m62DuCE07lw01soJwPiBGC0nAww0Q+RY70VZ+n49yDIO13yyinhbWCeNnaob0lakDtWQzSdtNWzJeOJt2ma+g==}
306
@@ -549,6 +555,39 @@ packages:
555 resolution: {integrity: sha512-eF9MyInM9RLNisAygiCrzHnqzOnuzGWy4f1SAqAis+XIMhcA98WuZDNWxyX9pP3aKQGc47FAJ/9XWJwv5KiquA==}
556 engines: {node: '>=18'}
557
558 + '@langchain/langgraph-checkpoint@0.0.17':
559 + resolution: {integrity: sha512-6b3CuVVYx+7x0uWLG+7YXz9j2iBa+tn2AXvkLxzEvaAsLE6Sij++8PPbS2BZzC+S/FPJdWsz6I5bsrqL0BYrCA==}
560 + engines: {node: '>=18'}
561 + peerDependencies:
562 + '@langchain/core': '>=0.2.31 <0.4.0'
563 +
564 + '@langchain/langgraph-sdk@0.0.74':
565 + resolution: {integrity: sha512-IUN0m4BYkGWdviFd4EaWDcQgxNq8z+1LIwXajCSt9B+Cb/pz0ZNpIPdu5hAIsf6a0RWu5yRUhzL1L40t7vu3Zg==}
566 + peerDependencies:
567 + '@langchain/core': '>=0.2.31 <0.4.0'
568 + react: ^18 || ^19
569 + peerDependenciesMeta:
570 + '@langchain/core':
571 + optional: true
572 + react:
573 + optional: true
574 +
575 + '@langchain/langgraph@0.2.72':
576 + resolution: {integrity: sha512-2Rs79mLSx0Yxr/omiWOXBlaS+eywZ9KACe06pI6XkA3hT2hwqjMlXYMvbeD7mxZlKrPtLsQaHWvL9IO2VAa+lQ==}
577 + engines: {node: '>=18'}
578 + peerDependencies:
579 + '@langchain/core': '>=0.2.36 <0.3.0 || >=0.3.40 < 0.4.0'
580 + zod-to-json-schema: ^3.x
581 + peerDependenciesMeta:
582 + zod-to-json-schema:
583 + optional: true
584 +
585 + '@langchain/openai@0.5.10':
586 + resolution: {integrity: sha512-hBQIWjcVxGS7tgVvgBBmrZ5jSaJ8nu9g6V64/Tx6KGjkW7VdGmUvqCO+koiQCOZVL7PBJkHWAvDsbghPYXiZEA==}
587 + engines: {node: '>=18'}
588 + peerDependencies:
589 + '@langchain/core': '>=0.3.48 <0.4.0'
590 +
591 '@lerna/create@8.2.2':
592 resolution: {integrity: sha512-1yn1MvWn2Yz0SFgTTQnef2m1YedF7KwqLLVIOrGkgQrkVHzsveAIk1A1RcRa2yyUh+siKI1YcJ7lUZIEt+qQ3Q==}
593 engines: {node: '>=18.0.0'}
@@ -3569,6 +3608,10 @@ packages:
3608 resolution: {integrity: sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==}
3609 hasBin: true
3610
3611 + uuid@9.0.1:
3612 + resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==}
3613 + hasBin: true
3614 +
3615 validate-npm-package-license@3.0.4:
3616 resolution: {integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==}
3617
@@ -3750,25 +3793,6 @@ snapshots:
3793
3794 '@composio/client@0.1.0-alpha.9': {}
3795
3753 - '@composio/core@0.1.0-rc.5(encoding@0.1.13)':
3754 - dependencies:
3755 - '@composio/client': 0.1.0-alpha.9
3756 - openai: 4.98.0(encoding@0.1.13)(zod@3.24.4)
3757 - pusher-js: 8.4.0
3758 - zod: 3.24.4
3759 - transitivePeerDependencies:
3760 - - encoding
3761 - - ws
3762 -
3763 - '@composio/langchain@0.1.0-rc.6(encoding@0.1.13)(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))':
3764 - dependencies:
3765 - '@composio/core': 0.1.0-rc.5(encoding@0.1.13)
3766 - '@langchain/core': 0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))
3767 - transitivePeerDependencies:
3768 - - encoding
3769 - - openai
3770 - - ws
3771 -
3796 '@emnapi/core@1.4.3':
3797 dependencies:
3798 '@emnapi/wasi-threads': 1.0.2
@@ -3965,6 +3989,44 @@ snapshots:
3989 transitivePeerDependencies:
3990 - openai
3991
3992 + '@langchain/langgraph-checkpoint@0.0.17(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))':
3993 + dependencies:
3994 + '@langchain/core': 0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))
3995 + uuid: 10.0.0
3996 +
3997 + '@langchain/langgraph-sdk@0.0.74(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))(react@19.1.0)':
3998 + dependencies:
3999 + '@types/json-schema': 7.0.15
4000 + p-queue: 6.6.2
4001 + p-retry: 4.6.2
4002 + uuid: 9.0.1
4003 + optionalDependencies:
4004 + '@langchain/core': 0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))
4005 + react: 19.1.0
4006 +
4007 + '@langchain/langgraph@0.2.72(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))(react@19.1.0)(zod-to-json-schema@3.24.5(zod@3.24.4))':
4008 + dependencies:
4009 + '@langchain/core': 0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))
4010 + '@langchain/langgraph-checkpoint': 0.0.17(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))
4011 + '@langchain/langgraph-sdk': 0.0.74(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))(react@19.1.0)
4012 + uuid: 10.0.0
4013 + zod: 3.24.4
4014 + optionalDependencies:
4015 + zod-to-json-schema: 3.24.5(zod@3.24.4)
4016 + transitivePeerDependencies:
4017 + - react
4018 +
4019 + '@langchain/openai@0.5.10(@langchain/core@0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4)))(encoding@0.1.13)':
4020 + dependencies:
4021 + '@langchain/core': 0.3.56(openai@4.98.0(encoding@0.1.13)(zod@3.24.4))
4022 + js-tiktoken: 1.0.20
4023 + openai: 4.98.0(encoding@0.1.13)(zod@3.24.4)
4024 + zod: 3.24.4
4025 + zod-to-json-schema: 3.24.5(zod@3.24.4)
4026 + transitivePeerDependencies:
4027 + - encoding
4028 + - ws
4029 +
4030 '@lerna/create@8.2.2(encoding@0.1.13)(typescript@5.8.3)':
4031 dependencies:
4032 '@npmcli/arborist': 7.5.4
@@ -7289,6 +7351,8 @@ snapshots:
7351
7352 uuid@11.1.0: {}
7353
7354 + uuid@9.0.1: {}
7355 +
7356 validate-npm-package-license@3.0.4:
7357 dependencies:
7358 spdx-correct: 3.2.0