-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
function_calling.ts
34 lines (32 loc) · 1.12 KB
/
function_calling.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
import { zodToJsonSchema } from "zod-to-json-schema";
import { StructuredToolInterface } from "../tools.js";
import { FunctionDefinition, ToolDefinition } from "../language_models/base.js";
/**
* Formats a `StructuredTool` instance into a format that is compatible
* with OpenAI function calling. It uses the `zodToJsonSchema`
* function to convert the schema of the `StructuredTool` into a JSON
* schema, which is then used as the parameters for the OpenAI function.
*/
export function convertToOpenAIFunction(
tool: StructuredToolInterface
): FunctionDefinition {
return {
name: tool.name,
description: tool.description,
parameters: zodToJsonSchema(tool.schema),
};
}
/**
* Formats a `StructuredTool` instance into a format that is compatible
* with OpenAI tool calling. It uses the `zodToJsonSchema`
* function to convert the schema of the `StructuredTool` into a JSON
* schema, which is then used as the parameters for the OpenAI tool.
*/
export function convertToOpenAITool(
tool: StructuredToolInterface
): ToolDefinition {
return {
type: "function",
function: convertToOpenAIFunction(tool),
};
}