adds very basic support for OpenAI function calling

This commit is contained in:
nai-degen
2024-01-24 16:42:26 -06:00
parent 935a633325
commit 79b2e5b6fd
3 changed files with 26 additions and 1 deletions
+3 -1
View File
@@ -194,7 +194,9 @@ export function getCompletionFromBody(req: Request, body: Record<string, any>) {
switch (format) {
case "openai":
case "mistral-ai":
return body.choices[0].message.content;
// Can be null if the model wants to invoke tools rather than return a
// completion.
return body.choices[0].message.content || "";
case "openai-text":
return body.choices[0].text;
case "anthropic":
@@ -92,7 +92,21 @@ export const OpenAIV1ChatCompletionSchema = z
// special cased it in `addAzureKey` rather than expecting clients to do it.
logprobs: z.boolean().optional().default(false),
top_logprobs: z.number().int().optional(),
// Quickly adding some newer tool usage params, not tested. They will be
// passed through to the API as-is.
tools: z.array(z.any()).optional(),
functions: z.array(z.any()).optional(),
tool_choice: z.any().optional(),
function_choice: z.any().optional(),
response_format: z.any(),
})
// Tool usage must be enabled via config because we currently have no way to
// track quota usage for them or enforce limits.
.omit(
Boolean(config.allowOpenAIToolUsage)
? {}
: { tools: true, functions: true }
)
.strip();
export type OpenAIChatMessage = z.infer<