@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
166 lines (165 loc) • 7.31 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAIProviderError = exports.OpenAIProvider = void 0;
const config_1 = require("./config");
const chat_1 = require("./chat");
const streaming_1 = require("./streaming");
const embedding_1 = require("./embedding");
const tools_1 = require("./tools");
const BaseProvider_1 = require("../BaseProvider");
const cache_1 = require("./cache");
const errors_1 = require("./errors");
/**
* OpenAIProvider class that extends BaseProvider and implements EmbeddingProvider.
* This provider interacts with the OpenAI API for various AI-related tasks.
*/
class OpenAIProvider extends BaseProvider_1.BaseProvider {
/**
* Creates an instance of OpenAIProvider.
* @param {OpenAIProviderConfig} config - The configuration for the OpenAI provider.
* @param {ILogger} logger - The logger instance for logging.
*/
constructor(config, logger) {
super(logger);
this.config = new config_1.OpenAIConfig(config, logger);
this.cache = new cache_1.OpenAICache();
}
/**
* Sends a chat request to the OpenAI API.
* @param {ChatMessage[]} messages - The chat messages to send.
* @param {ChatOptions} options - The options for the chat request.
* @param {Tool[]} [tools] - Optional tools to use in the chat.
* @returns {Promise<ProviderResponse>} The response from the API.
* @throws {OpenAIProviderError} If there's an error during the API call.
*/
async chat(messages, options, tools) {
try {
const cacheKey = { messages, options, tools };
if (this.cache.has(cacheKey)) {
this.logger.debug('Cache hit for chat request');
return this.cache.get(cacheKey);
}
const processedMessages = await this.applyMiddleware(messages);
const response = await (0, chat_1.chat)(this.config, processedMessages, options, tools);
const processedResponse = await this.applyMiddlewareToResponse(response);
if (processedResponse.toolCalls) {
processedResponse.toolCalls = await this.applyMiddlewareToToolCalls(processedResponse.toolCalls);
}
this.cache.set(cacheKey, processedResponse);
return processedResponse;
}
catch (error) {
this.logger.error('Error in OpenAIProvider chat', { error: error instanceof Error ? error.message : String(error) });
throw (0, errors_1.handleOpenAIError)(error);
}
}
/**
* Sends a streaming chat request to the OpenAI API.
* @param {ChatMessage[]} messages - The chat messages to send.
* @param {ChatOptions} options - The options for the chat request.
* @param {Tool[]} [tools] - Optional tools to use in the chat.
* @returns {AsyncIterableIterator<ProviderResponse>} An async iterator of responses from the API.
* @throws {OpenAIProviderError} If there's an error during the API call.
*/
async *streamChat(messages, options, tools) {
try {
const processedMessages = await this.applyMiddleware(messages);
for await (const response of (0, streaming_1.streamChat)(this.config, processedMessages, options, tools)) {
const processedResponse = await this.applyMiddlewareToResponse(response);
if (processedResponse.toolCalls) {
processedResponse.toolCalls = await this.applyMiddlewareToToolCalls(processedResponse.toolCalls);
}
yield processedResponse;
}
}
catch (error) {
this.logger.error('Error in OpenAIProvider streamChat', { error: error instanceof Error ? error.message : String(error) });
throw (0, errors_1.handleOpenAIError)(error);
}
}
/**
* Returns the capabilities of the OpenAI provider.
* @returns {ProviderCapabilities} The capabilities of the provider.
*/
getCapabilities() {
return {
maxTokens: 4096,
supportsFunctionCalling: true,
supportsStreaming: true,
supportedModels: ['gpt-4o', 'o1-preview'],
maxSimultaneousCalls: 1,
supportsSemanticCaching: false,
};
}
/**
* Generates an embedding for the given text.
* @param {string} text - The text to embed.
* @returns {Promise<number[]>} The embedding as an array of numbers.
* @throws {OpenAIProviderError} If there's an error during the API call.
*/
async embed(text) {
try {
return await (0, embedding_1.embed)(this.config, text);
}
catch (error) {
this.logger.error('Error in OpenAIProvider embed', { error: error instanceof Error ? error.message : String(error) });
throw (0, errors_1.handleOpenAIError)(error);
}
}
/**
* Generates embeddings for the given texts.
* @param {string[]} texts - The texts to embed.
* @returns {Promise<number[][]>} The embeddings as an array of number arrays.
* @throws {OpenAIProviderError} If there's an error during the API call.
*/
async embedBatch(texts) {
try {
return await (0, embedding_1.embedBatch)(this.config, texts);
}
catch (error) {
this.logger.error('Error in OpenAIProvider embedBatch', { error: error instanceof Error ? error.message : String(error) });
throw (0, errors_1.handleOpenAIError)(error);
}
}
/**
* Converts a generic tool schema to the OpenAI-specific format.
* @param {GenericToolSchema} schema - The generic tool schema to convert.
* @returns {any} The converted schema in OpenAI format.
*/
convertToolSchema(schema) {
return (0, tools_1.convertToolSchema)(schema);
}
/**
* Converts an OpenAI tool call to the standardized format.
* @param {any} call - The OpenAI tool call to convert.
* @returns {StandardizedToolCall} The converted tool call in standardized format.
*/
convertToolCall(call) {
return (0, tools_1.convertToolCall)(call);
}
/**
* Clears the internal cache of the provider.
*/
clearCache() {
this.cache.clear();
this.logger.debug('OpenAIProvider cache cleared');
}
}
exports.OpenAIProvider = OpenAIProvider;
__exportStar(require("./types"), exports);
var errors_2 = require("./errors");
Object.defineProperty(exports, "OpenAIProviderError", { enumerable: true, get: function () { return errors_2.OpenAIProviderError; } });