@typia/langchain 12.1.0-dev.20260325 → 13.0.0-dev.20260426
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/LICENSE +21 -21
- package/README.md +115 -115
- package/lib/_virtual/LangChainToolsRegistrar.mjs +4 -0
- package/lib/_virtual/LangChainToolsRegistrar.mjs.map +1 -0
- package/lib/_virtual/_commonjsHelpers.mjs +6 -0
- package/lib/_virtual/_commonjsHelpers.mjs.map +1 -0
- package/lib/_virtual/index.mjs +4 -0
- package/lib/_virtual/index.mjs.map +1 -0
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +5 -41
- package/lib/index.mjs.map +1 -1
- package/lib/index2.mjs +56 -0
- package/lib/index2.mjs.map +1 -0
- package/lib/internal/LangChainToolsRegistrar.js +16 -19
- package/lib/internal/LangChainToolsRegistrar.js.map +1 -1
- package/lib/internal/LangChainToolsRegistrar.mjs +5 -99
- package/lib/internal/LangChainToolsRegistrar.mjs.map +1 -1
- package/lib/internal/LangChainToolsRegistrar2.mjs +125 -0
- package/lib/internal/LangChainToolsRegistrar2.mjs.map +1 -0
- package/package.json +8 -9
- package/src/index.ts +64 -64
- package/src/internal/LangChainToolsRegistrar.ts +149 -149
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Jeongho Nam
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Jeongho Nam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
# `@typia/langchain`
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
[](https://github.com/samchon/typia/blob/master/LICENSE)
|
|
6
|
-
[](https://www.npmjs.com/package/typia)
|
|
7
|
-
[](https://www.npmjs.com/package/typia)
|
|
8
|
-
[](https://github.com/samchon/typia/actions?query=workflow%3Atest)
|
|
9
|
-
[](https://typia.io/docs/)
|
|
10
|
-
[](https://gurubase.io/g/typia)
|
|
11
|
-
[](https://discord.gg/E94XhzrUCZ)
|
|
12
|
-
|
|
13
|
-
[LangChain.js](https://github.com/langchain-ai/langchainjs) integration for [`typia`](https://github.com/samchon/typia).
|
|
14
|
-
|
|
15
|
-
Converts typia controllers to LangChain `DynamicStructuredTool[]` with automatic validation.
|
|
16
|
-
|
|
17
|
-
## Setup
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
npm install @typia/langchain @langchain/core
|
|
21
|
-
npm install typia
|
|
22
|
-
npx typia setup
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Usage
|
|
26
|
-
|
|
27
|
-
### From TypeScript class
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
import { ChainValues, Runnable } from "@langchain/core";
|
|
31
|
-
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
32
|
-
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
33
|
-
import { ChatOpenAI } from "@langchain/openai";
|
|
34
|
-
import { toLangChainTools } from "@typia/langchain";
|
|
35
|
-
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
|
|
36
|
-
import typia from "typia";
|
|
37
|
-
|
|
38
|
-
const tools: DynamicStructuredTool[] = toLangChainTools({
|
|
39
|
-
controllers: [
|
|
40
|
-
typia.llm.controller<Calculator>("Calculator", new Calculator()),
|
|
41
|
-
],
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const agent: Runnable = createToolCallingAgent({
|
|
45
|
-
llm: new ChatOpenAI({ model: "gpt-4o" }),
|
|
46
|
-
tools,
|
|
47
|
-
prompt: ChatPromptTemplate.fromMessages([
|
|
48
|
-
["system", "You are a helpful assistant."],
|
|
49
|
-
["human", "{input}"],
|
|
50
|
-
["placeholder", "{agent_scratchpad}"],
|
|
51
|
-
]),
|
|
52
|
-
});
|
|
53
|
-
const executor: AgentExecutor = new AgentExecutor({ agent, tools });
|
|
54
|
-
const result: ChainValues = await executor.invoke({ input: "What is 10 + 5?" });
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### From OpenAPI document
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
61
|
-
import { toLangChainTools } from "@typia/langchain";
|
|
62
|
-
import { HttpLlm } from "@typia/utils";
|
|
63
|
-
|
|
64
|
-
const tools: DynamicStructuredTool[] = toLangChainTools({
|
|
65
|
-
controllers: [
|
|
66
|
-
HttpLlm.controller({
|
|
67
|
-
name: "petStore",
|
|
68
|
-
document: yourOpenApiDocument,
|
|
69
|
-
connection: { host: "https://api.example.com" },
|
|
70
|
-
}),
|
|
71
|
-
],
|
|
72
|
-
});
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Structured Output
|
|
76
|
-
|
|
77
|
-
Use `typia.llm.parameters<T>()` with LangChain's `withStructuredOutput()` to generate structured output with validation:
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
import { ChatOpenAI } from "@langchain/openai";
|
|
81
|
-
import { dedent, LlmJson } from "@typia/utils";
|
|
82
|
-
import typia, { tags } from "typia";
|
|
83
|
-
|
|
84
|
-
interface IMember {
|
|
85
|
-
email: string & tags.Format<"email">;
|
|
86
|
-
name: string;
|
|
87
|
-
age: number & tags.Minimum<0> & tags.Maximum<100>;
|
|
88
|
-
hobbies: string[];
|
|
89
|
-
joined_at: string & tags.Format<"date">;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const model = new ChatOpenAI({ model: "gpt-4o" }).withStructuredOutput(
|
|
93
|
-
typia.llm.parameters<IMember>(),
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
const member: IMember = await model.invoke(dedent`
|
|
97
|
-
I am a new member of the community.
|
|
98
|
-
|
|
99
|
-
My name is John Doe, and I am 25 years old.
|
|
100
|
-
I like playing basketball and reading books,
|
|
101
|
-
and joined to this community at 2022-01-01.
|
|
102
|
-
`);
|
|
103
|
-
|
|
104
|
-
// Validate the result
|
|
105
|
-
const result = typia.validate<IMember>(member);
|
|
106
|
-
if (!result.success) {
|
|
107
|
-
console.error(LlmJson.stringify(result));
|
|
108
|
-
}
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
## Features
|
|
112
|
-
|
|
113
|
-
- No manual schema definition — generates everything from TypeScript types or OpenAPI
|
|
114
|
-
- Automatic argument validation with LLM-friendly error feedback
|
|
115
|
-
- Supports both class-based (`typia.llm.controller`) and HTTP-based (`HttpLlm.controller`) controllers
|
|
1
|
+
# `@typia/langchain`
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
[](https://github.com/samchon/typia/blob/master/LICENSE)
|
|
6
|
+
[](https://www.npmjs.com/package/typia)
|
|
7
|
+
[](https://www.npmjs.com/package/typia)
|
|
8
|
+
[](https://github.com/samchon/typia/actions?query=workflow%3Atest)
|
|
9
|
+
[](https://typia.io/docs/)
|
|
10
|
+
[](https://gurubase.io/g/typia)
|
|
11
|
+
[](https://discord.gg/E94XhzrUCZ)
|
|
12
|
+
|
|
13
|
+
[LangChain.js](https://github.com/langchain-ai/langchainjs) integration for [`typia`](https://github.com/samchon/typia).
|
|
14
|
+
|
|
15
|
+
Converts typia controllers to LangChain `DynamicStructuredTool[]` with automatic validation.
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @typia/langchain @langchain/core
|
|
21
|
+
npm install typia
|
|
22
|
+
npx typia setup
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### From TypeScript class
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { ChainValues, Runnable } from "@langchain/core";
|
|
31
|
+
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
32
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
33
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
34
|
+
import { toLangChainTools } from "@typia/langchain";
|
|
35
|
+
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
|
|
36
|
+
import typia from "typia";
|
|
37
|
+
|
|
38
|
+
const tools: DynamicStructuredTool[] = toLangChainTools({
|
|
39
|
+
controllers: [
|
|
40
|
+
typia.llm.controller<Calculator>("Calculator", new Calculator()),
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const agent: Runnable = createToolCallingAgent({
|
|
45
|
+
llm: new ChatOpenAI({ model: "gpt-4o" }),
|
|
46
|
+
tools,
|
|
47
|
+
prompt: ChatPromptTemplate.fromMessages([
|
|
48
|
+
["system", "You are a helpful assistant."],
|
|
49
|
+
["human", "{input}"],
|
|
50
|
+
["placeholder", "{agent_scratchpad}"],
|
|
51
|
+
]),
|
|
52
|
+
});
|
|
53
|
+
const executor: AgentExecutor = new AgentExecutor({ agent, tools });
|
|
54
|
+
const result: ChainValues = await executor.invoke({ input: "What is 10 + 5?" });
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### From OpenAPI document
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
61
|
+
import { toLangChainTools } from "@typia/langchain";
|
|
62
|
+
import { HttpLlm } from "@typia/utils";
|
|
63
|
+
|
|
64
|
+
const tools: DynamicStructuredTool[] = toLangChainTools({
|
|
65
|
+
controllers: [
|
|
66
|
+
HttpLlm.controller({
|
|
67
|
+
name: "petStore",
|
|
68
|
+
document: yourOpenApiDocument,
|
|
69
|
+
connection: { host: "https://api.example.com" },
|
|
70
|
+
}),
|
|
71
|
+
],
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Structured Output
|
|
76
|
+
|
|
77
|
+
Use `typia.llm.parameters<T>()` with LangChain's `withStructuredOutput()` to generate structured output with validation:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
81
|
+
import { dedent, LlmJson } from "@typia/utils";
|
|
82
|
+
import typia, { tags } from "typia";
|
|
83
|
+
|
|
84
|
+
interface IMember {
|
|
85
|
+
email: string & tags.Format<"email">;
|
|
86
|
+
name: string;
|
|
87
|
+
age: number & tags.Minimum<0> & tags.Maximum<100>;
|
|
88
|
+
hobbies: string[];
|
|
89
|
+
joined_at: string & tags.Format<"date">;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const model = new ChatOpenAI({ model: "gpt-4o" }).withStructuredOutput(
|
|
93
|
+
typia.llm.parameters<IMember>(),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const member: IMember = await model.invoke(dedent`
|
|
97
|
+
I am a new member of the community.
|
|
98
|
+
|
|
99
|
+
My name is John Doe, and I am 25 years old.
|
|
100
|
+
I like playing basketball and reading books,
|
|
101
|
+
and joined to this community at 2022-01-01.
|
|
102
|
+
`);
|
|
103
|
+
|
|
104
|
+
// Validate the result
|
|
105
|
+
const result = typia.validate<IMember>(member);
|
|
106
|
+
if (!result.success) {
|
|
107
|
+
console.error(LlmJson.stringify(result));
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Features
|
|
112
|
+
|
|
113
|
+
- No manual schema definition — generates everything from TypeScript types or OpenAPI
|
|
114
|
+
- Automatic argument validation with LLM-friendly error feedback
|
|
115
|
+
- Supports both class-based (`typia.llm.controller`) and HTTP-based (`HttpLlm.controller`) controllers
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LangChainToolsRegistrar.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_commonjsHelpers.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAGA,gFAA6E;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,0BAAiC,KAoBhC;IACC,OAAO,iDAAuB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChD,CAAC"}
|
package/lib/index.mjs
CHANGED
|
@@ -1,44 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getDefaultExportFromCjs } from './_virtual/_commonjsHelpers.mjs';
|
|
2
|
+
import { __require as requireLib } from './index2.mjs';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* Converts TypeScript class methods via `typia.llm.controller<Class>()` or
|
|
7
|
-
* OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
|
|
8
|
-
*
|
|
9
|
-
* Every tool call is validated by typia. If LLM provides invalid arguments,
|
|
10
|
-
* returns validation error formatted by {@link LlmJson.stringify} so that LLM
|
|
11
|
-
* can correct them automatically.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { initChatModel } from "langchain/chat_models/universal";
|
|
16
|
-
* import typia from "typia";
|
|
17
|
-
* import { toLangChainTools } from "@typia/langchain";
|
|
18
|
-
*
|
|
19
|
-
* class Calculator {
|
|
20
|
-
* add(input: { a: number; b: number }): { value: number } {
|
|
21
|
-
* return { value: input.a + input.b };
|
|
22
|
-
* }
|
|
23
|
-
* }
|
|
24
|
-
*
|
|
25
|
-
* const tools = toLangChainTools({
|
|
26
|
-
* controllers: [
|
|
27
|
-
* typia.llm.controller<Calculator>("calculator", new Calculator()),
|
|
28
|
-
* ],
|
|
29
|
-
* });
|
|
30
|
-
*
|
|
31
|
-
* const llm = await initChatModel();
|
|
32
|
-
* const modelWithTools = llm.bindTools(tools);
|
|
33
|
-
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
34
|
-
* ```;
|
|
35
|
-
*
|
|
36
|
-
* @param props Conversion properties
|
|
37
|
-
* @returns Array of LangChain DynamicStructuredTool
|
|
38
|
-
*/
|
|
39
|
-
function toLangChainTools(props) {
|
|
40
|
-
return LangChainToolsRegistrar.convert(props);
|
|
41
|
-
}
|
|
4
|
+
var libExports = requireLib();
|
|
5
|
+
var index = /*@__PURE__*/getDefaultExportFromCjs(libExports);
|
|
42
6
|
|
|
43
|
-
export {
|
|
7
|
+
export { index as default };
|
|
44
8
|
//# sourceMappingURL=index.mjs.map
|
package/lib/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["index.js?commonjs-entry"],"sourcesContent":["import { getDefaultExportFromCjs } from \"\u0000commonjsHelpers.js\";\nimport { __require as requireLib } from \"/home/samchon/github/samchon/typia@next/packages/langchain/lib/index.js\";\nvar libExports = requireLib();\nexport { libExports as __moduleExports };\nexport default /*@__PURE__*/getDefaultExportFromCjs(libExports);"],"names":[],"mappings":";;;AAEA,IAAI,UAAU,GAAG,UAAU,EAAE;AAE7B,YAAe,aAAa,uBAAuB,CAAC,UAAU,CAAC;;;;"}
|
package/lib/index2.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { __exports as lib } from './_virtual/index.mjs';
|
|
2
|
+
import { __require as requireLangChainToolsRegistrar } from './internal/LangChainToolsRegistrar2.mjs';
|
|
3
|
+
|
|
4
|
+
var hasRequiredLib;
|
|
5
|
+
|
|
6
|
+
function requireLib () {
|
|
7
|
+
if (hasRequiredLib) return lib;
|
|
8
|
+
hasRequiredLib = 1;
|
|
9
|
+
Object.defineProperty(lib, "__esModule", { value: true });
|
|
10
|
+
lib.toLangChainTools = toLangChainTools;
|
|
11
|
+
const LangChainToolsRegistrar_1 = requireLangChainToolsRegistrar();
|
|
12
|
+
/**
|
|
13
|
+
* Convert typia controllers to LangChain tools.
|
|
14
|
+
*
|
|
15
|
+
* Converts TypeScript class methods via `typia.llm.controller<Class>()` or
|
|
16
|
+
* OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
|
|
17
|
+
*
|
|
18
|
+
* Every tool call is validated by typia. If LLM provides invalid arguments,
|
|
19
|
+
* returns validation error formatted by {@link LlmJson.stringify} so that LLM
|
|
20
|
+
* can correct them automatically.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { initChatModel } from "langchain/chat_models/universal";
|
|
25
|
+
* import typia from "typia";
|
|
26
|
+
* import { toLangChainTools } from "@typia/langchain";
|
|
27
|
+
*
|
|
28
|
+
* class Calculator {
|
|
29
|
+
* add(input: { a: number; b: number }): { value: number } {
|
|
30
|
+
* return { value: input.a + input.b };
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* const tools = toLangChainTools({
|
|
35
|
+
* controllers: [
|
|
36
|
+
* typia.llm.controller<Calculator>("calculator", new Calculator()),
|
|
37
|
+
* ],
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* const llm = await initChatModel();
|
|
41
|
+
* const modelWithTools = llm.bindTools(tools);
|
|
42
|
+
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
43
|
+
* ```;
|
|
44
|
+
*
|
|
45
|
+
* @param props Conversion properties
|
|
46
|
+
* @returns Array of LangChain DynamicStructuredTool
|
|
47
|
+
*/
|
|
48
|
+
function toLangChainTools(props) {
|
|
49
|
+
return LangChainToolsRegistrar_1.LangChainToolsRegistrar.convert(props);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return lib;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { requireLib as __require };
|
|
56
|
+
//# sourceMappingURL=index2.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index2.mjs","sources":["index.js"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.toLangChainTools = toLangChainTools;\nconst LangChainToolsRegistrar_1 = require(\"./internal/LangChainToolsRegistrar\");\n/**\n * Convert typia controllers to LangChain tools.\n *\n * Converts TypeScript class methods via `typia.llm.controller<Class>()` or\n * OpenAPI operations via `HttpLlm.controller()` to LangChain tools.\n *\n * Every tool call is validated by typia. If LLM provides invalid arguments,\n * returns validation error formatted by {@link LlmJson.stringify} so that LLM\n * can correct them automatically.\n *\n * @example\n * ```typescript\n * import { initChatModel } from \"langchain/chat_models/universal\";\n * import typia from \"typia\";\n * import { toLangChainTools } from \"@typia/langchain\";\n *\n * class Calculator {\n * add(input: { a: number; b: number }): { value: number } {\n * return { value: input.a + input.b };\n * }\n * }\n *\n * const tools = toLangChainTools({\n * controllers: [\n * typia.llm.controller<Calculator>(\"calculator\", new Calculator()),\n * ],\n * });\n *\n * const llm = await initChatModel();\n * const modelWithTools = llm.bindTools(tools);\n * const result = await modelWithTools.invoke(\"What is 10 + 5?\");\n * ```;\n *\n * @param props Conversion properties\n * @returns Array of LangChain DynamicStructuredTool\n */\nfunction toLangChainTools(props) {\n return LangChainToolsRegistrar_1.LangChainToolsRegistrar.convert(props);\n}\n//# sourceMappingURL=index.js.map"],"names":["require$$0"],"mappings":";;;;;;;;AACA,CAAA,MAAM,CAAC,cAAc,CAAC,GAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,CAAA,GAAA,CAAA,gBAAwB,GAAG,gBAAgB;CAC3C,MAAM,yBAAyB,GAAGA,8BAAA,EAA6C;AAC/E;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;CACA,SAAS,gBAAgB,CAAC,KAAK,EAAE;KAC7B,OAAO,yBAAyB,CAAC,uBAAuB,CAAC,OAAO,CAAC,KAAK,CAAC;AAC3E,CAAA;AACA;;;;;;"}
|
|
@@ -92,24 +92,21 @@ var LangChainToolsRegistrar;
|
|
|
92
92
|
}));
|
|
93
93
|
}
|
|
94
94
|
};
|
|
95
|
-
const createTool = (entry) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}),
|
|
112
|
-
});
|
|
113
|
-
};
|
|
95
|
+
const createTool = (entry) => { var _a; return new tools_1.DynamicStructuredTool({
|
|
96
|
+
name: entry.name,
|
|
97
|
+
description: (_a = entry.function.description) !== null && _a !== void 0 ? _a : "",
|
|
98
|
+
schema: entry.function.parameters,
|
|
99
|
+
func: (args) => __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
const coerced = utils_1.LlmJson.coerce(args, entry.function.parameters);
|
|
101
|
+
const valid = entry.function.validate(coerced);
|
|
102
|
+
if (valid.success === false)
|
|
103
|
+
throw new tools_1.ToolInputParsingException(`Type errors in "${entry.name}" arguments:\n\n` +
|
|
104
|
+
`\`\`\`json\n${utils_1.LlmJson.stringify(valid)}\n\`\`\``, JSON.stringify(coerced));
|
|
105
|
+
const result = yield entry.execute(valid.data);
|
|
106
|
+
return result === undefined
|
|
107
|
+
? { success: true }
|
|
108
|
+
: { success: true, data: result };
|
|
109
|
+
}),
|
|
110
|
+
}); };
|
|
114
111
|
})(LangChainToolsRegistrar || (exports.LangChainToolsRegistrar = LangChainToolsRegistrar = {}));
|
|
115
112
|
//# sourceMappingURL=LangChainToolsRegistrar.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LangChainToolsRegistrar.js","sourceRoot":"","sources":["../../src/internal/LangChainToolsRegistrar.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAG+B;AAQ/B,wCAAgD;AAEhD,IAAiB,uBAAuB,CAuIvC;AAvID,WAAiB,uBAAuB;IACzB,+BAAO,GAAG,CAAC,KAGvB,EAA2B,EAAE;;QAC5B,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"LangChainToolsRegistrar.js","sourceRoot":"","sources":["../../src/internal/LangChainToolsRegistrar.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAG+B;AAQ/B,wCAAgD;AAEhD,IAAiB,uBAAuB,CAuIvC;AAvID,WAAiB,uBAAuB;IACzB,+BAAO,GAAG,CAAC,KAGvB,EAA2B,EAAE;;QAC5B,MAAM,MAAM,SAAY,KAAK,CAAC,MAAM,mCAAI,KAAK,CAAC;QAC9C,MAAM,KAAK,GAA4B,EAAE,CAAC;QAE1C,6BAA6B;QAC7B,IAAI,MAAM,KAAK,KAAK,IAAI,KAAK,CAAC,WAAW,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;YACtD,MAAM,KAAK,GAAwB,IAAI,GAAG,EAAE,CAAC;YAC7C,MAAM,UAAU,GAAa,EAAE,CAAC;YAChC,KAAK,MAAM,UAAU,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;gBAC3C,KAAK,MAAM,IAAI,IAAI,UAAU,CAAC,WAAW,CAAC,SAAS,E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
|
|
@@ -1,102 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { getDefaultExportFromCjs } from '../_virtual/_commonjsHelpers.mjs';
|
|
2
|
+
import { __require as requireLangChainToolsRegistrar } from './LangChainToolsRegistrar2.mjs';
|
|
3
3
|
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
LangChainToolsRegistrar.convert = (props) => {
|
|
7
|
-
const prefix = props.prefix ?? false;
|
|
8
|
-
const tools = [];
|
|
9
|
-
// check duplicate tool names
|
|
10
|
-
if (prefix === false && props.controllers.length >= 2) {
|
|
11
|
-
const names = new Map();
|
|
12
|
-
const duplicates = [];
|
|
13
|
-
for (const controller of props.controllers) {
|
|
14
|
-
for (const func of controller.application.functions) {
|
|
15
|
-
const existing = names.get(func.name);
|
|
16
|
-
if (existing !== undefined)
|
|
17
|
-
duplicates.push(`"${func.name}" in "${controller.name}" (conflicts with "${existing}")`);
|
|
18
|
-
else
|
|
19
|
-
names.set(func.name, controller.name);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
if (duplicates.length > 0)
|
|
23
|
-
throw new Error(`Duplicate tool names found:\n - ${duplicates.join("\n - ")}`);
|
|
24
|
-
}
|
|
25
|
-
// convert controllers to tools
|
|
26
|
-
for (const controller of props.controllers) {
|
|
27
|
-
if (controller.protocol === "class") {
|
|
28
|
-
convertClassController(tools, controller, prefix);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
convertHttpController(tools, controller, prefix);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return tools;
|
|
35
|
-
};
|
|
36
|
-
const convertClassController = (tools, controller, prefix) => {
|
|
37
|
-
const execute = controller.execute;
|
|
38
|
-
for (const func of controller.application.functions) {
|
|
39
|
-
const toolName = prefix
|
|
40
|
-
? `${controller.name}_${func.name}`
|
|
41
|
-
: func.name;
|
|
42
|
-
const method = execute[func.name];
|
|
43
|
-
if (typeof method !== "function") {
|
|
44
|
-
throw new Error(`Method "${func.name}" not found on controller "${controller.name}"`);
|
|
45
|
-
}
|
|
46
|
-
tools.push(createTool({
|
|
47
|
-
name: toolName,
|
|
48
|
-
function: func,
|
|
49
|
-
execute: async (args) => method.call(execute, args),
|
|
50
|
-
}));
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const convertHttpController = (tools, controller, prefix) => {
|
|
54
|
-
const application = controller.application;
|
|
55
|
-
const connection = controller.connection;
|
|
56
|
-
for (const func of application.functions) {
|
|
57
|
-
const toolName = prefix
|
|
58
|
-
? `${controller.name}_${func.name}`
|
|
59
|
-
: func.name;
|
|
60
|
-
tools.push(createTool({
|
|
61
|
-
name: toolName,
|
|
62
|
-
function: func,
|
|
63
|
-
execute: async (args) => {
|
|
64
|
-
if (controller.execute !== undefined) {
|
|
65
|
-
const response = await controller.execute({
|
|
66
|
-
connection,
|
|
67
|
-
application,
|
|
68
|
-
function: func,
|
|
69
|
-
arguments: args,
|
|
70
|
-
});
|
|
71
|
-
return response.body;
|
|
72
|
-
}
|
|
73
|
-
return HttpLlm.execute({
|
|
74
|
-
application,
|
|
75
|
-
function: func,
|
|
76
|
-
connection,
|
|
77
|
-
input: args,
|
|
78
|
-
});
|
|
79
|
-
},
|
|
80
|
-
}));
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
const createTool = (entry) => new DynamicStructuredTool({
|
|
84
|
-
name: entry.name,
|
|
85
|
-
description: entry.function.description ?? "",
|
|
86
|
-
schema: entry.function.parameters,
|
|
87
|
-
func: async (args) => {
|
|
88
|
-
const coerced = LlmJson.coerce(args, entry.function.parameters);
|
|
89
|
-
const valid = entry.function.validate(coerced);
|
|
90
|
-
if (valid.success === false)
|
|
91
|
-
throw new ToolInputParsingException(`Type errors in "${entry.name}" arguments:\n\n` +
|
|
92
|
-
`\`\`\`json\n${LlmJson.stringify(valid)}\n\`\`\``, JSON.stringify(coerced));
|
|
93
|
-
const result = await entry.execute(valid.data);
|
|
94
|
-
return result === undefined
|
|
95
|
-
? { success: true }
|
|
96
|
-
: { success: true, data: result };
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
})(LangChainToolsRegistrar || (LangChainToolsRegistrar = {}));
|
|
4
|
+
var LangChainToolsRegistrarExports = requireLangChainToolsRegistrar();
|
|
5
|
+
var LangChainToolsRegistrar = /*@__PURE__*/getDefaultExportFromCjs(LangChainToolsRegistrarExports);
|
|
100
6
|
|
|
101
|
-
export { LangChainToolsRegistrar };
|
|
7
|
+
export { LangChainToolsRegistrar as default };
|
|
102
8
|
//# sourceMappingURL=LangChainToolsRegistrar.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LangChainToolsRegistrar.mjs","sources":["
|
|
1
|
+
{"version":3,"file":"LangChainToolsRegistrar.mjs","sources":["LangChainToolsRegistrar.js?commonjs-entry"],"sourcesContent":["import { getDefaultExportFromCjs } from \"\u0000commonjsHelpers.js\";\nimport { __require as requireLangChainToolsRegistrar } from \"/home/samchon/github/samchon/typia@next/packages/langchain/lib/internal/LangChainToolsRegistrar.js\";\nvar LangChainToolsRegistrarExports = requireLangChainToolsRegistrar();\nexport { LangChainToolsRegistrarExports as __moduleExports };\nexport default /*@__PURE__*/getDefaultExportFromCjs(LangChainToolsRegistrarExports);"],"names":[],"mappings":";;;AAEA,IAAI,8BAA8B,GAAG,8BAA8B,EAAE;AAErE,8BAAe,aAAa,uBAAuB,CAAC,8BAA8B,CAAC;;;;"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { __exports as LangChainToolsRegistrar } from '../_virtual/LangChainToolsRegistrar.mjs';
|
|
2
|
+
import require$$0 from '@langchain/core/tools';
|
|
3
|
+
import require$$1 from '@typia/utils';
|
|
4
|
+
|
|
5
|
+
var hasRequiredLangChainToolsRegistrar;
|
|
6
|
+
|
|
7
|
+
function requireLangChainToolsRegistrar () {
|
|
8
|
+
if (hasRequiredLangChainToolsRegistrar) return LangChainToolsRegistrar;
|
|
9
|
+
hasRequiredLangChainToolsRegistrar = 1;
|
|
10
|
+
var __awaiter = (LangChainToolsRegistrar && LangChainToolsRegistrar.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(LangChainToolsRegistrar, "__esModule", { value: true });
|
|
20
|
+
LangChainToolsRegistrar.LangChainToolsRegistrar = void 0;
|
|
21
|
+
const tools_1 = require$$0;
|
|
22
|
+
const utils_1 = require$$1;
|
|
23
|
+
var LangChainToolsRegistrar$1;
|
|
24
|
+
(function (LangChainToolsRegistrar) {
|
|
25
|
+
LangChainToolsRegistrar.convert = (props) => {
|
|
26
|
+
var _a;
|
|
27
|
+
const prefix = (_a = props.prefix) !== null && _a !== void 0 ? _a : false;
|
|
28
|
+
const tools = [];
|
|
29
|
+
// check duplicate tool names
|
|
30
|
+
if (prefix === false && props.controllers.length >= 2) {
|
|
31
|
+
const names = new Map();
|
|
32
|
+
const duplicates = [];
|
|
33
|
+
for (const controller of props.controllers) {
|
|
34
|
+
for (const func of controller.application.functions) {
|
|
35
|
+
const existing = names.get(func.name);
|
|
36
|
+
if (existing !== undefined)
|
|
37
|
+
duplicates.push(`"${func.name}" in "${controller.name}" (conflicts with "${existing}")`);
|
|
38
|
+
else
|
|
39
|
+
names.set(func.name, controller.name);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (duplicates.length > 0)
|
|
43
|
+
throw new Error(`Duplicate tool names found:\n - ${duplicates.join("\n - ")}`);
|
|
44
|
+
}
|
|
45
|
+
// convert controllers to tools
|
|
46
|
+
for (const controller of props.controllers) {
|
|
47
|
+
if (controller.protocol === "class") {
|
|
48
|
+
convertClassController(tools, controller, prefix);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
convertHttpController(tools, controller, prefix);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return tools;
|
|
55
|
+
};
|
|
56
|
+
const convertClassController = (tools, controller, prefix) => {
|
|
57
|
+
const execute = controller.execute;
|
|
58
|
+
for (const func of controller.application.functions) {
|
|
59
|
+
const toolName = prefix
|
|
60
|
+
? `${controller.name}_${func.name}`
|
|
61
|
+
: func.name;
|
|
62
|
+
const method = execute[func.name];
|
|
63
|
+
if (typeof method !== "function") {
|
|
64
|
+
throw new Error(`Method "${func.name}" not found on controller "${controller.name}"`);
|
|
65
|
+
}
|
|
66
|
+
tools.push(createTool({
|
|
67
|
+
name: toolName,
|
|
68
|
+
function: func,
|
|
69
|
+
execute: (args) => __awaiter(this, void 0, void 0, function* () { return method.call(execute, args); }),
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const convertHttpController = (tools, controller, prefix) => {
|
|
74
|
+
const application = controller.application;
|
|
75
|
+
const connection = controller.connection;
|
|
76
|
+
for (const func of application.functions) {
|
|
77
|
+
const toolName = prefix
|
|
78
|
+
? `${controller.name}_${func.name}`
|
|
79
|
+
: func.name;
|
|
80
|
+
tools.push(createTool({
|
|
81
|
+
name: toolName,
|
|
82
|
+
function: func,
|
|
83
|
+
execute: (args) => __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
if (controller.execute !== undefined) {
|
|
85
|
+
const response = yield controller.execute({
|
|
86
|
+
connection,
|
|
87
|
+
application,
|
|
88
|
+
function: func,
|
|
89
|
+
arguments: args,
|
|
90
|
+
});
|
|
91
|
+
return response.body;
|
|
92
|
+
}
|
|
93
|
+
return utils_1.HttpLlm.execute({
|
|
94
|
+
application,
|
|
95
|
+
function: func,
|
|
96
|
+
connection,
|
|
97
|
+
input: args,
|
|
98
|
+
});
|
|
99
|
+
}),
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const createTool = (entry) => { var _a; return new tools_1.DynamicStructuredTool({
|
|
104
|
+
name: entry.name,
|
|
105
|
+
description: (_a = entry.function.description) !== null && _a !== void 0 ? _a : "",
|
|
106
|
+
schema: entry.function.parameters,
|
|
107
|
+
func: (args) => __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const coerced = utils_1.LlmJson.coerce(args, entry.function.parameters);
|
|
109
|
+
const valid = entry.function.validate(coerced);
|
|
110
|
+
if (valid.success === false)
|
|
111
|
+
throw new tools_1.ToolInputParsingException(`Type errors in "${entry.name}" arguments:\n\n` +
|
|
112
|
+
`\`\`\`json\n${utils_1.LlmJson.stringify(valid)}\n\`\`\``, JSON.stringify(coerced));
|
|
113
|
+
const result = yield entry.execute(valid.data);
|
|
114
|
+
return result === undefined
|
|
115
|
+
? { success: true }
|
|
116
|
+
: { success: true, data: result };
|
|
117
|
+
}),
|
|
118
|
+
}); };
|
|
119
|
+
})(LangChainToolsRegistrar$1 || (LangChainToolsRegistrar.LangChainToolsRegistrar = LangChainToolsRegistrar$1 = {}));
|
|
120
|
+
|
|
121
|
+
return LangChainToolsRegistrar;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { requireLangChainToolsRegistrar as __require };
|
|
125
|
+
//# sourceMappingURL=LangChainToolsRegistrar2.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LangChainToolsRegistrar2.mjs","sources":["LangChainToolsRegistrar.js"],"sourcesContent":["\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.LangChainToolsRegistrar = void 0;\nconst tools_1 = require(\"@langchain/core/tools\");\nconst utils_1 = require(\"@typia/utils\");\nvar LangChainToolsRegistrar;\n(function (LangChainToolsRegistrar) {\n LangChainToolsRegistrar.convert = (props) => {\n var _a;\n const prefix = (_a = props.prefix) !== null && _a !== void 0 ? _a : false;\n const tools = [];\n // check duplicate tool names\n if (prefix === false && props.controllers.length >= 2) {\n const names = new Map();\n const duplicates = [];\n for (const controller of props.controllers) {\n for (const func of controller.application.functions) {\n const existing = names.get(func.name);\n if (existing !== undefined)\n duplicates.push(`\"${func.name}\" in \"${controller.name}\" (conflicts with \"${existing}\")`);\n else\n names.set(func.name, controller.name);\n }\n }\n if (duplicates.length > 0)\n throw new Error(`Duplicate tool names found:\\n - ${duplicates.join(\"\\n - \")}`);\n }\n // convert controllers to tools\n for (const controller of props.controllers) {\n if (controller.protocol === \"class\") {\n convertClassController(tools, controller, prefix);\n }\n else {\n convertHttpController(tools, controller, prefix);\n }\n }\n return tools;\n };\n const convertClassController = (tools, controller, prefix) => {\n const execute = controller.execute;\n for (const func of controller.application.functions) {\n const toolName = prefix\n ? `${controller.name}_${func.name}`\n : func.name;\n const method = execute[func.name];\n if (typeof method !== \"function\") {\n throw new Error(`Method \"${func.name}\" not found on controller \"${controller.name}\"`);\n }\n tools.push(createTool({\n name: toolName,\n function: func,\n execute: (args) => __awaiter(this, void 0, void 0, function* () { return method.call(execute, args); }),\n }));\n }\n };\n const convertHttpController = (tools, controller, prefix) => {\n const application = controller.application;\n const connection = controller.connection;\n for (const func of application.functions) {\n const toolName = prefix\n ? `${controller.name}_${func.name}`\n : func.name;\n tools.push(createTool({\n name: toolName,\n function: func,\n execute: (args) => __awaiter(this, void 0, void 0, function* () {\n if (controller.execute !== undefined) {\n const response = yield controller.execute({\n connection,\n application,\n function: func,\n arguments: args,\n });\n return response.body;\n }\n return utils_1.HttpLlm.execute({\n application,\n function: func,\n connection,\n input: args,\n });\n }),\n }));\n }\n };\n const createTool = (entry) => { var _a; return new tools_1.DynamicStructuredTool({\n name: entry.name,\n description: (_a = entry.function.description) !== null && _a !== void 0 ? _a : \"\",\n schema: entry.function.parameters,\n func: (args) => __awaiter(this, void 0, void 0, function* () {\n const coerced = utils_1.LlmJson.coerce(args, entry.function.parameters);\n const valid = entry.function.validate(coerced);\n if (valid.success === false)\n throw new tools_1.ToolInputParsingException(`Type errors in \"${entry.name}\" arguments:\\n\\n` +\n `\\`\\`\\`json\\n${utils_1.LlmJson.stringify(valid)}\\n\\`\\`\\``, JSON.stringify(coerced));\n const result = yield entry.execute(valid.data);\n return result === undefined\n ? { success: true }\n : { success: true, data: result };\n }),\n }); };\n})(LangChainToolsRegistrar || (exports.LangChainToolsRegistrar = LangChainToolsRegistrar = {}));\n//# sourceMappingURL=LangChainToolsRegistrar.js.map"],"names":["this","LangChainToolsRegistrar_1","LangChainToolsRegistrar"],"mappings":";;;;;;;;;AACA,CAAA,IAAI,SAAS,GAAG,CAACA,uBAAI,IAAIA,uBAAI,CAAC,SAAS,KAAK,UAAU,OAAO,EAAE,UAAU,EAAE,CAAC,EAAE,SAAS,EAAE;KACrF,SAAS,KAAK,CAAC,KAAK,EAAE,EAAE,OAAO,KAAK,YAAY,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,UAAU,OAAO,EAAE,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA,CAAE,CAAC,CAAC,CAAA;AAC9G,KAAI,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,EAAE,UAAU,OAAO,EAAE,MAAM,EAAE;AAC/D,SAAQ,SAAS,SAAS,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,S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C,UAAU,CAAC;aACvE,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC;AAC1D,aAAY,IAAI,KAAK,CAAC,OAAO,KAAK,KAAK;AACvC,iBAAgB,MAAM,IAAI,OAAO,CAAC,yBAAyB,CAAC,CAAC,gBAAgB,EAAE,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC;qBACvF,CAAC,YAAY,EAAE,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;aAC3F,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC;aAC9C,OAAO,MAAM,KAAK;mBACZ,EAAE,OAAO,EAAE,IAAI;mBACf,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE;AACjD,SAAA,CAAS,CAAC;MACL,CAAC,CAAC,CAAA,CAAE;CACT,CAAC,EAAEA,yBAAuB,KAAKD,uBAAA,CAAA,uBAA+B,GAAGC,yBAAuB,GAAG,EAAE,CAAC,CAAC;AAC/F;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typia/langchain",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.0.0-dev.20260426",
|
|
4
4
|
"description": "LangChain.js integration for typia",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://typia.io",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@typia/
|
|
26
|
-
"@typia/
|
|
25
|
+
"@typia/interface": "^13.0.0-dev.20260426",
|
|
26
|
+
"@typia/utils": "^13.0.0-dev.20260426"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@langchain/core": ">=1.0.0"
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
"@langchain/core": "^1.1.31",
|
|
33
33
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
34
34
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
35
|
-
"@
|
|
35
|
+
"@typescript/native-preview": "7.0.0-dev.20260421.2",
|
|
36
36
|
"rimraf": "^6.1.2",
|
|
37
37
|
"rollup": "^4.56.0",
|
|
38
38
|
"rollup-plugin-auto-external": "^2.0.0",
|
|
39
39
|
"rollup-plugin-node-externals": "^8.1.2",
|
|
40
40
|
"tinyglobby": "^0.2.12",
|
|
41
|
-
"
|
|
41
|
+
"ttsc": "^0.4.2"
|
|
42
42
|
},
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"files": [
|
|
@@ -58,15 +58,14 @@
|
|
|
58
58
|
"chatgpt",
|
|
59
59
|
"gemini",
|
|
60
60
|
"validation",
|
|
61
|
-
"json-schema"
|
|
62
|
-
"typescript"
|
|
61
|
+
"json-schema"
|
|
63
62
|
],
|
|
64
63
|
"publishConfig": {
|
|
65
64
|
"access": "public"
|
|
66
65
|
},
|
|
67
66
|
"scripts": {
|
|
68
|
-
"build": "rimraf lib &&
|
|
69
|
-
"dev": "
|
|
67
|
+
"build": "rimraf lib && ttsc && rollup -c",
|
|
68
|
+
"dev": "ttsc --watch"
|
|
70
69
|
},
|
|
71
70
|
"module": "lib/index.mjs",
|
|
72
71
|
"types": "lib/index.d.ts"
|
package/src/index.ts
CHANGED
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import type { DynamicStructuredTool } from "@langchain/core/tools";
|
|
2
|
-
import { IHttpLlmController, ILlmController } from "@typia/interface";
|
|
3
|
-
|
|
4
|
-
import { LangChainToolsRegistrar } from "./internal/LangChainToolsRegistrar";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Convert typia controllers to LangChain tools.
|
|
8
|
-
*
|
|
9
|
-
* Converts TypeScript class methods via `typia.llm.controller<Class>()` or
|
|
10
|
-
* OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
|
|
11
|
-
*
|
|
12
|
-
* Every tool call is validated by typia. If LLM provides invalid arguments,
|
|
13
|
-
* returns validation error formatted by {@link LlmJson.stringify} so that LLM
|
|
14
|
-
* can correct them automatically.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { initChatModel } from "langchain/chat_models/universal";
|
|
19
|
-
* import typia from "typia";
|
|
20
|
-
* import { toLangChainTools } from "@typia/langchain";
|
|
21
|
-
*
|
|
22
|
-
* class Calculator {
|
|
23
|
-
* add(input: { a: number; b: number }): { value: number } {
|
|
24
|
-
* return { value: input.a + input.b };
|
|
25
|
-
* }
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* const tools = toLangChainTools({
|
|
29
|
-
* controllers: [
|
|
30
|
-
* typia.llm.controller<Calculator>("calculator", new Calculator()),
|
|
31
|
-
* ],
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* const llm = await initChatModel();
|
|
35
|
-
* const modelWithTools = llm.bindTools(tools);
|
|
36
|
-
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
37
|
-
* ```;
|
|
38
|
-
*
|
|
39
|
-
* @param props Conversion properties
|
|
40
|
-
* @returns Array of LangChain DynamicStructuredTool
|
|
41
|
-
*/
|
|
42
|
-
export function toLangChainTools(props: {
|
|
43
|
-
/**
|
|
44
|
-
* List of controllers to convert to LangChain tools.
|
|
45
|
-
*
|
|
46
|
-
* - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
|
|
47
|
-
* methods of the class to tools
|
|
48
|
-
* - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
|
|
49
|
-
* operations from OpenAPI document to tools
|
|
50
|
-
*/
|
|
51
|
-
controllers: Array<ILlmController | IHttpLlmController>;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Whether to add controller name as prefix to tool names.
|
|
55
|
-
*
|
|
56
|
-
* If `true`, tool names become `{controllerName}_{methodName}`. If `false`,
|
|
57
|
-
* tool names are just `{methodName}`.
|
|
58
|
-
*
|
|
59
|
-
* @default false
|
|
60
|
-
*/
|
|
61
|
-
prefix?: boolean | undefined;
|
|
62
|
-
}): DynamicStructuredTool[] {
|
|
63
|
-
return LangChainToolsRegistrar.convert(props);
|
|
64
|
-
}
|
|
1
|
+
import type { DynamicStructuredTool } from "@langchain/core/tools";
|
|
2
|
+
import { IHttpLlmController, ILlmController } from "@typia/interface";
|
|
3
|
+
|
|
4
|
+
import { LangChainToolsRegistrar } from "./internal/LangChainToolsRegistrar";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Convert typia controllers to LangChain tools.
|
|
8
|
+
*
|
|
9
|
+
* Converts TypeScript class methods via `typia.llm.controller<Class>()` or
|
|
10
|
+
* OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
|
|
11
|
+
*
|
|
12
|
+
* Every tool call is validated by typia. If LLM provides invalid arguments,
|
|
13
|
+
* returns validation error formatted by {@link LlmJson.stringify} so that LLM
|
|
14
|
+
* can correct them automatically.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { initChatModel } from "langchain/chat_models/universal";
|
|
19
|
+
* import typia from "typia";
|
|
20
|
+
* import { toLangChainTools } from "@typia/langchain";
|
|
21
|
+
*
|
|
22
|
+
* class Calculator {
|
|
23
|
+
* add(input: { a: number; b: number }): { value: number } {
|
|
24
|
+
* return { value: input.a + input.b };
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const tools = toLangChainTools({
|
|
29
|
+
* controllers: [
|
|
30
|
+
* typia.llm.controller<Calculator>("calculator", new Calculator()),
|
|
31
|
+
* ],
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* const llm = await initChatModel();
|
|
35
|
+
* const modelWithTools = llm.bindTools(tools);
|
|
36
|
+
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
37
|
+
* ```;
|
|
38
|
+
*
|
|
39
|
+
* @param props Conversion properties
|
|
40
|
+
* @returns Array of LangChain DynamicStructuredTool
|
|
41
|
+
*/
|
|
42
|
+
export function toLangChainTools(props: {
|
|
43
|
+
/**
|
|
44
|
+
* List of controllers to convert to LangChain tools.
|
|
45
|
+
*
|
|
46
|
+
* - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
|
|
47
|
+
* methods of the class to tools
|
|
48
|
+
* - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
|
|
49
|
+
* operations from OpenAPI document to tools
|
|
50
|
+
*/
|
|
51
|
+
controllers: Array<ILlmController | IHttpLlmController>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether to add controller name as prefix to tool names.
|
|
55
|
+
*
|
|
56
|
+
* If `true`, tool names become `{controllerName}_{methodName}`. If `false`,
|
|
57
|
+
* tool names are just `{methodName}`.
|
|
58
|
+
*
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
61
|
+
prefix?: boolean | undefined;
|
|
62
|
+
}): DynamicStructuredTool[] {
|
|
63
|
+
return LangChainToolsRegistrar.convert(props);
|
|
64
|
+
}
|
|
@@ -1,149 +1,149 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DynamicStructuredTool,
|
|
3
|
-
ToolInputParsingException,
|
|
4
|
-
} from "@langchain/core/tools";
|
|
5
|
-
import {
|
|
6
|
-
IHttpLlmController,
|
|
7
|
-
IHttpLlmFunction,
|
|
8
|
-
ILlmController,
|
|
9
|
-
ILlmFunction,
|
|
10
|
-
IValidation,
|
|
11
|
-
} from "@typia/interface";
|
|
12
|
-
import { HttpLlm, LlmJson } from "@typia/utils";
|
|
13
|
-
|
|
14
|
-
export namespace LangChainToolsRegistrar {
|
|
15
|
-
export const convert = (props: {
|
|
16
|
-
controllers: Array<ILlmController | IHttpLlmController>;
|
|
17
|
-
prefix?: boolean | undefined;
|
|
18
|
-
}): DynamicStructuredTool[] => {
|
|
19
|
-
const prefix: boolean = props.prefix ?? false;
|
|
20
|
-
const tools: DynamicStructuredTool[] = [];
|
|
21
|
-
|
|
22
|
-
// check duplicate tool names
|
|
23
|
-
if (prefix === false && props.controllers.length >= 2) {
|
|
24
|
-
const names: Map<string, string> = new Map();
|
|
25
|
-
const duplicates: string[] = [];
|
|
26
|
-
for (const controller of props.controllers) {
|
|
27
|
-
for (const func of controller.application.functions) {
|
|
28
|
-
const existing: string | undefined = names.get(func.name);
|
|
29
|
-
if (existing !== undefined)
|
|
30
|
-
duplicates.push(
|
|
31
|
-
`"${func.name}" in "${controller.name}" (conflicts with "${existing}")`,
|
|
32
|
-
);
|
|
33
|
-
else names.set(func.name, controller.name);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (duplicates.length > 0)
|
|
37
|
-
throw new Error(
|
|
38
|
-
`Duplicate tool names found:\n - ${duplicates.join("\n - ")}`,
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// convert controllers to tools
|
|
43
|
-
for (const controller of props.controllers) {
|
|
44
|
-
if (controller.protocol === "class") {
|
|
45
|
-
convertClassController(tools, controller, prefix);
|
|
46
|
-
} else {
|
|
47
|
-
convertHttpController(tools, controller, prefix);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return tools;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const convertClassController = (
|
|
55
|
-
tools: DynamicStructuredTool[],
|
|
56
|
-
controller: ILlmController,
|
|
57
|
-
prefix: boolean,
|
|
58
|
-
): void => {
|
|
59
|
-
const execute: Record<string, unknown> = controller.execute;
|
|
60
|
-
|
|
61
|
-
for (const func of controller.application.functions) {
|
|
62
|
-
const toolName: string = prefix
|
|
63
|
-
? `${controller.name}_${func.name}`
|
|
64
|
-
: func.name;
|
|
65
|
-
|
|
66
|
-
const method: unknown = execute[func.name];
|
|
67
|
-
if (typeof method !== "function") {
|
|
68
|
-
throw new Error(
|
|
69
|
-
`Method "${func.name}" not found on controller "${controller.name}"`,
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
tools.push(
|
|
74
|
-
createTool({
|
|
75
|
-
name: toolName,
|
|
76
|
-
function: func,
|
|
77
|
-
execute: async (args: unknown) => method.call(execute, args),
|
|
78
|
-
}),
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const convertHttpController = (
|
|
84
|
-
tools: DynamicStructuredTool[],
|
|
85
|
-
controller: IHttpLlmController,
|
|
86
|
-
prefix: boolean,
|
|
87
|
-
): void => {
|
|
88
|
-
const application = controller.application;
|
|
89
|
-
const connection = controller.connection;
|
|
90
|
-
|
|
91
|
-
for (const func of application.functions) {
|
|
92
|
-
const toolName: string = prefix
|
|
93
|
-
? `${controller.name}_${func.name}`
|
|
94
|
-
: func.name;
|
|
95
|
-
|
|
96
|
-
tools.push(
|
|
97
|
-
createTool({
|
|
98
|
-
name: toolName,
|
|
99
|
-
function: func,
|
|
100
|
-
execute: async (args: unknown) => {
|
|
101
|
-
if (controller.execute !== undefined) {
|
|
102
|
-
const response = await controller.execute({
|
|
103
|
-
connection,
|
|
104
|
-
application,
|
|
105
|
-
function: func,
|
|
106
|
-
arguments: args as object,
|
|
107
|
-
});
|
|
108
|
-
return response.body;
|
|
109
|
-
}
|
|
110
|
-
return HttpLlm.execute({
|
|
111
|
-
application,
|
|
112
|
-
function: func,
|
|
113
|
-
connection,
|
|
114
|
-
input: args as object,
|
|
115
|
-
});
|
|
116
|
-
},
|
|
117
|
-
}),
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const createTool = (entry: {
|
|
123
|
-
name: string;
|
|
124
|
-
function: ILlmFunction | IHttpLlmFunction;
|
|
125
|
-
execute: (args: unknown) => Promise<unknown>;
|
|
126
|
-
}): DynamicStructuredTool =>
|
|
127
|
-
new DynamicStructuredTool<any>({
|
|
128
|
-
name: entry.name,
|
|
129
|
-
description: entry.function.description ?? "",
|
|
130
|
-
schema: entry.function.parameters,
|
|
131
|
-
func: async (args: unknown): Promise<unknown> => {
|
|
132
|
-
const coerced: unknown = LlmJson.coerce(
|
|
133
|
-
args,
|
|
134
|
-
entry.function.parameters,
|
|
135
|
-
);
|
|
136
|
-
const valid: IValidation<unknown> = entry.function.validate(coerced);
|
|
137
|
-
if (valid.success === false)
|
|
138
|
-
throw new ToolInputParsingException(
|
|
139
|
-
`Type errors in "${entry.name}" arguments:\n\n` +
|
|
140
|
-
`\`\`\`json\n${LlmJson.stringify(valid)}\n\`\`\``,
|
|
141
|
-
JSON.stringify(coerced),
|
|
142
|
-
);
|
|
143
|
-
const result: unknown = await entry.execute(valid.data);
|
|
144
|
-
return result === undefined
|
|
145
|
-
? { success: true }
|
|
146
|
-
: { success: true, data: result };
|
|
147
|
-
},
|
|
148
|
-
});
|
|
149
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
DynamicStructuredTool,
|
|
3
|
+
ToolInputParsingException,
|
|
4
|
+
} from "@langchain/core/tools";
|
|
5
|
+
import {
|
|
6
|
+
IHttpLlmController,
|
|
7
|
+
IHttpLlmFunction,
|
|
8
|
+
ILlmController,
|
|
9
|
+
ILlmFunction,
|
|
10
|
+
IValidation,
|
|
11
|
+
} from "@typia/interface";
|
|
12
|
+
import { HttpLlm, LlmJson } from "@typia/utils";
|
|
13
|
+
|
|
14
|
+
export namespace LangChainToolsRegistrar {
|
|
15
|
+
export const convert = (props: {
|
|
16
|
+
controllers: Array<ILlmController | IHttpLlmController>;
|
|
17
|
+
prefix?: boolean | undefined;
|
|
18
|
+
}): DynamicStructuredTool[] => {
|
|
19
|
+
const prefix: boolean = props.prefix ?? false;
|
|
20
|
+
const tools: DynamicStructuredTool[] = [];
|
|
21
|
+
|
|
22
|
+
// check duplicate tool names
|
|
23
|
+
if (prefix === false && props.controllers.length >= 2) {
|
|
24
|
+
const names: Map<string, string> = new Map();
|
|
25
|
+
const duplicates: string[] = [];
|
|
26
|
+
for (const controller of props.controllers) {
|
|
27
|
+
for (const func of controller.application.functions) {
|
|
28
|
+
const existing: string | undefined = names.get(func.name);
|
|
29
|
+
if (existing !== undefined)
|
|
30
|
+
duplicates.push(
|
|
31
|
+
`"${func.name}" in "${controller.name}" (conflicts with "${existing}")`,
|
|
32
|
+
);
|
|
33
|
+
else names.set(func.name, controller.name);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (duplicates.length > 0)
|
|
37
|
+
throw new Error(
|
|
38
|
+
`Duplicate tool names found:\n - ${duplicates.join("\n - ")}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// convert controllers to tools
|
|
43
|
+
for (const controller of props.controllers) {
|
|
44
|
+
if (controller.protocol === "class") {
|
|
45
|
+
convertClassController(tools, controller, prefix);
|
|
46
|
+
} else {
|
|
47
|
+
convertHttpController(tools, controller, prefix);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return tools;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const convertClassController = (
|
|
55
|
+
tools: DynamicStructuredTool[],
|
|
56
|
+
controller: ILlmController,
|
|
57
|
+
prefix: boolean,
|
|
58
|
+
): void => {
|
|
59
|
+
const execute: Record<string, unknown> = controller.execute;
|
|
60
|
+
|
|
61
|
+
for (const func of controller.application.functions) {
|
|
62
|
+
const toolName: string = prefix
|
|
63
|
+
? `${controller.name}_${func.name}`
|
|
64
|
+
: func.name;
|
|
65
|
+
|
|
66
|
+
const method: unknown = execute[func.name];
|
|
67
|
+
if (typeof method !== "function") {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Method "${func.name}" not found on controller "${controller.name}"`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
tools.push(
|
|
74
|
+
createTool({
|
|
75
|
+
name: toolName,
|
|
76
|
+
function: func,
|
|
77
|
+
execute: async (args: unknown) => method.call(execute, args),
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const convertHttpController = (
|
|
84
|
+
tools: DynamicStructuredTool[],
|
|
85
|
+
controller: IHttpLlmController,
|
|
86
|
+
prefix: boolean,
|
|
87
|
+
): void => {
|
|
88
|
+
const application = controller.application;
|
|
89
|
+
const connection = controller.connection;
|
|
90
|
+
|
|
91
|
+
for (const func of application.functions) {
|
|
92
|
+
const toolName: string = prefix
|
|
93
|
+
? `${controller.name}_${func.name}`
|
|
94
|
+
: func.name;
|
|
95
|
+
|
|
96
|
+
tools.push(
|
|
97
|
+
createTool({
|
|
98
|
+
name: toolName,
|
|
99
|
+
function: func,
|
|
100
|
+
execute: async (args: unknown) => {
|
|
101
|
+
if (controller.execute !== undefined) {
|
|
102
|
+
const response = await controller.execute({
|
|
103
|
+
connection,
|
|
104
|
+
application,
|
|
105
|
+
function: func,
|
|
106
|
+
arguments: args as object,
|
|
107
|
+
});
|
|
108
|
+
return response.body;
|
|
109
|
+
}
|
|
110
|
+
return HttpLlm.execute({
|
|
111
|
+
application,
|
|
112
|
+
function: func,
|
|
113
|
+
connection,
|
|
114
|
+
input: args as object,
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const createTool = (entry: {
|
|
123
|
+
name: string;
|
|
124
|
+
function: ILlmFunction | IHttpLlmFunction;
|
|
125
|
+
execute: (args: unknown) => Promise<unknown>;
|
|
126
|
+
}): DynamicStructuredTool =>
|
|
127
|
+
new DynamicStructuredTool<any>({
|
|
128
|
+
name: entry.name,
|
|
129
|
+
description: entry.function.description ?? "",
|
|
130
|
+
schema: entry.function.parameters,
|
|
131
|
+
func: async (args: unknown): Promise<unknown> => {
|
|
132
|
+
const coerced: unknown = LlmJson.coerce(
|
|
133
|
+
args,
|
|
134
|
+
entry.function.parameters,
|
|
135
|
+
);
|
|
136
|
+
const valid: IValidation<unknown> = entry.function.validate(coerced);
|
|
137
|
+
if (valid.success === false)
|
|
138
|
+
throw new ToolInputParsingException(
|
|
139
|
+
`Type errors in "${entry.name}" arguments:\n\n` +
|
|
140
|
+
`\`\`\`json\n${LlmJson.stringify(valid)}\n\`\`\``,
|
|
141
|
+
JSON.stringify(coerced),
|
|
142
|
+
);
|
|
143
|
+
const result: unknown = await entry.execute(valid.data);
|
|
144
|
+
return result === undefined
|
|
145
|
+
? { success: true }
|
|
146
|
+
: { success: true, data: result };
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|