-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathrender.ts
72 lines (70 loc) · 2.23 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { StructuredToolInterface } from "@langchain/core/tools";
import {
ToolDefinition,
isOpenAITool,
} from "@langchain/core/language_models/base";
import { zodToJsonSchema, type JsonSchema7Type } from "zod-to-json-schema";
import { isZodSchema } from "@langchain/core/utils/types";
/**
* Render the tool name and description in plain text.
*
* Output will be in the format of:
* ```
* search: This tool is used for search
* calculator: This tool is used for math
* ```
* @param tools
* @returns a string of all tools and their descriptions
*/
export function renderTextDescription(
tools: StructuredToolInterface[] | ToolDefinition[]
): string {
if ((tools as unknown[]).every(isOpenAITool)) {
return (tools as ToolDefinition[])
.map(
(tool) =>
`${tool.function.name}${
tool.function.description ? `: ${tool.function.description}` : ""
}`
)
.join("\n");
}
return (tools as StructuredToolInterface[])
.map((tool) => `${tool.name}: ${tool.description}`)
.join("\n");
}
/**
* Render the tool name, description, and args in plain text.
* Output will be in the format of:'
* ```
* search: This tool is used for search, args: {"query": {"type": "string"}}
* calculator: This tool is used for math,
* args: {"expression": {"type": "string"}}
* ```
* @param tools
* @returns a string of all tools, their descriptions and a stringified version of their schemas
*/
export function renderTextDescriptionAndArgs(
tools: StructuredToolInterface[] | ToolDefinition[]
): string {
if ((tools as unknown[]).every(isOpenAITool)) {
return (tools as ToolDefinition[])
.map(
(tool) =>
`${tool.function.name}${
tool.function.description ? `: ${tool.function.description}` : ""
}, args: ${JSON.stringify(tool.function.parameters)}`
)
.join("\n");
}
return (tools as StructuredToolInterface[])
.map((tool) => {
const jsonSchema = (
isZodSchema(tool.schema) ? zodToJsonSchema(tool.schema) : tool.schema
) as { properties?: Record<string, JsonSchema7Type> } | undefined;
return `${tool.name}: ${tool.description}, args: ${JSON.stringify(
jsonSchema?.properties
)}`;
})
.join("\n");
}