2023-10-30 23:44:03 +01:00
|
|
|
const { v4 } = require("uuid");
|
2024-03-12 23:21:27 +01:00
|
|
|
const {
|
|
|
|
writeResponseChunk,
|
|
|
|
clientAbortedHandler,
|
|
|
|
} = require("../../helpers/chat/responses");
|
2024-05-17 02:25:05 +02:00
|
|
|
const { NativeEmbedder } = require("../../EmbeddingEngines/native");
|
2024-04-14 21:04:38 +02:00
|
|
|
|
2023-10-30 23:44:03 +01:00
|
|
|
class AnthropicLLM {
|
2024-01-17 21:59:25 +01:00
|
|
|
constructor(embedder = null, modelPreference = null) {
|
2023-10-30 23:44:03 +01:00
|
|
|
if (!process.env.ANTHROPIC_API_KEY)
|
|
|
|
throw new Error("No Anthropic API key was set.");
|
|
|
|
|
|
|
|
// Docs: https://www.npmjs.com/package/@anthropic-ai/sdk
|
|
|
|
const AnthropicAI = require("@anthropic-ai/sdk");
|
|
|
|
const anthropic = new AnthropicAI({
|
|
|
|
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
|
|
});
|
|
|
|
this.anthropic = anthropic;
|
2024-01-17 21:59:25 +01:00
|
|
|
this.model =
|
2024-03-06 23:57:47 +01:00
|
|
|
modelPreference || process.env.ANTHROPIC_MODEL_PREF || "claude-2.0";
|
2023-11-06 22:13:53 +01:00
|
|
|
this.limits = {
|
|
|
|
history: this.promptWindowLimit() * 0.15,
|
|
|
|
system: this.promptWindowLimit() * 0.15,
|
|
|
|
user: this.promptWindowLimit() * 0.7,
|
|
|
|
};
|
2023-10-30 23:44:03 +01:00
|
|
|
|
2024-05-17 02:25:05 +02:00
|
|
|
this.embedder = embedder ?? new NativeEmbedder();
|
2024-01-17 23:42:05 +01:00
|
|
|
this.defaultTemp = 0.7;
|
2023-10-30 23:44:03 +01:00
|
|
|
}
|
|
|
|
|
2023-11-14 00:07:30 +01:00
|
|
|
streamingEnabled() {
|
2024-05-02 01:52:28 +02:00
|
|
|
return "streamGetChatCompletion" in this;
|
2023-11-14 00:07:30 +01:00
|
|
|
}
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
promptWindowLimit() {
|
|
|
|
switch (this.model) {
|
2024-03-06 23:57:47 +01:00
|
|
|
case "claude-instant-1.2":
|
|
|
|
return 100_000;
|
|
|
|
case "claude-2.0":
|
2023-11-06 22:13:53 +01:00
|
|
|
return 100_000;
|
2024-03-06 23:57:47 +01:00
|
|
|
case "claude-2.1":
|
|
|
|
return 200_000;
|
|
|
|
case "claude-3-opus-20240229":
|
|
|
|
return 200_000;
|
|
|
|
case "claude-3-sonnet-20240229":
|
|
|
|
return 200_000;
|
2024-03-14 01:32:02 +01:00
|
|
|
case "claude-3-haiku-20240307":
|
|
|
|
return 200_000;
|
2024-06-20 19:13:53 +02:00
|
|
|
case "claude-3-5-sonnet-20240620":
|
|
|
|
return 200_000;
|
2023-11-06 22:13:53 +01:00
|
|
|
default:
|
2024-03-06 23:57:47 +01:00
|
|
|
return 100_000; // assume a claude-instant-1.2 model
|
2023-11-06 22:13:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidChatCompletionModel(modelName = "") {
|
2024-03-06 23:57:47 +01:00
|
|
|
const validModels = [
|
|
|
|
"claude-instant-1.2",
|
|
|
|
"claude-2.0",
|
|
|
|
"claude-2.1",
|
|
|
|
"claude-3-opus-20240229",
|
|
|
|
"claude-3-sonnet-20240229",
|
2024-03-14 01:32:02 +01:00
|
|
|
"claude-3-haiku-20240307",
|
2024-06-20 19:13:53 +02:00
|
|
|
"claude-3-5-sonnet-20240620",
|
2024-03-06 23:57:47 +01:00
|
|
|
];
|
2023-10-30 23:44:03 +01:00
|
|
|
return validModels.includes(modelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moderation can be done with Anthropic, but its not really "exact" so we skip it
|
|
|
|
// https://docs.anthropic.com/claude/docs/content-moderation
|
|
|
|
async isSafe(_input = "") {
|
|
|
|
// Not implemented so must be stubbed
|
|
|
|
return { safe: true, reasons: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
constructPrompt({
|
|
|
|
systemPrompt = "",
|
|
|
|
contextTexts = [],
|
|
|
|
chatHistory = [],
|
|
|
|
userPrompt = "",
|
|
|
|
}) {
|
2024-03-06 23:57:47 +01:00
|
|
|
const prompt = {
|
|
|
|
role: "system",
|
|
|
|
content: `${systemPrompt}${this.#appendContext(contextTexts)}`,
|
|
|
|
};
|
|
|
|
|
|
|
|
return [prompt, ...chatHistory, { role: "user", content: userPrompt }];
|
|
|
|
}
|
|
|
|
|
|
|
|
async getChatCompletion(messages = null, { temperature = 0.7 }) {
|
|
|
|
if (!this.isValidChatCompletionModel(this.model))
|
|
|
|
throw new Error(
|
|
|
|
`Anthropic chat: ${this.model} is not valid for chat completion!`
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.anthropic.messages.create({
|
|
|
|
model: this.model,
|
|
|
|
max_tokens: 4096,
|
|
|
|
system: messages[0].content, // Strip out the system message
|
|
|
|
messages: messages.slice(1), // Pop off the system message
|
|
|
|
temperature: Number(temperature ?? this.defaultTemp),
|
|
|
|
});
|
|
|
|
|
|
|
|
return response.content[0].text;
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return error;
|
|
|
|
}
|
2023-10-30 23:44:03 +01:00
|
|
|
}
|
|
|
|
|
2024-03-06 23:57:47 +01:00
|
|
|
async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
|
2023-11-06 22:13:53 +01:00
|
|
|
if (!this.isValidChatCompletionModel(this.model))
|
2023-10-30 23:44:03 +01:00
|
|
|
throw new Error(
|
2024-04-17 01:25:32 +02:00
|
|
|
`Anthropic chat: ${this.model} is not valid for chat completion!`
|
2023-10-30 23:44:03 +01:00
|
|
|
);
|
|
|
|
|
2024-03-06 23:57:47 +01:00
|
|
|
const streamRequest = await this.anthropic.messages.stream({
|
|
|
|
model: this.model,
|
|
|
|
max_tokens: 4096,
|
|
|
|
system: messages[0].content, // Strip out the system message
|
|
|
|
messages: messages.slice(1), // Pop off the system message
|
|
|
|
temperature: Number(temperature ?? this.defaultTemp),
|
|
|
|
});
|
|
|
|
return streamRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleStream(response, stream, responseProps) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
let fullText = "";
|
|
|
|
const { uuid = v4(), sources = [] } = responseProps;
|
|
|
|
|
2024-03-12 23:21:27 +01:00
|
|
|
// Establish listener to early-abort a streaming response
|
|
|
|
// in case things go sideways or the user does not like the response.
|
|
|
|
// We preserve the generated text but continue as if chat was completed
|
|
|
|
// to preserve previously generated content.
|
|
|
|
const handleAbort = () => clientAbortedHandler(resolve, fullText);
|
|
|
|
response.on("close", handleAbort);
|
|
|
|
|
2024-04-17 01:25:32 +02:00
|
|
|
stream.on("error", (event) => {
|
|
|
|
const parseErrorMsg = (event) => {
|
|
|
|
const error = event?.error?.error;
|
|
|
|
if (!!error)
|
|
|
|
return `Anthropic Error:${error?.type || "unknown"} ${
|
|
|
|
error?.message || "unknown error."
|
|
|
|
}`;
|
|
|
|
return event.message;
|
|
|
|
};
|
|
|
|
|
|
|
|
writeResponseChunk(response, {
|
|
|
|
uuid,
|
|
|
|
sources: [],
|
|
|
|
type: "abort",
|
|
|
|
textResponse: null,
|
|
|
|
close: true,
|
|
|
|
error: parseErrorMsg(event),
|
|
|
|
});
|
|
|
|
response.removeListener("close", handleAbort);
|
|
|
|
resolve(fullText);
|
|
|
|
});
|
|
|
|
|
2024-03-06 23:57:47 +01:00
|
|
|
stream.on("streamEvent", (message) => {
|
|
|
|
const data = message;
|
|
|
|
if (
|
|
|
|
data.type === "content_block_delta" &&
|
|
|
|
data.delta.type === "text_delta"
|
|
|
|
) {
|
|
|
|
const text = data.delta.text;
|
|
|
|
fullText += text;
|
|
|
|
|
|
|
|
writeResponseChunk(response, {
|
|
|
|
uuid,
|
|
|
|
sources,
|
|
|
|
type: "textResponseChunk",
|
|
|
|
textResponse: text,
|
|
|
|
close: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
message.type === "message_stop" ||
|
|
|
|
(data.stop_reason && data.stop_reason === "end_turn")
|
|
|
|
) {
|
|
|
|
writeResponseChunk(response, {
|
|
|
|
uuid,
|
|
|
|
sources,
|
|
|
|
type: "textResponseChunk",
|
|
|
|
textResponse: "",
|
|
|
|
close: true,
|
|
|
|
error: false,
|
|
|
|
});
|
2024-03-12 23:21:27 +01:00
|
|
|
response.removeListener("close", handleAbort);
|
2024-03-06 23:57:47 +01:00
|
|
|
resolve(fullText);
|
|
|
|
}
|
2023-10-30 23:44:03 +01:00
|
|
|
});
|
2024-03-06 23:57:47 +01:00
|
|
|
});
|
|
|
|
}
|
2023-10-30 23:44:03 +01:00
|
|
|
|
2024-03-06 23:57:47 +01:00
|
|
|
#appendContext(contextTexts = []) {
|
|
|
|
if (!contextTexts || !contextTexts.length) return "";
|
|
|
|
return (
|
|
|
|
"\nContext:\n" +
|
|
|
|
contextTexts
|
|
|
|
.map((text, i) => {
|
|
|
|
return `[CONTEXT ${i}]:\n${text}\n[END CONTEXT ${i}]\n\n`;
|
|
|
|
})
|
|
|
|
.join("")
|
|
|
|
);
|
2023-10-30 23:44:03 +01:00
|
|
|
}
|
|
|
|
|
2023-11-06 22:13:53 +01:00
|
|
|
async compressMessages(promptArgs = {}, rawHistory = []) {
|
|
|
|
const { messageStringCompressor } = require("../../helpers/chat");
|
|
|
|
const compressedPrompt = await messageStringCompressor(
|
|
|
|
this,
|
|
|
|
promptArgs,
|
|
|
|
rawHistory
|
|
|
|
);
|
|
|
|
return compressedPrompt;
|
|
|
|
}
|
|
|
|
|
2023-10-30 23:44:03 +01:00
|
|
|
// Simple wrapper for dynamic embedder & normalize interface for all LLM implementations
|
|
|
|
async embedTextInput(textInput) {
|
|
|
|
return await this.embedder.embedTextInput(textInput);
|
|
|
|
}
|
|
|
|
async embedChunks(textChunks = []) {
|
|
|
|
return await this.embedder.embedChunks(textChunks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
AnthropicLLM,
|
|
|
|
};
|