@truto/langchain-toolset 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # Truto's SuperAI Toolset for Langchain.js
2
+
3
+ A powerful toolset for integrating Truto's Proxy APIs with Langchain.js applications. This package provides all the Proxy APIs configured to be used as a tool on Truto as tools to Langchain.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @truto/langchain-toolset
9
+ # or
10
+ yarn add @truto/langchain-toolset
11
+ ```
12
+
13
+ ## Prerequisites
14
+
15
+ - Node.js 22.14.0 or higher
16
+ - Yarn 1.22.19 or higher
17
+ - Truto API credentials
18
+ - [Bun](https://bun.sh) (for running examples)
19
+ - OpenAI API key (only needed for running examples)
20
+
21
+ ## Environment Setup
22
+
23
+ Create a `.env` file in your project root with the following variables:
24
+
25
+ ```env
26
+ TRUTO_API_TOKEN=your_truto_api_token
27
+
28
+ #OPTIONAL
29
+ OPENAI_API_KEY=your_openai_api_key # if you are using OpenAI models
30
+ TRUTO_API_BASE_URL=your_truto_api_base_url
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ Here's a complete example showing how to use the toolset with Langchain.js, including handling multiple tool executions:
36
+
37
+ ```typescript
38
+ import { ChatOpenAI } from '@langchain/openai';
39
+ import { HumanMessage, SystemMessage } from '@langchain/core/messages';
40
+ import { isEmpty, values } from 'lodash-es';
41
+ import { ToolCall } from '@langchain/core/dist/messages/tool';
42
+ import { getTools } from '@truto/langchain-toolset';
43
+
44
+ // Initialize the LLM
45
+ const llm = new ChatOpenAI({
46
+ model: 'o3-mini',
47
+ });
48
+
49
+ const INTEGRATED_ACCOUNT_ID = 'your_account_id';
50
+
51
+ async function main() {
52
+ // Get all available tools for a customer connection
53
+ const tools = await getTools(INTEGRATED_ACCOUNT_ID, {
54
+ truto: {
55
+ baseUrl: process.env.TRUTO_API_BASE_URL,
56
+ token: process.env.TRUTO_API_TOKEN as string,
57
+ },
58
+ });
59
+
60
+ // Bind tools to the LLM
61
+ const llmWithTools = llm.bindTools(values(tools));
62
+
63
+ // Create your messages
64
+ const messages = [
65
+ new SystemMessage(
66
+ 'You are an assistant who uses the available tools to give the user an answer. Make sure you respect the arguments required for a tool call, use them to filter down the results wherever necessary. All the tools return a JSON string response, so parse the output correctly and use them in the arguments.'
67
+ ),
68
+ new HumanMessage('Your question here'),
69
+ ];
70
+
71
+ let toolCalls: ToolCall[] = [];
72
+ do {
73
+ // Execute any pending tool calls
74
+ for (const toolCall of toolCalls) {
75
+ const toolResponse = await tools[toolCall.name].invoke(toolCall);
76
+ console.log('\n===================\n');
77
+ console.log(
78
+ 'Called tool',
79
+ toolCall.name,
80
+ toolCall.args,
81
+ toolResponse.content
82
+ );
83
+ messages.push(toolResponse);
84
+ }
85
+
86
+ // Get the next AI response
87
+ const aiMessage = await llmWithTools.invoke(messages);
88
+ messages.push(aiMessage);
89
+
90
+ // Check if there are more tool calls to execute
91
+ if (isEmpty(aiMessage.tool_calls)) {
92
+ console.log('\n\n===================\n\n');
93
+ console.log(aiMessage.content);
94
+ break;
95
+ }
96
+ toolCalls = aiMessage.tool_calls || [];
97
+ } while (!isEmpty(toolCalls));
98
+ }
99
+
100
+ main();
101
+ ```
102
+
103
+ This example demonstrates:
104
+ - How to initialize the tools with Truto credentials
105
+ - How to bind tools to a Langchain.js LLM
106
+ - How to handle multiple tool calls in sequence
107
+ - How to process tool responses and continue the conversation
108
+ - How to properly handle the conversation flow until all tool calls are complete
109
+
110
+ ## Running Examples
111
+
112
+ The repository includes example code in the `examples` directory. To run the examples:
113
+
114
+ 1. First, install Bun if you haven't already:
115
+ - Visit [bun.sh](https://bun.sh) to install Bun for your platform
116
+ - Or use the following command:
117
+ ```bash
118
+ curl -fsSL https://bun.sh/install | bash
119
+ ```
120
+
121
+ 2. Install dependencies:
122
+ ```bash
123
+ bun install
124
+ ```
125
+
126
+ 3. Set up your environment variables in `.env` as described above.
127
+
128
+ 4. Run the example:
129
+ ```bash
130
+ bun run examples/index.ts
131
+ ```
132
+
133
+ The example demonstrates how to:
134
+ - Initialize the tools with Truto credentials
135
+ - Bind tools to a Langchain.js LLM
136
+ - Make tool calls and handle responses
137
+ - Process multi-turn conversations with tool usage
138
+
139
+ ## Development
140
+
141
+ To build the package locally:
142
+
143
+ ```bash
144
+ yarn build
145
+ ```
146
+
147
+ To run type checking:
148
+
149
+ ```bash
150
+ yarn check
151
+ ```
152
+
153
+ ## License
154
+
155
+ MIT
156
+
157
+ ## Contributing
158
+
159
+ Contributions are welcome! Please feel free to submit a Pull Request.
160
+