2024-04-16 19:50:10 +02:00
|
|
|
/**
|
|
|
|
* A service that provides an AI client to create a completion.
|
|
|
|
*/
|
|
|
|
|
2024-04-17 23:04:51 +02:00
|
|
|
const { ChatOpenAI } = require("langchain/chat_models/openai");
|
|
|
|
const { ChatAnthropic } = require("langchain/chat_models/anthropic");
|
|
|
|
|
2024-04-16 19:50:10 +02:00
|
|
|
class Provider {
|
|
|
|
_client;
|
|
|
|
constructor(client) {
|
|
|
|
if (this.constructor == Provider) {
|
|
|
|
throw new Error("Class is of abstract type and can't be instantiated");
|
|
|
|
}
|
|
|
|
this._client = client;
|
|
|
|
}
|
|
|
|
|
|
|
|
get client() {
|
|
|
|
return this._client;
|
|
|
|
}
|
2024-04-17 23:04:51 +02:00
|
|
|
|
|
|
|
static LangChainChatModel(provider = "openai", config = {}) {
|
|
|
|
switch (provider) {
|
|
|
|
case "openai":
|
|
|
|
return new ChatOpenAI({
|
|
|
|
openAIApiKey: process.env.OPEN_AI_KEY,
|
|
|
|
...config,
|
|
|
|
});
|
|
|
|
case "anthropic":
|
|
|
|
return new ChatAnthropic({
|
|
|
|
anthropicApiKey: process.env.ANTHROPIC_API_KEY,
|
|
|
|
...config,
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
return new ChatOpenAI({
|
|
|
|
openAIApiKey: process.env.OPEN_AI_KEY,
|
|
|
|
...config,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static contextLimit(provider = "openai") {
|
|
|
|
switch (provider) {
|
|
|
|
case "openai":
|
|
|
|
return 8_000;
|
|
|
|
case "anthropic":
|
|
|
|
return 100_000;
|
|
|
|
default:
|
|
|
|
return 8_000;
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 19:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Provider;
|