mongodb-rag-core
Version:
Common elements used by MongoDB Chatbot Framework components.
44 lines • 1.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOpenAiFunctionResponse = void 0;
const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
async function getOpenAiFunctionResponse({ messages, llmOptions, schema, functionName, functionDescription, openAiClient, }) {
const parameters = (0, zod_to_json_schema_1.default)(schema, {
$refStrategy: "none",
});
const { ...createChatCompletionParams } = llmOptions;
const res = await openAiClient.chat.completions.create({
messages,
...createChatCompletionParams,
tool_choice: {
type: "function",
function: {
name: functionName,
},
},
tools: [
{
type: "function",
function: {
name: functionName,
description: functionDescription,
parameters,
},
},
],
});
return parseObjectFromOpenAiResponse(res, functionName, schema);
}
exports.getOpenAiFunctionResponse = getOpenAiFunctionResponse;
function parseObjectFromOpenAiResponse(response, functionName, schema) {
const toolCall = response.choices[0].message.tool_calls?.[0];
if (!toolCall || toolCall.function.name !== functionName) {
throw new Error("Unexpected response format from OpenAI");
}
const descriptions = JSON.parse(toolCall.function.arguments);
return schema.parse(descriptions);
}
//# sourceMappingURL=getOpenAiFunctionResponse.js.map