adds very basic support for OpenAI function calling
This commit is contained in:
parent
935a633325
commit
79b2e5b6fd
|
@ -236,6 +236,14 @@ type Config = {
|
||||||
* Defaults to 1, as most deployments are on HuggingFace or Cloudflare Tunnel.
|
* Defaults to 1, as most deployments are on HuggingFace or Cloudflare Tunnel.
|
||||||
*/
|
*/
|
||||||
trustedProxies?: number;
|
trustedProxies?: number;
|
||||||
|
/**
|
||||||
|
* Whether to allow OpenAI tool usage. The proxy doesn't impelment any
|
||||||
|
* support for tools/function calling but can pass requests and responses as
|
||||||
|
* is. Note that the proxy also cannot accurately track quota usage for
|
||||||
|
* requests involving tools, so you must opt in to this feature at your own
|
||||||
|
* risk.
|
||||||
|
*/
|
||||||
|
allowOpenAIToolUsage?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
// To change configs, create a file called .env in the root directory.
|
// To change configs, create a file called .env in the root directory.
|
||||||
|
@ -326,6 +334,7 @@ export const config: Config = {
|
||||||
useInsecureCookies: getEnvWithDefault("USE_INSECURE_COOKIES", isDev),
|
useInsecureCookies: getEnvWithDefault("USE_INSECURE_COOKIES", isDev),
|
||||||
staticServiceInfo: getEnvWithDefault("STATIC_SERVICE_INFO", false),
|
staticServiceInfo: getEnvWithDefault("STATIC_SERVICE_INFO", false),
|
||||||
trustedProxies: getEnvWithDefault("TRUSTED_PROXIES", 1),
|
trustedProxies: getEnvWithDefault("TRUSTED_PROXIES", 1),
|
||||||
|
allowOpenAIToolUsage: getEnvWithDefault("ALLOW_OPENAI_TOOL_USAGE", false),
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
function generateCookieSecret() {
|
function generateCookieSecret() {
|
||||||
|
|
|
@ -194,7 +194,9 @@ export function getCompletionFromBody(req: Request, body: Record<string, any>) {
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case "openai":
|
case "openai":
|
||||||
case "mistral-ai":
|
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":
|
case "openai-text":
|
||||||
return body.choices[0].text;
|
return body.choices[0].text;
|
||||||
case "anthropic":
|
case "anthropic":
|
||||||
|
|
|
@ -92,7 +92,21 @@ export const OpenAIV1ChatCompletionSchema = z
|
||||||
// special cased it in `addAzureKey` rather than expecting clients to do it.
|
// special cased it in `addAzureKey` rather than expecting clients to do it.
|
||||||
logprobs: z.boolean().optional().default(false),
|
logprobs: z.boolean().optional().default(false),
|
||||||
top_logprobs: z.number().int().optional(),
|
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();
|
.strip();
|
||||||
|
|
||||||
export type OpenAIChatMessage = z.infer<
|
export type OpenAIChatMessage = z.infer<
|
||||||
|
|
Loading…
Reference in New Issue