ludmi
Version:
LU (Layer Understanding) is a lightweight framework for controlled chatbot interactions with LLMs, action orchestration, and retrieval-augmented generation (RAG).
65 lines (64 loc) • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEmbeddings = exports.getAIResponse = void 0;
const openai_1 = require("../../services/openai");
const PRICE = {
"gpt-4o-mini": {
INPUT: 0.00000015,
OUTPUT: 0.000000075
},
"text-embedding-3-small": {
INPUT: 0,
OUTPUT: 0
},
default: {
INPUT: 0.00000015,
OUTPUT: 0.000000075
}
};
/**
* Get the response from the AI.
*
* @param {AIResponseProps} props - The properties containing messages, model and temperature.
* @param {Messages} props.messages - The messages to be sent to the AI.
* @param {Model} props.model - The model to be used by the AI.
* @param {number} props.temperature - The temperature to be used by the AI.
* @param {string[]} props.tools - The tools to be used by the AI (optional).
* @returns {} { price, content }
*/
const getAIResponse = async ({ messages, model = "gpt-4o-mini", temperature = 1, tools = [] }) => {
const completions = await openai_1.openai.chat.completions.create({
model,
messages,
temperature,
tools,
tool_choice: "auto",
});
const usage = completions.usage;
const itokens = Number(usage.prompt_tokens);
const otokens = Number(usage.completion_tokens);
const priceModel = PRICE[model] || PRICE.default;
const price = itokens * priceModel.INPUT + otokens * priceModel.OUTPUT;
console.log(`INPUT: $${itokens * priceModel.INPUT} (${itokens} it) - OUTPUT: $${otokens * priceModel.OUTPUT} (${otokens} it) - Total: $${price} (${itokens + otokens} it)`);
console.log(0, completions.choices[0].message.tool_calls || "No tool calls made");
return {
content: completions.choices[0].message.content || "",
price,
calls: completions.choices[0].message.tool_calls || []
};
};
exports.getAIResponse = getAIResponse;
/**
* Get the embeddings of a text.
*
* @param text - The text to be embedded.
* @returns The embeddings of the text.
*/
const getEmbeddings = async (text) => {
const embeddingResponse = await openai_1.openai.embeddings.create({
model: "text-embedding-3-small",
input: text,
});
return embeddingResponse.data[0].embedding;
};
exports.getEmbeddings = getEmbeddings;