forked from hmarr/openai-chat-tokens
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.ts
131 lines (122 loc) · 3.38 KB
/
functions.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
import type OpenAI from "openai"
type OpenAIFunction = OpenAI.Chat.ChatCompletionCreateParams.Function
/** Types representing the OpenAI function definitions. While the OpenAI client
* library does have types for function definitions, the properties are just
* Record<string, unknown>, which isn't very useful for type checking this
* formatting code. */
export interface FunctionDef extends Omit<OpenAIFunction, "parameters"> {
readonly name: string
readonly description?: string
readonly parameters: ObjectProp
}
export interface ObjectProp {
readonly type: "object"
readonly properties?: Record<string, Prop>
readonly required?: string[]
}
export interface AnyOfProp {
readonly anyOf: Prop[]
}
export type Prop = {
description?: string
} & (
| AnyOfProp
| ObjectProp
| {
type: "array"
items?: Prop
}
| {
type: "integer" | "number"
minimum?: number
maximum?: number
enum?: number[]
}
| {
type: "string"
enum?: string[]
}
| { type: "boolean" }
| { type: "null" }
)
export function isAnyOfProp(prop: Prop): prop is AnyOfProp {
return (
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
(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[]): string {
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) */
export function formatObjectProperties(
obj: ObjectProp,
indent: number,
): string {
const lines = []
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 */
export 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.toString()}"`).join(" | ")
}
return "string"
case "number":
if (param.enum) {
return param.enum.map(v => v.toString()).join(" | ")
}
return "number"
case "integer":
if (param.enum) {
return param.enum.map(v => v.toString()).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[]"
}
}