Repository navigation
Expand file tree
/
Copy pathRunnableFunction.ts
More file actions
114 lines (102 loc) · 3.68 KB
/
Copy pathRunnableFunction.ts
File metadata and controls
114 lines (102 loc) · 3.68 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { type ChatCompletionRunner } from './ChatCompletionRunner';
import { type ChatCompletionStreamingRunner } from './ChatCompletionStreamingRunner';
import { JSONSchema } from './jsonschema';
type PromiseOrValue<T> = T | Promise<T>;
export type RunnableFunctionWithParse<Args extends object> = {
/**
* @param args the return value from `parse`.
* @param runner the runner evaluating this callback.
* @returns a string to send back to OpenAI.
*/
function: (
args: Args,
runner: ChatCompletionRunner<unknown> | ChatCompletionStreamingRunner<unknown>,
) => PromiseOrValue<unknown>;
/**
* @param input the raw args from the OpenAI function call.
* @returns the parsed arguments to pass to `function`
*/
parse: (input: string) => PromiseOrValue<Args>;
/**
* The parameters the function accepts, describes as a JSON Schema object.
*/
parameters: JSONSchema;
/**
* A description of what the function does, used by the model to choose when and how to call the function.
*/
description: string;
/**
* The name of the function to be called. Will default to function.name if omitted.
*/
name?: string | undefined;
strict?: boolean | undefined;
};
export type RunnableFunctionWithoutParse = {
/**
* @param args the raw args from the OpenAI function call.
* @returns a string to send back to OpenAI
*/
function: (
args: string,
runner: ChatCompletionRunner<unknown> | ChatCompletionStreamingRunner<unknown>,
) => PromiseOrValue<unknown>;
/**
* The parameters the function accepts, describes as a JSON Schema object.
*/
parameters: JSONSchema;
/**
* A description of what the function does, used by the model to choose when and how to call the function.
*/
description: string;
/**
* The name of the function to be called. Will default to function.name if omitted.
*/
name?: string | undefined;
strict?: boolean | undefined;
};
export type RunnableFunction<Args extends object | string> =
Args extends string ? RunnableFunctionWithoutParse
: Args extends object ? RunnableFunctionWithParse<Args>
: never;
export type RunnableToolFunction<Args extends object | string> =
Args extends string ? RunnableToolFunctionWithoutParse
: Args extends object ? RunnableToolFunctionWithParse<Args>
: never;
export type RunnableToolFunctionWithoutParse = {
type: 'function';
function: RunnableFunctionWithoutParse;
};
export type RunnableToolFunctionWithParse<Args extends object> = {
type: 'function';
function: RunnableFunctionWithParse<Args>;
};
export function isRunnableFunctionWithParse<Args extends object>(
fn: any,
): fn is RunnableFunctionWithParse<Args> {
return typeof (fn as any).parse === 'function';
}
export type BaseFunctionsArgs = readonly (object | string)[];
export type RunnableFunctions<FunctionsArgs extends BaseFunctionsArgs> =
[any[]] extends [FunctionsArgs] ? readonly RunnableFunction<any>[]
: {
[Index in keyof FunctionsArgs]: Index extends number ? RunnableFunction<FunctionsArgs[Index]>
: FunctionsArgs[Index];
};
export type RunnableTools<FunctionsArgs extends BaseFunctionsArgs> =
[any[]] extends [FunctionsArgs] ? readonly RunnableToolFunction<any>[]
: {
[Index in keyof FunctionsArgs]: Index extends number ? RunnableToolFunction<FunctionsArgs[Index]>
: FunctionsArgs[Index];
};
/**
* This is helper class for passing a `function` and `parse` where the `function`
* argument type matches the `parse` return type.
*/
export class ParsingToolFunction<Args extends object> {
type: 'function';
function: RunnableFunctionWithParse<Args>;
constructor(input: RunnableFunctionWithParse<Args>) {
this.type = 'function';
this.function = input;
}
}