-
Notifications
You must be signed in to change notification settings - Fork 2k
/
openai-format-fndef.ts
131 lines (122 loc) Β· 3.35 KB
/
openai-format-fndef.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Formatting function definitions for calculating openai function defination token usage.
*
* https://github.com/hmarr/openai-chat-tokens/blob/main/src/functions.ts
* (c) 2023 Harry Marr
* MIT license
*/
import { FunctionDefinition } from "@azure/openai";
export interface FunctionDef extends Omit<FunctionDefinition, "parameters"> {
name: string;
description?: string;
parameters: ObjectProp;
}
interface ObjectProp {
type: "object";
properties?: {
[key: string]: Prop;
};
required?: string[];
}
interface AnyOfProp {
anyOf: Prop[];
}
type Prop = {
description?: string;
} & (
| AnyOfProp
| ObjectProp
| {
type: "string";
enum?: string[];
}
| {
type: "number" | "integer";
minimum?: number;
maximum?: number;
enum?: number[];
}
| { type: "boolean" }
| { type: "null" }
| {
type: "array";
items?: Prop;
}
);
function isAnyOfProp(prop: Prop): prop is AnyOfProp {
return (
(prop as AnyOfProp).anyOf !== undefined &&
Array.isArray((prop as AnyOfProp).anyOf)
);
}
// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
// This function converts the JSON schemas into TypeScript definitions.
export function formatFunctionDefinitions(functions: FunctionDef[]) {
const lines = ["namespace functions {", ""];
for (const f of functions) {
if (f.description) {
lines.push(`// ${f.description}`);
}
if (Object.keys(f.parameters.properties ?? {}).length > 0) {
lines.push(`type ${f.name} = (_: {`);
lines.push(formatObjectProperties(f.parameters, 0));
lines.push("}) => any;");
} else {
lines.push(`type ${f.name} = () => any;`);
}
lines.push("");
}
lines.push("} // namespace functions");
return lines.join("\n");
}
// Format just the properties of an object (not including the surrounding braces)
function formatObjectProperties(obj: ObjectProp, indent: number): string {
const lines: string[] = [];
for (const [name, param] of Object.entries(obj.properties ?? {})) {
if (param.description && indent < 2) {
lines.push(`// ${param.description}`);
}
if (obj.required?.includes(name)) {
lines.push(`${name}: ${formatType(param, indent)},`);
} else {
lines.push(`${name}?: ${formatType(param, indent)},`);
}
}
return lines.map((line) => " ".repeat(indent) + line).join("\n");
}
// Format a single property type
function formatType(param: Prop, indent: number): string {
if (isAnyOfProp(param)) {
return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
}
switch (param.type) {
case "string":
if (param.enum) {
return param.enum.map((v) => `"${v}"`).join(" | ");
}
return "string";
case "number":
if (param.enum) {
return param.enum.map((v) => `${v}`).join(" | ");
}
return "number";
case "integer":
if (param.enum) {
return param.enum.map((v) => `${v}`).join(" | ");
}
return "number";
case "boolean":
return "boolean";
case "null":
return "null";
case "object":
return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
case "array":
if (param.items) {
return `${formatType(param.items, indent)}[]`;
}
return "any[]";
default:
return "";
}
}