@typia/langchain 13.0.0-rc.1 → 13.0.0-rc.2
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 +111 -114
- package/lib/index.d.ts +34 -26
- package/lib/index.js +14 -38
- package/lib/index.js.map +1 -1
- package/lib/index2.mjs +14 -38
- package/lib/index2.mjs.map +1 -1
- package/lib/internal/LangChainToolsRegistrar.js +26 -12
- package/lib/internal/LangChainToolsRegistrar.js.map +1 -1
- package/lib/internal/LangChainToolsRegistrar2.mjs +26 -12
- package/lib/internal/LangChainToolsRegistrar2.mjs.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +70 -28
- package/src/internal/LangChainToolsRegistrar.ts +37 -15
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,114 +1,111 @@
|
|
|
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 typia
|
|
21
|
-
npm install -D ttsc typescript@rc
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
### From TypeScript class
|
|
27
|
-
|
|
28
|
-
```typescript
|
|
29
|
-
import { ChainValues, Runnable } from "@langchain/core";
|
|
30
|
-
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
31
|
-
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
32
|
-
import { ChatOpenAI } from "@langchain/openai";
|
|
33
|
-
import { toLangChainTools } from "@typia/langchain";
|
|
34
|
-
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
|
|
35
|
-
import typia from "typia";
|
|
36
|
-
|
|
37
|
-
const tools: DynamicStructuredTool[] = toLangChainTools(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
["
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
import {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
- No manual schema definition — generates everything from TypeScript types or OpenAPI
|
|
113
|
-
- Automatic argument validation with LLM-friendly error feedback
|
|
114
|
-
- 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 typia
|
|
21
|
+
npm install -D ttsc typescript@rc
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### From TypeScript class
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { ChainValues, Runnable } from "@langchain/core";
|
|
30
|
+
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
31
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
32
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
33
|
+
import { toLangChainTools } from "@typia/langchain";
|
|
34
|
+
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
|
|
35
|
+
import typia from "typia";
|
|
36
|
+
|
|
37
|
+
const tools: DynamicStructuredTool[] = toLangChainTools(
|
|
38
|
+
typia.llm.controller<Calculator>("Calculator", new Calculator()),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const agent: Runnable = createToolCallingAgent({
|
|
42
|
+
llm: new ChatOpenAI({ model: "gpt-4o" }),
|
|
43
|
+
tools,
|
|
44
|
+
prompt: ChatPromptTemplate.fromMessages([
|
|
45
|
+
["system", "You are a helpful assistant."],
|
|
46
|
+
["human", "{input}"],
|
|
47
|
+
["placeholder", "{agent_scratchpad}"],
|
|
48
|
+
]),
|
|
49
|
+
});
|
|
50
|
+
const executor: AgentExecutor = new AgentExecutor({ agent, tools });
|
|
51
|
+
const result: ChainValues = await executor.invoke({ input: "What is 10 + 5?" });
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### From OpenAPI document
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
58
|
+
import { toLangChainTools } from "@typia/langchain";
|
|
59
|
+
import { HttpLlm } from "@typia/utils";
|
|
60
|
+
|
|
61
|
+
const tools: DynamicStructuredTool[] = toLangChainTools(
|
|
62
|
+
HttpLlm.controller({
|
|
63
|
+
name: "petStore",
|
|
64
|
+
document: yourOpenApiDocument,
|
|
65
|
+
connection: { host: "https://api.example.com" },
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Structured Output
|
|
71
|
+
|
|
72
|
+
Use `typia.llm.parameters<T>()` with LangChain's `withStructuredOutput()` to generate structured output with validation:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
76
|
+
import { dedent, LlmJson } from "@typia/utils";
|
|
77
|
+
import typia, { tags } from "typia";
|
|
78
|
+
|
|
79
|
+
interface IMember {
|
|
80
|
+
email: string & tags.Format<"email">;
|
|
81
|
+
name: string;
|
|
82
|
+
age: number & tags.Minimum<0> & tags.Maximum<100>;
|
|
83
|
+
hobbies: string[];
|
|
84
|
+
joined_at: string & tags.Format<"date">;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const model = new ChatOpenAI({ model: "gpt-4o" }).withStructuredOutput(
|
|
88
|
+
typia.llm.parameters<IMember>(),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const member: IMember = await model.invoke(dedent`
|
|
92
|
+
I am a new member of the community.
|
|
93
|
+
|
|
94
|
+
My name is John Doe, and I am 25 years old.
|
|
95
|
+
I like playing basketball and reading books,
|
|
96
|
+
and joined to this community at 2022-01-01.
|
|
97
|
+
`);
|
|
98
|
+
|
|
99
|
+
// Validate the result
|
|
100
|
+
const result = typia.validate<IMember>(member);
|
|
101
|
+
if (!result.success) {
|
|
102
|
+
console.error(LlmJson.stringify(result));
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Features
|
|
107
|
+
|
|
108
|
+
- No manual schema definition — generates everything from TypeScript types or OpenAPI
|
|
109
|
+
- Automatic argument validation with LLM-friendly error feedback
|
|
110
|
+
- Runtime tool errors are returned as `{ success: false, error }` for model recovery
|
|
111
|
+
- Supports both class-based (`typia.llm.controller`) and HTTP-based (`HttpLlm.controller`) controllers
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
import type { DynamicStructuredTool } from "@langchain/core/tools";
|
|
2
2
|
import { IHttpLlmController, ILlmController } from "@typia/interface";
|
|
3
|
+
type ILangChainController = ILlmController | IHttpLlmController;
|
|
4
|
+
interface ILangChainToolsOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether to add controller name as prefix to tool names.
|
|
7
|
+
*
|
|
8
|
+
* If `true`, tool names become `{controllerName}_{methodName}`. If `false`,
|
|
9
|
+
* tool names are just `{methodName}`.
|
|
10
|
+
*
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
prefix?: boolean | undefined;
|
|
14
|
+
}
|
|
15
|
+
interface ILangChainToolsProps extends ILangChainToolsOptions {
|
|
16
|
+
/**
|
|
17
|
+
* List of controllers to convert to LangChain tools.
|
|
18
|
+
*
|
|
19
|
+
* - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
|
|
20
|
+
* methods of the class to tools
|
|
21
|
+
* - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
|
|
22
|
+
* operations from OpenAPI document to tools
|
|
23
|
+
*/
|
|
24
|
+
controllers: ILangChainController[];
|
|
25
|
+
}
|
|
3
26
|
/**
|
|
4
27
|
* Convert typia controllers to LangChain tools.
|
|
5
28
|
*
|
|
@@ -22,37 +45,22 @@ import { IHttpLlmController, ILlmController } from "@typia/interface";
|
|
|
22
45
|
* }
|
|
23
46
|
* }
|
|
24
47
|
*
|
|
25
|
-
* const
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
48
|
+
* const controller = typia.llm.controller<Calculator>(
|
|
49
|
+
* "calculator",
|
|
50
|
+
* new Calculator(),
|
|
51
|
+
* );
|
|
52
|
+
* const tools = toLangChainTools(controller);
|
|
30
53
|
*
|
|
31
54
|
* const llm = await initChatModel();
|
|
32
55
|
* const modelWithTools = llm.bindTools(tools);
|
|
33
56
|
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
34
57
|
* ```;
|
|
35
58
|
*
|
|
36
|
-
* @param
|
|
59
|
+
* @param input Controller, controller list, or conversion properties
|
|
60
|
+
* @param options Conversion options when `input` is not a properties object
|
|
37
61
|
* @returns Array of LangChain DynamicStructuredTool
|
|
38
62
|
*/
|
|
39
|
-
export declare function toLangChainTools(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
* - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
|
|
44
|
-
* methods of the class to tools
|
|
45
|
-
* - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
|
|
46
|
-
* operations from OpenAPI document to tools
|
|
47
|
-
*/
|
|
48
|
-
controllers: Array<ILlmController | IHttpLlmController>;
|
|
49
|
-
/**
|
|
50
|
-
* Whether to add controller name as prefix to tool names.
|
|
51
|
-
*
|
|
52
|
-
* If `true`, tool names become `{controllerName}_{methodName}`. If `false`,
|
|
53
|
-
* tool names are just `{methodName}`.
|
|
54
|
-
*
|
|
55
|
-
* @default false
|
|
56
|
-
*/
|
|
57
|
-
prefix?: boolean | undefined;
|
|
58
|
-
}): DynamicStructuredTool[];
|
|
63
|
+
export declare function toLangChainTools(controller: ILangChainController, options?: ILangChainToolsOptions | undefined): DynamicStructuredTool[];
|
|
64
|
+
export declare function toLangChainTools(controllers: ILangChainController[], options?: ILangChainToolsOptions | undefined): DynamicStructuredTool[];
|
|
65
|
+
export declare function toLangChainTools(props: ILangChainToolsProps): DynamicStructuredTool[];
|
|
66
|
+
export {};
|
package/lib/index.js
CHANGED
|
@@ -2,43 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toLangChainTools = toLangChainTools;
|
|
4
4
|
const LangChainToolsRegistrar_1 = require("./internal/LangChainToolsRegistrar");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
* Converts TypeScript class methods via `typia.llm.controller<Class>()` or
|
|
9
|
-
* OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
|
|
10
|
-
*
|
|
11
|
-
* Every tool call is validated by typia. If LLM provides invalid arguments,
|
|
12
|
-
* returns validation error formatted by {@link LlmJson.stringify} so that LLM
|
|
13
|
-
* can correct them automatically.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { initChatModel } from "langchain/chat_models/universal";
|
|
18
|
-
* import typia from "typia";
|
|
19
|
-
* import { toLangChainTools } from "@typia/langchain";
|
|
20
|
-
*
|
|
21
|
-
* class Calculator {
|
|
22
|
-
* add(input: { a: number; b: number }): { value: number } {
|
|
23
|
-
* return { value: input.a + input.b };
|
|
24
|
-
* }
|
|
25
|
-
* }
|
|
26
|
-
*
|
|
27
|
-
* const tools = toLangChainTools({
|
|
28
|
-
* controllers: [
|
|
29
|
-
* typia.llm.controller<Calculator>("calculator", new Calculator()),
|
|
30
|
-
* ],
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* const llm = await initChatModel();
|
|
34
|
-
* const modelWithTools = llm.bindTools(tools);
|
|
35
|
-
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
36
|
-
* ```;
|
|
37
|
-
*
|
|
38
|
-
* @param props Conversion properties
|
|
39
|
-
* @returns Array of LangChain DynamicStructuredTool
|
|
40
|
-
*/
|
|
41
|
-
function toLangChainTools(props) {
|
|
42
|
-
return LangChainToolsRegistrar_1.LangChainToolsRegistrar.convert(props);
|
|
5
|
+
function toLangChainTools(input, options) {
|
|
6
|
+
return LangChainToolsRegistrar_1.LangChainToolsRegistrar.convert(normalizeLangChainToolsProps(input, options));
|
|
43
7
|
}
|
|
8
|
+
const normalizeLangChainToolsProps = (input, options) => {
|
|
9
|
+
if (isLangChainToolsProps(input))
|
|
10
|
+
return input;
|
|
11
|
+
return {
|
|
12
|
+
controllers: Array.isArray(input) ? input : [input],
|
|
13
|
+
prefix: options === null || options === void 0 ? void 0 : options.prefix,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
const isLangChainToolsProps = (input) => Array.isArray(input) === false &&
|
|
17
|
+
typeof input === "object" &&
|
|
18
|
+
input !== null &&
|
|
19
|
+
"controllers" in input;
|
|
44
20
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAGA,gFAA6E;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAGA,gFAA6E;AA4E7E,0BACE,KAA2E,EAC3E,OAA4C;IAE5C,OAAO,iDAAuB,CAAC,OAAO,CACpC,4BAA4B,CAAC,KAAK,EAAE,OAAO,CAAC,CAC7C,CAAC;AACJ,CAAC;AAED,MAAM,4BAA4B,GAAG,CACnC,KAA2E,EAC3E,OAA4C,EACtB,EAAE;IACxB,IAAI,qBAAqB,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC/C,OAAO;QACL,WAAW,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACnD,MAAM,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM;KACxB,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,CAC5B,KAA2E,EAC5C,EAAE,CACjC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK;IAC9B,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,aAAa,IAAI,KAAK,CAAC"}
|
package/lib/index2.mjs
CHANGED
|
@@ -5,45 +5,21 @@ import { __exports as LangChainToolsRegistrar } from './_virtual/LangChainToolsR
|
|
|
5
5
|
Object.defineProperty(lib, "__esModule", { value: true });
|
|
6
6
|
var toLangChainTools_1 = lib.toLangChainTools = toLangChainTools;
|
|
7
7
|
const LangChainToolsRegistrar_1 = LangChainToolsRegistrar;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* Converts TypeScript class methods via `typia.llm.controller<Class>()` or
|
|
12
|
-
* OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
|
|
13
|
-
*
|
|
14
|
-
* Every tool call is validated by typia. If LLM provides invalid arguments,
|
|
15
|
-
* returns validation error formatted by {@link LlmJson.stringify} so that LLM
|
|
16
|
-
* can correct them automatically.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* import { initChatModel } from "langchain/chat_models/universal";
|
|
21
|
-
* import typia from "typia";
|
|
22
|
-
* import { toLangChainTools } from "@typia/langchain";
|
|
23
|
-
*
|
|
24
|
-
* class Calculator {
|
|
25
|
-
* add(input: { a: number; b: number }): { value: number } {
|
|
26
|
-
* return { value: input.a + input.b };
|
|
27
|
-
* }
|
|
28
|
-
* }
|
|
29
|
-
*
|
|
30
|
-
* const tools = toLangChainTools({
|
|
31
|
-
* controllers: [
|
|
32
|
-
* typia.llm.controller<Calculator>("calculator", new Calculator()),
|
|
33
|
-
* ],
|
|
34
|
-
* });
|
|
35
|
-
*
|
|
36
|
-
* const llm = await initChatModel();
|
|
37
|
-
* const modelWithTools = llm.bindTools(tools);
|
|
38
|
-
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
39
|
-
* ```;
|
|
40
|
-
*
|
|
41
|
-
* @param props Conversion properties
|
|
42
|
-
* @returns Array of LangChain DynamicStructuredTool
|
|
43
|
-
*/
|
|
44
|
-
function toLangChainTools(props) {
|
|
45
|
-
return LangChainToolsRegistrar_1.LangChainToolsRegistrar.convert(props);
|
|
8
|
+
function toLangChainTools(input, options) {
|
|
9
|
+
return LangChainToolsRegistrar_1.LangChainToolsRegistrar.convert(normalizeLangChainToolsProps(input, options));
|
|
46
10
|
}
|
|
11
|
+
const normalizeLangChainToolsProps = (input, options) => {
|
|
12
|
+
if (isLangChainToolsProps(input))
|
|
13
|
+
return input;
|
|
14
|
+
return {
|
|
15
|
+
controllers: Array.isArray(input) ? input : [input],
|
|
16
|
+
prefix: options === null || options === void 0 ? void 0 : options.prefix,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const isLangChainToolsProps = (input) => Array.isArray(input) === false &&
|
|
20
|
+
typeof input === "object" &&
|
|
21
|
+
input !== null &&
|
|
22
|
+
"controllers" in input;
|
|
47
23
|
|
|
48
24
|
export { lib as default, toLangChainTools_1 as toLangChainTools };
|
|
49
25
|
//# sourceMappingURL=index2.mjs.map
|
package/lib/index2.mjs.map
CHANGED
|
@@ -1 +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\");\
|
|
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\");\nfunction toLangChainTools(input, options) {\n return LangChainToolsRegistrar_1.LangChainToolsRegistrar.convert(normalizeLangChainToolsProps(input, options));\n}\nconst normalizeLangChainToolsProps = (input, options) => {\n if (isLangChainToolsProps(input))\n return input;\n return {\n controllers: Array.isArray(input) ? input : [input],\n prefix: options === null || options === void 0 ? void 0 : options.prefix,\n };\n};\nconst isLangChainToolsProps = (input) => Array.isArray(input) === false &&\n typeof input === \"object\" &&\n input !== null &&\n \"controllers\" in input;\n//# sourceMappingURL=index.js.map"],"names":["require$$0"],"mappings":";;;;AACA,MAAM,CAAC,cAAc,CAAC,GAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,IAAA,kBAAA,GAAA,GAAA,CAAA,gBAAwB,GAAG;AAC3B,MAAM,yBAAyB,GAAGA,uBAA6C;AAC/E,SAAS,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE;AAC1C,IAAI,OAAO,yBAAyB,CAAC,uBAAuB,CAAC,OAAO,CAAC,4BAA4B,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AAClH;AACA,MAAM,4BAA4B,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK;AACzD,IAAI,IAAI,qBAAqB,CAAC,KAAK,CAAC;AACpC,QAAQ,OAAO,KAAK;AACpB,IAAI,OAAO;AACX,QAAQ,WAAW,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,GAAG,CAAC,KAAK,CAAC;AAC3D,QAAQ,MAAM,EAAE,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM;AAChF,KAAK;AACL,CAAC;AACD,MAAM,qBAAqB,GAAG,CAAC,KAAK,KAAK,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK;AACvE,IAAI,OAAO,KAAK,KAAK,QAAQ;AAC7B,IAAI,KAAK,KAAK,IAAI;AAClB,IAAI,aAAa,IAAI,KAAK;;;;"}
|
|
@@ -92,21 +92,35 @@ var LangChainToolsRegistrar;
|
|
|
92
92
|
}));
|
|
93
93
|
}
|
|
94
94
|
};
|
|
95
|
-
const createTool = (entry) => { var _a; return
|
|
95
|
+
const createTool = (entry) => { var _a; return (0, tools_1.tool)((args) => __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
const valid = utils_1.LlmJson.validateArguments(entry.function, args !== null && args !== void 0 ? args : {});
|
|
97
|
+
if (valid.success === false)
|
|
98
|
+
throw new tools_1.ToolInputParsingException(`Type errors in "${entry.name}" arguments:\n\n` +
|
|
99
|
+
`\`\`\`json\n${utils_1.LlmJson.stringify(valid)}\n\`\`\``, JSON.stringify(args !== null && args !== void 0 ? args : {}));
|
|
100
|
+
try {
|
|
101
|
+
const result = yield entry.execute(valid.data);
|
|
102
|
+
if (result === undefined) {
|
|
103
|
+
return entry.function.output === undefined
|
|
104
|
+
? { success: true }
|
|
105
|
+
: {
|
|
106
|
+
success: false,
|
|
107
|
+
error: `Function "${entry.name}" returned undefined despite declaring an output schema`,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return { success: true, data: result };
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
return {
|
|
114
|
+
success: false,
|
|
115
|
+
error: error instanceof Error
|
|
116
|
+
? `${error.name}: ${error.message}`
|
|
117
|
+
: String(error),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}), {
|
|
96
121
|
name: entry.name,
|
|
97
122
|
description: (_a = entry.function.description) !== null && _a !== void 0 ? _a : "",
|
|
98
123
|
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
124
|
}); };
|
|
111
125
|
})(LangChainToolsRegistrar || (exports.LangChainToolsRegistrar = LangChainToolsRegistrar = {}));
|
|
112
126
|
//# sourceMappingURL=LangChainToolsRegistrar.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LangChainToolsRegistrar.js","sourceRoot":"","sources":["../../src/internal/LangChainToolsRegistrar.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
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; return (0, tools_1.tool)((args) => __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const valid = utils_1.LlmJson.validateArguments(entry.function, args !== null && args !== void 0 ? args : {});
|
|
100
|
+
if (valid.success === false)
|
|
101
|
+
throw new tools_1.ToolInputParsingException(`Type errors in "${entry.name}" arguments:\n\n` +
|
|
102
|
+
`\`\`\`json\n${utils_1.LlmJson.stringify(valid)}\n\`\`\``, JSON.stringify(args !== null && args !== void 0 ? args : {}));
|
|
103
|
+
try {
|
|
104
|
+
const result = yield entry.execute(valid.data);
|
|
105
|
+
if (result === undefined) {
|
|
106
|
+
return entry.function.output === undefined
|
|
107
|
+
? { success: true }
|
|
108
|
+
: {
|
|
109
|
+
success: false,
|
|
110
|
+
error: `Function "${entry.name}" returned undefined despite declaring an output schema`,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return { success: true, data: result };
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
return {
|
|
117
|
+
success: false,
|
|
118
|
+
error: error instanceof Error
|
|
119
|
+
? `${error.name}: ${error.message}`
|
|
120
|
+
: String(error),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}), {
|
|
99
124
|
name: entry.name,
|
|
100
125
|
description: (_a = entry.function.description) !== null && _a !== void 0 ? _a : "",
|
|
101
126
|
schema: entry.function.parameters,
|
|
102
|
-
func: (args) => __awaiter(this, void 0, void 0, function* () {
|
|
103
|
-
const coerced = utils_1.LlmJson.coerce(args, entry.function.parameters);
|
|
104
|
-
const valid = entry.function.validate(coerced);
|
|
105
|
-
if (valid.success === false)
|
|
106
|
-
throw new tools_1.ToolInputParsingException(`Type errors in "${entry.name}" arguments:\n\n` +
|
|
107
|
-
`\`\`\`json\n${utils_1.LlmJson.stringify(valid)}\n\`\`\``, JSON.stringify(coerced));
|
|
108
|
-
const result = yield entry.execute(valid.data);
|
|
109
|
-
return result === undefined
|
|
110
|
-
? { success: true }
|
|
111
|
-
: { success: true, data: result };
|
|
112
|
-
}),
|
|
113
127
|
}); };
|
|
114
128
|
})(LangChainToolsRegistrar || (LangChainToolsRegistrar_2 = LangChainToolsRegistrar$1.LangChainToolsRegistrar = LangChainToolsRegistrar = {}));
|
|
115
129
|
|
|
@@ -1 +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"],"mappings":";;;;AACA,IAAI,SAAS,GAAG,CAACA,yBAAI,IAAIA,yBAAI,CAAC,SAAS,KAAK,UAAU,OAAO,EAAE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AAE,IAAI,EAAE,MAAM,EAAE;AACjD,QAAA,CAAS,CAAC;AACV,KAAK,CAAC,CAAC,CAAA,CAAE;AACT,CAAC,EAAE,uBAAuB,KAAK,yBAAA,GAAAA,yBAAA,CAAA,uBAA+B,GAAG,uBAAuB,GAAG,EAAE,CAAC,CAAC;;;;"}
|
|
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 (0, tools_1.tool)((args) => __awaiter(this, void 0, void 0, function* () {\n const valid = utils_1.LlmJson.validateArguments(entry.function, args !== null && args !== void 0 ? args : {});\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(args !== null && args !== void 0 ? args : {}));\n try {\n const result = yield entry.execute(valid.data);\n if (result === undefined) {\n return entry.function.output === undefined\n ? { success: true }\n : {\n success: false,\n error: `Function \"${entry.name}\" returned undefined despite declaring an output schema`,\n };\n }\n return { success: true, data: result };\n }\n catch (error) {\n return {\n success: false,\n error: error instanceof Error\n ? `${error.name}: ${error.message}`\n : String(error),\n };\n }\n }), {\n name: entry.name,\n description: (_a = entry.function.description) !== null && _a !== void 0 ? _a : \"\",\n schema: entry.function.parameters,\n }); };\n})(LangChainToolsRegistrar || (exports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AE,IAAI;AACrC,sBAAsB;AACtB,wBAAwB,OAAO,EAAE,KAAK;AACtC,wBAAwB,KAAK,EAAE,CAAC,UAAU,EAAE,KAAK,CAAC,IAAI,CAAC,uDAAuD,CAAC;AAC/G,qBAAqB;AACrB,YAAA;AACA,YAAY,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE;AAClD,QAAA;AACA,QAAQ,OAAO,KAAK,EAAE;AACtB,YAAY,OAAO;AACnB,gBAAgB,OAAO,EAAE,KAAK;AAC9B,gBAAgB,KAAK,EAAE,KAAK,YAAY;AACxC,sBAAsB,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,CAAC,OAAO,CAAC;AACtD,sBAAsB,MAAM,CAAC,KAAK,CAAC;AACnC,aAAa;AACb,QAAA;AACA,IAAA,CAAK,CAAC,EAAE;AACR,QAAQ,IAAI,EAAE,KAAK,CAAC,IAAI;AACxB,QAAQ,WAAW,EAAE,CAAC,EAAE,GAAG,KAAK,CAAC,QAAQ,CAAC,WAAW,MAAM,IAAI,IAAI,EAAE,KAAK,MAAM,GAAG,EAAE,GAAG,EAAE;AAC1F,QAAQ,MAAM,EAAE,KAAK,CAAC,QAAQ,CAAC,UAAU;AACzC,KAAK,CAAC,CAAC,CAAA,CAAE;AACT,CAAC,EAAE,uBAAuB,KAAK,yBAAA,GAAAA,yBAAA,CAAA,uBAA+B,GAAG,uBAAuB,GAAG,EAAE,CAAC,CAAC;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typia/langchain",
|
|
3
|
-
"version": "13.0.0-rc.
|
|
3
|
+
"version": "13.0.0-rc.2",
|
|
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-rc.2",
|
|
26
|
+
"@typia/utils": "^13.0.0-rc.2"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@langchain/core": ">=1.0.0"
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"rollup": "^4.56.0",
|
|
39
39
|
"rollup-plugin-node-externals": "^8.1.2",
|
|
40
40
|
"tinyglobby": "^0.2.12",
|
|
41
|
-
"ttsc": "^0.
|
|
41
|
+
"ttsc": "^0.17.1"
|
|
42
42
|
},
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"files": [
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,32 @@ import { IHttpLlmController, ILlmController } from "@typia/interface";
|
|
|
3
3
|
|
|
4
4
|
import { LangChainToolsRegistrar } from "./internal/LangChainToolsRegistrar";
|
|
5
5
|
|
|
6
|
+
type ILangChainController = ILlmController | IHttpLlmController;
|
|
7
|
+
|
|
8
|
+
interface ILangChainToolsOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Whether to add controller name as prefix to tool names.
|
|
11
|
+
*
|
|
12
|
+
* If `true`, tool names become `{controllerName}_{methodName}`. If `false`,
|
|
13
|
+
* tool names are just `{methodName}`.
|
|
14
|
+
*
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
17
|
+
prefix?: boolean | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ILangChainToolsProps extends ILangChainToolsOptions {
|
|
21
|
+
/**
|
|
22
|
+
* List of controllers to convert to LangChain tools.
|
|
23
|
+
*
|
|
24
|
+
* - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
|
|
25
|
+
* methods of the class to tools
|
|
26
|
+
* - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
|
|
27
|
+
* operations from OpenAPI document to tools
|
|
28
|
+
*/
|
|
29
|
+
controllers: ILangChainController[];
|
|
30
|
+
}
|
|
31
|
+
|
|
6
32
|
/**
|
|
7
33
|
* Convert typia controllers to LangChain tools.
|
|
8
34
|
*
|
|
@@ -25,40 +51,56 @@ import { LangChainToolsRegistrar } from "./internal/LangChainToolsRegistrar";
|
|
|
25
51
|
* }
|
|
26
52
|
* }
|
|
27
53
|
*
|
|
28
|
-
* const
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
54
|
+
* const controller = typia.llm.controller<Calculator>(
|
|
55
|
+
* "calculator",
|
|
56
|
+
* new Calculator(),
|
|
57
|
+
* );
|
|
58
|
+
* const tools = toLangChainTools(controller);
|
|
33
59
|
*
|
|
34
60
|
* const llm = await initChatModel();
|
|
35
61
|
* const modelWithTools = llm.bindTools(tools);
|
|
36
62
|
* const result = await modelWithTools.invoke("What is 10 + 5?");
|
|
37
63
|
* ```;
|
|
38
64
|
*
|
|
39
|
-
* @param
|
|
65
|
+
* @param input Controller, controller list, or conversion properties
|
|
66
|
+
* @param options Conversion options when `input` is not a properties object
|
|
40
67
|
* @returns Array of LangChain DynamicStructuredTool
|
|
41
68
|
*/
|
|
42
|
-
export function toLangChainTools(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
*/
|
|
61
|
-
prefix?: boolean | undefined;
|
|
62
|
-
}): DynamicStructuredTool[] {
|
|
63
|
-
return LangChainToolsRegistrar.convert(props);
|
|
69
|
+
export function toLangChainTools(
|
|
70
|
+
controller: ILangChainController,
|
|
71
|
+
options?: ILangChainToolsOptions | undefined,
|
|
72
|
+
): DynamicStructuredTool[];
|
|
73
|
+
export function toLangChainTools(
|
|
74
|
+
controllers: ILangChainController[],
|
|
75
|
+
options?: ILangChainToolsOptions | undefined,
|
|
76
|
+
): DynamicStructuredTool[];
|
|
77
|
+
export function toLangChainTools(
|
|
78
|
+
props: ILangChainToolsProps,
|
|
79
|
+
): DynamicStructuredTool[];
|
|
80
|
+
export function toLangChainTools(
|
|
81
|
+
input: ILangChainController | ILangChainController[] | ILangChainToolsProps,
|
|
82
|
+
options?: ILangChainToolsOptions | undefined,
|
|
83
|
+
): DynamicStructuredTool[] {
|
|
84
|
+
return LangChainToolsRegistrar.convert(
|
|
85
|
+
normalizeLangChainToolsProps(input, options),
|
|
86
|
+
);
|
|
64
87
|
}
|
|
88
|
+
|
|
89
|
+
const normalizeLangChainToolsProps = (
|
|
90
|
+
input: ILangChainController | ILangChainController[] | ILangChainToolsProps,
|
|
91
|
+
options?: ILangChainToolsOptions | undefined,
|
|
92
|
+
): ILangChainToolsProps => {
|
|
93
|
+
if (isLangChainToolsProps(input)) return input;
|
|
94
|
+
return {
|
|
95
|
+
controllers: Array.isArray(input) ? input : [input],
|
|
96
|
+
prefix: options?.prefix,
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const isLangChainToolsProps = (
|
|
101
|
+
input: ILangChainController | ILangChainController[] | ILangChainToolsProps,
|
|
102
|
+
): input is ILangChainToolsProps =>
|
|
103
|
+
Array.isArray(input) === false &&
|
|
104
|
+
typeof input === "object" &&
|
|
105
|
+
input !== null &&
|
|
106
|
+
"controllers" in input;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DynamicStructuredTool,
|
|
3
3
|
ToolInputParsingException,
|
|
4
|
+
tool,
|
|
4
5
|
} from "@langchain/core/tools";
|
|
5
6
|
import {
|
|
6
7
|
IHttpLlmController,
|
|
@@ -124,26 +125,47 @@ export namespace LangChainToolsRegistrar {
|
|
|
124
125
|
function: ILlmFunction | IHttpLlmFunction;
|
|
125
126
|
execute: (args: unknown) => Promise<unknown>;
|
|
126
127
|
}): DynamicStructuredTool =>
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const coerced: unknown = LlmJson.coerce(
|
|
133
|
-
args,
|
|
134
|
-
entry.function.parameters,
|
|
128
|
+
tool(
|
|
129
|
+
async (args: unknown): Promise<unknown> => {
|
|
130
|
+
const valid: IValidation<unknown> = LlmJson.validateArguments(
|
|
131
|
+
entry.function,
|
|
132
|
+
args ?? {},
|
|
135
133
|
);
|
|
136
|
-
const valid: IValidation<unknown> = entry.function.validate(coerced);
|
|
137
134
|
if (valid.success === false)
|
|
138
135
|
throw new ToolInputParsingException(
|
|
139
136
|
`Type errors in "${entry.name}" arguments:\n\n` +
|
|
140
137
|
`\`\`\`json\n${LlmJson.stringify(valid)}\n\`\`\``,
|
|
141
|
-
JSON.stringify(
|
|
138
|
+
JSON.stringify(args ?? {}),
|
|
142
139
|
);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
140
|
+
try {
|
|
141
|
+
const result: unknown = await entry.execute(valid.data);
|
|
142
|
+
if (result === undefined) {
|
|
143
|
+
return entry.function.output === undefined
|
|
144
|
+
? ({ success: true } satisfies ITryResult)
|
|
145
|
+
: ({
|
|
146
|
+
success: false,
|
|
147
|
+
error: `Function "${entry.name}" returned undefined despite declaring an output schema`,
|
|
148
|
+
} satisfies ITryResult);
|
|
149
|
+
}
|
|
150
|
+
return { success: true, data: result } satisfies ITryResult;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return {
|
|
153
|
+
success: false,
|
|
154
|
+
error:
|
|
155
|
+
error instanceof Error
|
|
156
|
+
? `${error.name}: ${error.message}`
|
|
157
|
+
: String(error),
|
|
158
|
+
} satisfies ITryResult;
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: entry.name,
|
|
163
|
+
description: entry.function.description ?? "",
|
|
164
|
+
schema: entry.function.parameters,
|
|
147
165
|
},
|
|
148
|
-
|
|
166
|
+
);
|
|
149
167
|
}
|
|
168
|
+
|
|
169
|
+
type ITryResult =
|
|
170
|
+
| { success: true; data?: unknown | undefined }
|
|
171
|
+
| { success: false; error: string };
|