This repository was archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIOpenAiFunction.ts
217 lines (204 loc) · 6.73 KB
/
IOpenAiFunction.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { IOpenAiSchema } from "./IOpenAiSchema";
import { ISwaggerMigrateRoute } from "./ISwaggerMigrateRoute";
import { ISwaggerOperation } from "./ISwaggerOperation";
/**
* OpenAI function metadata.
*
* `IOpenAiFunction` is a data structure representing a function,
* which is provided by Restful API, and used for the OpenAI function call.
*
* If you provide this `IOpenAiFunction` instance to the OpenAI, the OpenAI
* will construct arguments by conversating with the user. Also, you can
* execute the function call with the OpenAI constructed arguments by using
* {@link OpenAiFetcher.execute}.
*
* For reference, different between `IOpenAiFunction` and its origin source
* {@link ISwaggerOperation} is, `IOpenAiFunction` has converted every type schema
* informations from {@link ISwaggerSchema} to {@link IOpenAiSchema} to escape
* {@link ISwaggerSchema.IReference reference types}, and downgrade the version of
* the JSON schema to OpenAPI 3.0. It's because OpenAI function call feature cannot
* understand both reference types and OpenAPI 3.1 specification.
*
* Additionally, if you've composed `IOpenAiFunction` with
* {@link IOpenAiDocument.IOptions.keyword} configuration (as `true`), number of
* {@link IOpenAiFunction.parameters} are always 1 and the first parameter's type is
* always {@link IOpenAiSchema.IObject}. The properties' rule is:
*
* - `pathParameters`: Path parameters of {@link ISwaggerMigrateRoute.parameters}
* - `query`: Query parameter of {@link ISwaggerMigrateRoute.query}
* - `body`: Body parameter of {@link ISwaggerMigrateRoute.body}
*
* ```typescript
* {
* ...pathParameters,
* query,
* body,
* }
* ```
*
* Otherwise, the parameters would be multiple, and the sequence of the parameters
* are following below rules:
*
* ```typescript
* [
* ...pathParameters,
* ...(query ? [query] : []),
* ...(body ? [body] : []),
* ]
* ```
*
* @reference https://platform.openai.com/docs/guides/function-calling
* @author Samchon
*/
export interface IOpenAiFunction {
/**
* HTTP method of the endpoint.
*/
method: "get" | "post" | "patch" | "put" | "delete";
/**
* Path of the endpoint.
*/
path: string;
/**
* Representative name of the function.
*
* The `name` is a repsentative name identifying the function in the
* {@link IOpenAiDocument}. The `name` value is just composed by joining the
* {@link IMigrateRoute.accessor} by underscore `_` character.
*
* Here is the composition rule of the {@link IMigrateRoute.accessor}:
*
* > The `accessor` is composed with the following rules. At first, namespaces
* > are composed by static directory names in the {@link path}. Parametric
* > symbols represented by `:param` or `{param}` cannot be a part of the
* > namespace.
* >
* > Instead, they would be a part of the function name. The function
* > name is composed with the {@link method HTTP method} and parametric symbols
* > like `getByParam` or `postByParam`. If there are multiple path parameters,
* > they would be concatenated by `And` like `getByParam1AndParam2`.
* >
* > For refefence, if the {@link operation}'s {@link method} is `delete`, the
* > function name would be replaced to `erase` instead of `delete`. It is
* > the reason why the `delete` is a reserved keyword in many programming
* > languages.
* >
* > - Example 1
* > - path: `POST /shopping/sellers/sales`
* > - accessor: `shopping.sellers.sales.post`
* > - Example 2
* > - endpoint: `GET /shoppings/sellers/sales/:saleId/reviews/:reviewId/comments/:id
* > - accessor: `shoppings.sellers.sales.reviews.getBySaleIdAndReviewIdAndCommentId`
*/
name: string;
/**
* Whether the function schema types are strict or not.
*
* Newly added specification at 2024-08-07.
*
* @reference https://openai.com/index/introducing-structured-outputs-in-the-api/
*/
strict: true;
/**
* List of parameter schemas.
*
* If you've configured {@link IOpenAiDocument.IOptions.keyword} (as `true`),
* number of {@link IOpenAiFunction.parameters} are always 1 and the first parameter's
* type is always {@link IOpenAiSchema.IObject}. The properties' rule is:
*
* - `pathParameters`: Path parameters of {@link IMigrateRoute.parameters}
* - `query`: Query parameter of {@link IMigrateRoute.query}
* - `body`: Body parameter of {@link IMigrateRoute.body}
*
* ```typescript
* {
* ...pathParameters,
* query,
* body,
* }
* ```
*
* Otherwise, the parameters would be multiple, and the sequence of the parameters
* are following below rules:
*
* ```typescript
* [
* ...pathParameters,
* ...(query ? [query] : []),
* ...(body ? [body] : []),
* ]
* ```
*/
parameters: IOpenAiSchema[];
/**
* Collection of separated parameters.
*
* Filled only when {@link IOpenAiDocument.IOptions.separate} has been configured.
*/
separated?: IOpenAiFunction.ISeparated;
/**
* Expected return type.
*
* If the function returns nothing (`void`), then the output is `undefined`.
*/
output?: IOpenAiSchema | undefined;
/**
* Description of the function.
*
* Composed by such rule:
*
* 1. Starts from the {@link OpenApi.IOperation.summary} paragraph.
* 2. The next paragraphs are filled with the {@link OpenApi.IOperation.description}.
* By the way, if the first paragraph of {@link OpenApi.IOperation.description} is same
* with the {@link OpenApi.IOperation.summary}, it would not be duplicated.
* 3. Parameters' descriptions are added with `@param` tag.
* 4. {@link OpenApi.IOperation.security Security requirements} are added with `@security` tag.
* 5. Tag names are added with `@tag` tag.
* 6. If {@link OpenApi.IOperation.deprecated}, `@deprecated` tag is added.
*/
description?: string;
/**
* Get the Swagger operation metadata.
*
* Get the Swagger operation metadata, of the source.
*
* @returns Swagger operation metadata.
*/
operation: () => ISwaggerOperation;
/**
* Get the migration route metadata.
*
* Get the migration route metadata, of the source.
*
* @returns Migration route metadata.
*/
route: () => ISwaggerMigrateRoute;
}
export namespace IOpenAiFunction {
/**
* Collection of separated parameters.
*/
export interface ISeparated {
/**
* Parameters that would be composed by the OpenAI.
*/
llm: ISeparatedParameter[];
/**
* Parameters that would be composed by the human.
*/
human: ISeparatedParameter[];
}
/**
* Separated parameter.
*/
export interface ISeparatedParameter {
/**
* Index of the parameter.
*/
index: number;
/**
* Type schema info of the parameter.
*/
schema: IOpenAiSchema;
}
}