2025-02-05 16:35:22 -08:00
|
|
|
const { v4: uuidv4 } = require("uuid");
|
2024-02-22 12:48:57 -08:00
|
|
|
const { NativeEmbedder } = require("../../EmbeddingEngines/native");
|
2024-04-30 12:33:42 -07:00
|
|
|
const {
|
2025-02-05 16:35:22 -08:00
|
|
|
writeResponseChunk,
|
|
|
|
clientAbortedHandler,
|
2024-04-30 12:33:42 -07:00
|
|
|
} = require("../../helpers/chat/responses");
|
2024-12-16 14:31:17 -08:00
|
|
|
const {
|
|
|
|
LLMPerformanceMonitor,
|
|
|
|
} = require("../../helpers/chat/LLMPerformanceMonitor");
|
2024-02-22 12:48:57 -08:00
|
|
|
|
|
|
|
function perplexityModels() {
|
|
|
|
const { MODELS } = require("./models.js");
|
|
|
|
return MODELS || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
class PerplexityLLM {
|
|
|
|
constructor(embedder = null, modelPreference = null) {
|
|
|
|
if (!process.env.PERPLEXITY_API_KEY)
|
|
|
|
throw new Error("No Perplexity API key was set.");
|
|
|
|
|
2024-04-30 12:33:42 -07:00
|
|
|
const { OpenAI: OpenAIApi } = require("openai");
|
|
|
|
this.openai = new OpenAIApi({
|
|
|
|
baseURL: "https://api.perplexity.ai",
|
|
|
|
apiKey: process.env.PERPLEXITY_API_KEY ?? null,
|
2024-02-22 12:48:57 -08:00
|
|
|
});
|
|
|
|
this.model =
|
2024-04-30 12:33:42 -07:00
|
|
|
modelPreference ||
|
|
|
|
process.env.PERPLEXITY_MODEL_PREF ||
|
2024-07-31 10:58:34 -07:00
|
|
|
"llama-3-sonar-large-32k-online"; // Give at least a unique model to the provider as last fallback.
|
2024-02-22 12:48:57 -08:00
|
|
|
this.limits = {
|
|
|
|
history: this.promptWindowLimit() * 0.15,
|
|
|
|
system: this.promptWindowLimit() * 0.15,
|
|
|
|
user: this.promptWindowLimit() * 0.7,
|
|
|
|
};
|
|
|
|
|
2024-05-16 17:25:05 -07:00
|
|
|
this.embedder = embedder ?? new NativeEmbedder();
|
2024-02-22 12:48:57 -08:00
|
|
|
this.defaultTemp = 0.7;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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("")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
allModelInformation() {
|
|
|
|
return perplexityModels();
|
|
|
|
}
|
|
|
|
|
|
|
|
streamingEnabled() {
|
2024-05-01 16:52:28 -07:00
|
|
|
return "streamGetChatCompletion" in this;
|
2024-02-22 12:48:57 -08:00
|
|
|
}
|
|
|
|
|
2024-08-15 12:13:28 -07:00
|
|
|
static promptWindowLimit(modelName) {
|
|
|
|
const availableModels = perplexityModels();
|
|
|
|
return availableModels[modelName]?.maxLength || 4096;
|
|
|
|
}
|
|
|
|
|
2024-02-22 12:48:57 -08:00
|
|
|
promptWindowLimit() {
|
|
|
|
const availableModels = this.allModelInformation();
|
|
|
|
return availableModels[this.model]?.maxLength || 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
async isValidChatCompletionModel(model = "") {
|
|
|
|
const availableModels = this.allModelInformation();
|
|
|
|
return availableModels.hasOwnProperty(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructPrompt({
|
|
|
|
systemPrompt = "",
|
|
|
|
contextTexts = [],
|
|
|
|
chatHistory = [],
|
|
|
|
userPrompt = "",
|
|
|
|
}) {
|
|
|
|
const prompt = {
|
|
|
|
role: "system",
|
|
|
|
content: `${systemPrompt}${this.#appendContext(contextTexts)}`,
|
|
|
|
};
|
|
|
|
return [prompt, ...chatHistory, { role: "user", content: userPrompt }];
|
|
|
|
}
|
|
|
|
|
|
|
|
async getChatCompletion(messages = null, { temperature = 0.7 }) {
|
|
|
|
if (!(await this.isValidChatCompletionModel(this.model)))
|
|
|
|
throw new Error(
|
|
|
|
`Perplexity chat: ${this.model} is not valid for chat completion!`
|
|
|
|
);
|
|
|
|
|
2024-12-16 14:31:17 -08:00
|
|
|
const result = await LLMPerformanceMonitor.measureAsyncFunction(
|
|
|
|
this.openai.chat.completions
|
|
|
|
.create({
|
|
|
|
model: this.model,
|
|
|
|
messages,
|
|
|
|
temperature,
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
throw new Error(e.message);
|
|
|
|
})
|
|
|
|
);
|
2024-02-22 12:48:57 -08:00
|
|
|
|
2024-12-16 14:31:17 -08:00
|
|
|
if (
|
|
|
|
!result.output.hasOwnProperty("choices") ||
|
|
|
|
result.output.choices.length === 0
|
|
|
|
)
|
2024-04-30 12:33:42 -07:00
|
|
|
return null;
|
2024-12-16 14:31:17 -08:00
|
|
|
|
|
|
|
return {
|
|
|
|
textResponse: result.output.choices[0].message.content,
|
|
|
|
metrics: {
|
|
|
|
prompt_tokens: result.output.usage?.prompt_tokens || 0,
|
|
|
|
completion_tokens: result.output.usage?.completion_tokens || 0,
|
|
|
|
total_tokens: result.output.usage?.total_tokens || 0,
|
|
|
|
outputTps: result.output.usage?.completion_tokens / result.duration,
|
|
|
|
duration: result.duration,
|
|
|
|
},
|
|
|
|
};
|
2024-02-22 12:48:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
|
|
|
|
if (!(await this.isValidChatCompletionModel(this.model)))
|
|
|
|
throw new Error(
|
|
|
|
`Perplexity chat: ${this.model} is not valid for chat completion!`
|
|
|
|
);
|
|
|
|
|
2024-12-16 14:31:17 -08:00
|
|
|
const measuredStreamRequest = await LLMPerformanceMonitor.measureStream(
|
|
|
|
this.openai.chat.completions.create({
|
|
|
|
model: this.model,
|
|
|
|
stream: true,
|
|
|
|
messages,
|
|
|
|
temperature,
|
|
|
|
}),
|
|
|
|
messages
|
|
|
|
);
|
|
|
|
return measuredStreamRequest;
|
2024-02-22 12:48:57 -08:00
|
|
|
}
|
|
|
|
|
2025-02-05 16:35:22 -08:00
|
|
|
enrichToken(token, citations) {
|
|
|
|
if (Array.isArray(citations) && citations.length !== 0) {
|
|
|
|
return token.replace(/\[(\d+)\]/g, (match, index) => {
|
|
|
|
const citationIndex = parseInt(index) - 1;
|
|
|
|
return citations[citationIndex]
|
|
|
|
? `[[${index}](${citations[citationIndex]})]`
|
|
|
|
: match;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2024-02-22 12:48:57 -08:00
|
|
|
handleStream(response, stream, responseProps) {
|
2025-02-05 16:35:22 -08:00
|
|
|
const timeoutThresholdMs = 800;
|
|
|
|
const { uuid = uuidv4(), sources = [] } = responseProps;
|
|
|
|
let hasUsageMetrics = false;
|
|
|
|
let pplxCitations = []; // Array of links
|
|
|
|
let usage = {
|
|
|
|
completion_tokens: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
let fullText = "";
|
|
|
|
let lastChunkTime = null;
|
|
|
|
|
|
|
|
const handleAbort = () => {
|
|
|
|
stream?.endMeasurement(usage);
|
|
|
|
clientAbortedHandler(resolve, fullText);
|
|
|
|
};
|
|
|
|
response.on("close", handleAbort);
|
|
|
|
|
|
|
|
const timeoutCheck = setInterval(() => {
|
|
|
|
if (lastChunkTime === null) return;
|
|
|
|
|
|
|
|
const now = Number(new Date());
|
|
|
|
const diffMs = now - lastChunkTime;
|
|
|
|
if (diffMs >= timeoutThresholdMs) {
|
|
|
|
console.log(
|
|
|
|
`Perplexity stream did not self-close and has been stale for >${timeoutThresholdMs}ms. Closing response stream.`
|
|
|
|
);
|
|
|
|
writeResponseChunk(response, {
|
|
|
|
uuid,
|
|
|
|
sources,
|
|
|
|
type: "textResponseChunk",
|
|
|
|
textResponse: "",
|
|
|
|
close: true,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
clearInterval(timeoutCheck);
|
|
|
|
response.removeListener("close", handleAbort);
|
|
|
|
stream?.endMeasurement(usage);
|
|
|
|
resolve(fullText);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
// Now handle the chunks from the streamed response and append to fullText.
|
|
|
|
try {
|
|
|
|
for await (const chunk of stream) {
|
|
|
|
lastChunkTime = Number(new Date());
|
|
|
|
const message = chunk?.choices?.[0];
|
|
|
|
const token = message?.delta?.content;
|
|
|
|
|
|
|
|
if (Array.isArray(chunk.citations) && chunk.citations.length !== 0) {
|
|
|
|
pplxCitations = chunk.citations;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we see usage metrics in the chunk, we can use them directly
|
|
|
|
// instead of estimating them, but we only want to assign values if
|
|
|
|
// the response object is the exact same key:value pair we expect.
|
|
|
|
if (
|
|
|
|
chunk.hasOwnProperty("usage") && // exists
|
|
|
|
!!chunk.usage && // is not null
|
|
|
|
Object.values(chunk.usage).length > 0 // has values
|
|
|
|
) {
|
|
|
|
if (chunk.usage.hasOwnProperty("prompt_tokens")) {
|
|
|
|
usage.prompt_tokens = Number(chunk.usage.prompt_tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunk.usage.hasOwnProperty("completion_tokens")) {
|
|
|
|
hasUsageMetrics = true; // to stop estimating counter
|
|
|
|
usage.completion_tokens = Number(chunk.usage.completion_tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
let enrichedToken = this.enrichToken(token, pplxCitations);
|
|
|
|
fullText += enrichedToken;
|
|
|
|
if (!hasUsageMetrics) usage.completion_tokens++;
|
|
|
|
|
|
|
|
writeResponseChunk(response, {
|
|
|
|
uuid,
|
|
|
|
sources: [],
|
|
|
|
type: "textResponseChunk",
|
|
|
|
textResponse: enrichedToken,
|
|
|
|
close: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message?.finish_reason) {
|
|
|
|
console.log("closing");
|
|
|
|
writeResponseChunk(response, {
|
|
|
|
uuid,
|
|
|
|
sources,
|
|
|
|
type: "textResponseChunk",
|
|
|
|
textResponse: "",
|
|
|
|
close: true,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
response.removeListener("close", handleAbort);
|
|
|
|
stream?.endMeasurement(usage);
|
|
|
|
clearInterval(timeoutCheck);
|
|
|
|
resolve(fullText);
|
|
|
|
break; // Break streaming when a valid finish_reason is first encountered
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(`\x1b[43m\x1b[34m[STREAMING ERROR]\x1b[0m ${e.message}`);
|
|
|
|
writeResponseChunk(response, {
|
|
|
|
uuid,
|
|
|
|
type: "abort",
|
|
|
|
textResponse: null,
|
|
|
|
sources: [],
|
|
|
|
close: true,
|
|
|
|
error: e.message,
|
|
|
|
});
|
|
|
|
stream?.endMeasurement(usage);
|
|
|
|
clearInterval(timeoutCheck);
|
|
|
|
resolve(fullText); // Return what we currently have - if anything.
|
|
|
|
}
|
|
|
|
});
|
2024-02-22 12:48:57 -08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
async compressMessages(promptArgs = {}, rawHistory = []) {
|
|
|
|
const { messageArrayCompressor } = require("../../helpers/chat");
|
|
|
|
const messageArray = this.constructPrompt(promptArgs);
|
|
|
|
return await messageArrayCompressor(this, messageArray, rawHistory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
PerplexityLLM,
|
|
|
|
perplexityModels,
|
|
|
|
};
|