@gluneau/n8n-nodes-venice
Version:
Venice.ai integration for n8n
178 lines • 6.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatVenice = void 0;
const chat_models_1 = require("@langchain/core/language_models/chat_models");
const messages_1 = require("@langchain/core/messages");
const env_1 = require("@langchain/core/utils/env");
class ChatVenice extends chat_models_1.BaseChatModel {
apiKey;
modelName;
temperature;
maxTokens;
topP;
frequencyPenalty;
presencePenalty;
n;
stop;
seed;
streaming;
maxRetries;
baseUrl;
repetitionPenalty;
topK;
minP;
maxTemp;
minTemp;
veniceParameters;
responseFormat;
constructor(fields) {
super(fields ?? {});
this.apiKey = fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("VENICE_API_KEY") ?? "";
if (!this.apiKey) {
throw new Error("Venice API key is required");
}
this.modelName = fields?.modelName ?? "llama-3.3-70b";
this.temperature = fields?.temperature ?? 0.15;
this.maxTokens = fields?.maxTokens ?? 1024;
this.topP = fields?.topP ?? 0.9;
this.frequencyPenalty = fields?.frequencyPenalty ?? 0;
this.presencePenalty = fields?.presencePenalty ?? 0;
this.n = fields?.n ?? 1;
this.stop = fields?.stop;
this.seed = fields?.seed;
this.streaming = fields?.streaming ?? false;
this.maxRetries = fields?.maxRetries ?? 2;
this.baseUrl = fields?.baseUrl ?? "https://api.venice.ai/api/v1";
this.repetitionPenalty = fields?.repetitionPenalty;
this.topK = fields?.topK;
this.minP = fields?.minP;
this.maxTemp = fields?.maxTemp;
this.minTemp = fields?.minTemp;
this.veniceParameters = fields?.veniceParameters;
this.responseFormat = fields?.responseFormat;
}
_llmType() {
return "venice";
}
get identifyingParams() {
return {
model_name: this.modelName,
temperature: this.temperature,
max_tokens: this.maxTokens,
top_p: this.topP,
frequency_penalty: this.frequencyPenalty,
presence_penalty: this.presencePenalty,
n: this.n,
};
}
messagesToVeniceFormat(messages) {
return messages.map((message) => {
const role = messageTypeToVeniceRole(message._getType());
const content = typeof message.content === "string" ? message.content : JSON.stringify(message.content);
return { role, content };
});
}
async _generate(messages, options, runManager) {
const messageList = this.messagesToVeniceFormat(messages);
const params = {
model: this.modelName,
messages: messageList,
temperature: this.temperature,
max_tokens: this.maxTokens,
top_p: this.topP,
frequency_penalty: this.frequencyPenalty,
presence_penalty: this.presencePenalty,
n: this.n,
stream: this.streaming,
};
if (this.stop)
params.stop = this.stop;
if (this.seed !== undefined)
params.seed = this.seed;
if (this.repetitionPenalty !== undefined)
params.repetition_penalty = this.repetitionPenalty;
if (this.topK !== undefined)
params.top_k = this.topK;
if (this.minP !== undefined)
params.min_p = this.minP;
if (this.maxTemp !== undefined)
params.max_temp = this.maxTemp;
if (this.minTemp !== undefined)
params.min_temp = this.minTemp;
if (this.veniceParameters) {
params.venice_parameters = {};
if (this.veniceParameters.characterSlug) {
params.venice_parameters.character_slug = this.veniceParameters.characterSlug;
}
if (this.veniceParameters.enableWebSearch) {
params.venice_parameters.enable_web_search = this.veniceParameters.enableWebSearch;
}
if (this.veniceParameters.includeVeniceSystemPrompt !== undefined) {
params.venice_parameters.include_venice_system_prompt = this.veniceParameters.includeVeniceSystemPrompt;
}
}
if (this.responseFormat && this.responseFormat.type) {
params.response_format = { type: this.responseFormat.type };
if (this.responseFormat.type === 'json_schema' && this.responseFormat.jsonSchema) {
params.response_format.json_schema =
typeof this.responseFormat.jsonSchema === 'string'
? JSON.parse(this.responseFormat.jsonSchema)
: this.responseFormat.jsonSchema;
}
}
try {
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify(params),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Venice API returned an error: ${text}`);
}
const json = await response.json();
const generations = (json.choices || []).map((choice) => ({
text: choice.message?.content || "",
message: new messages_1.AIMessage(choice.message?.content || ""),
generationInfo: {
finishReason: choice.finish_reason,
model: json.model,
...choice,
},
}));
return {
generations,
llmOutput: {
model: json.model,
usage: json.usage,
id: json.id,
created: json.created,
},
};
}
catch (error) {
console.error("Error calling Venice API:", error);
throw error;
}
}
async getNumTokensFromMessages(messages) {
throw new Error("getNumTokensFromMessages not implemented for Venice");
}
}
exports.ChatVenice = ChatVenice;
function messageTypeToVeniceRole(type) {
switch (type) {
case "system":
return "system";
case "human":
return "user";
case "ai":
return "assistant";
default:
return "user";
}
}
//# sourceMappingURL=VeniceChatModel.js.map