mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
28 lines
653 B
TypeScript
28 lines
653 B
TypeScript
import OpenAI from 'openai';
|
|
import { Tool } from './Tool';
|
|
|
|
/**
|
|
* Convert a custom Tool to OpenAI's ChatCompletionTool format
|
|
*/
|
|
export function toolToOpenAI<TParams>(name: string, tool: Tool<TParams>): OpenAI.Chat.Completions.ChatCompletionTool {
|
|
const functionDef: {
|
|
name: string;
|
|
description: string;
|
|
parameters?: Record<string, unknown>;
|
|
} = {
|
|
name,
|
|
description: tool.description,
|
|
};
|
|
|
|
if (tool.inputSchema) {
|
|
functionDef.parameters = tool.inputSchema.toJSONSchema();
|
|
} else {
|
|
functionDef.parameters = { type: "object", properties: {} };
|
|
}
|
|
|
|
return {
|
|
type: 'function',
|
|
function: functionDef
|
|
};
|
|
}
|