UNPKG

mongodb-rag-core

Version:

Common elements used by MongoDB Chatbot Framework components.

61 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeOpenAiChatLlm = void 0; const assert_1 = require("assert"); /** Construct the {@link ChatLlm} service using the [OpenAI client](https://www.npmjs.com/package/openai). */ function makeOpenAiChatLlm({ deployment, openAiClient, openAiLmmConfigOptions, tools, }) { const toolDict = {}; tools?.forEach((tool) => { const name = tool.definition.name; toolDict[name] = tool; }); return { async answerQuestionStream({ messages, toolCallOptions, }) { const completionStream = await openAiClient.chat.completions.create({ model: deployment, messages, ...(openAiLmmConfigOptions ?? {}), ...(toolCallOptions ? { function_call: toolCallOptions } : {}), functions: tools?.map((tool) => tool.definition), stream: true, }); return completionStream; }, async answerQuestionAwaited({ messages, toolCallOptions, }) { const { choices: [choice], } = await openAiClient.chat.completions.create({ model: deployment, messages, ...(openAiLmmConfigOptions ?? {}), ...(toolCallOptions ? { function_call: toolCallOptions } : {}), functions: tools?.map((tool) => tool.definition), stream: false, }); const { message } = choice; if (!message) { throw new Error("No message returned from OpenAI"); } return message; }, async callTool({ messages, conversation, dataStreamer, request }) { const lastMessage = messages[messages.length - 1]; // Only call tool if the message is an assistant message with a function call. (0, assert_1.strict)(lastMessage.role === "assistant" && lastMessage.function_call !== undefined, `Message must be a tool call`); (0, assert_1.strict)(lastMessage.function_call !== null, `Function call must be defined`); (0, assert_1.strict)(Object.keys(toolDict).includes(lastMessage.function_call.name), `Tool not found`); const { function_call } = lastMessage; const tool = toolDict[function_call.name]; const toolResponse = await tool.call({ functionArgs: JSON.parse(function_call.arguments), conversation, dataStreamer, request, }); return toolResponse; }, }; } exports.makeOpenAiChatLlm = makeOpenAiChatLlm; //# sourceMappingURL=OpenAiChatLlm.js.map