@hashbrownai/openai
Version:
OpenAI provider for Hashbrown AI
134 lines • 5.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.text = text;
const tslib_1 = require("tslib");
const core_1 = require("@hashbrownai/core");
const openai_1 = require("openai");
function text(options) {
return tslib_1.__asyncGenerator(this, arguments, function* text_1() {
var _a, e_1, _b, _c;
const { apiKey, request, transformRequestOptions } = options;
const { messages, model, tools, responseFormat, toolChoice, system } = request;
const openai = new openai_1.default({
apiKey,
});
try {
const baseOptions = {
stream: true,
model: model,
messages: [
{
role: 'system',
content: system,
},
...messages.map((message) => {
if (message.role === 'user') {
return {
role: message.role,
content: message.content,
};
}
if (message.role === 'assistant') {
return {
role: message.role,
content: message.content,
tool_calls: message.toolCalls && message.toolCalls.length > 0
? message.toolCalls.map((toolCall) => (Object.assign(Object.assign({}, toolCall), { type: 'function', function: Object.assign(Object.assign({}, toolCall.function), { arguments: JSON.stringify(toolCall.function.arguments) }) })))
: undefined,
};
}
if (message.role === 'tool') {
return {
role: message.role,
content: JSON.stringify(message.content),
tool_call_id: message.toolCallId,
};
}
throw new Error(`Invalid message role`);
}),
],
tools: tools && tools.length > 0
? tools.map((tool) => ({
type: 'function',
function: {
name: tool.name,
description: tool.description,
parameters: tool.parameters,
strict: true,
},
}))
: undefined,
tool_choice: toolChoice,
response_format: responseFormat
? {
type: 'json_schema',
json_schema: {
strict: true,
name: 'schema',
description: '',
schema: responseFormat,
},
}
: undefined,
};
const resolvedOptions = transformRequestOptions
? yield tslib_1.__await(transformRequestOptions(baseOptions))
: baseOptions;
const stream = openai.beta.chat.completions.stream(resolvedOptions);
try {
for (var _d = true, stream_1 = tslib_1.__asyncValues(stream), stream_1_1; stream_1_1 = yield tslib_1.__await(stream_1.next()), _a = stream_1_1.done, !_a; _d = true) {
_c = stream_1_1.value;
_d = false;
const chunk = _c;
const chunkMessage = {
choices: chunk.choices.map((choice) => ({
index: choice.index,
delta: {
content: choice.delta.content,
role: choice.delta.role,
toolCalls: choice.delta.tool_calls,
},
finishReason: choice.finish_reason,
})),
};
const frame = {
type: 'chunk',
chunk: chunkMessage,
};
yield yield tslib_1.__await((0, core_1.encodeFrame)(frame));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = stream_1.return)) yield tslib_1.__await(_b.call(stream_1));
}
finally { if (e_1) throw e_1.error; }
}
}
catch (error) {
if (error instanceof Error) {
const frame = {
type: 'error',
error: error.toString(),
stacktrace: error.stack,
};
yield yield tslib_1.__await((0, core_1.encodeFrame)(frame));
}
else {
const frame = {
type: 'error',
error: String(error),
};
yield yield tslib_1.__await((0, core_1.encodeFrame)(frame));
}
}
finally {
const frame = {
type: 'finish',
};
yield yield tslib_1.__await((0, core_1.encodeFrame)(frame));
}
});
}
//# sourceMappingURL=text.fn.js.map