UNPKG

chatgpt-optimized-official

Version:
304 lines 12.5 kB
import { randomUUID } from "crypto"; import { encode } from "gpt-3-encoder"; import MessageType from "../enums/message-type.js"; import AppDbContext from "./app-dbcontext.js"; import { OpenAI } from "openai"; class Assistant { options; db; onUsage; openai; assistantId; theadId; runId; conversationThreads = {}; constructor(key, options) { this.db = new AppDbContext(); this.db.WaitForLoad().then(() => { if (typeof key === "string") { if (this.db.keys.Any((x) => x.key === key)) return; this.db.keys.Add({ key: key, queries: 0, balance: 0, tokens: 0, }); } else if (Array.isArray(key)) { key.forEach((k) => { if (this.db.keys.Any((x) => x.key === k)) return; this.db.keys.Add({ key: k, queries: 0, balance: 0, tokens: 0, }); }); } }); this.options = { model: options?.model || "gpt-4-1106-preview", temperature: options?.temperature || 0.7, max_tokens: options?.max_tokens || 100, top_p: options?.top_p || 0.9, frequency_penalty: options?.frequency_penalty || 0, presence_penalty: options?.presence_penalty || 0, instructions: options?.instructions || `You are Assistant, a language model developed by OpenAI. You are designed to respond to user input in a conversational manner, Answer as concisely as possible. Your training data comes from a diverse range of internet text and You have been trained to generate human-like responses to various questions and prompts. You can provide information on a wide range of topics, but your knowledge is limited to what was present in your training data, which has a cutoff date of 2021. You strive to provide accurate and helpful information to the best of your ability.\nKnowledge cutoff: 2021-09`, price: options?.price || 0.002, max_conversation_tokens: options?.max_conversation_tokens || 4097, endpoint: options?.endpoint || "https://api.openai.com/v1/chat/completions", moderation: options?.moderation || false, functions: options?.functions || null, function_call: options?.function_call || null, name_assistant: options?.name_assistant || "My Assistant", tools: options?.tools || null, tool_choice: options?.tool_choice || null, }; this.openai = new OpenAI({ apiKey: String(key) }); this.createAssistant(this.options); } async createAssistant(options) { try { const response = await this.openai.beta.assistants.create({ name: options.name_assistant, instructions: options.instructions, tools: options.tools, model: options.model, }); console.log(response); this.assistantId = response.id; } catch (error) { console.log(error); } } async createThread() { const response = await this.openai.beta.threads.create(); this.theadId = response.id; return response.id; } async addMessageToThread(threadId, content) { return await this.openai.beta.threads.messages.create(threadId, { role: 'user', content: content }); } async runAssistant(threadId, instructions) { const response = await this.openai.beta.threads.runs.create(threadId, { assistant_id: this.assistantId, instructions: instructions || '' }); return response.id; } async checkRunStatus(threadId, runId) { return await this.openai.beta.threads.runs.retrieve(threadId, runId); } async getAssistantResponses(threadId) { const messages = await this.openai.beta.threads.messages.list(threadId); return messages.data.filter(message => message.role === "assistant"); } async createAndRunThread(prompt, userName = "User", conversationId = null) { let threadId; if (!conversationId) { threadId = await this.createThread(); } else { threadId = conversationId; } await this.addMessageToThread(threadId, prompt); const runId = await this.runAssistant(threadId); await this.waitForRunToComplete(threadId, runId); const responses = await this.getAssistantResponses(threadId); const formattedResponse = this.formatResponse(responses); return { conversationId: threadId, ...formattedResponse }; } async waitForRunToComplete(threadId, runId) { let status = await this.checkRunStatus(threadId, runId); while (status.status !== 'completed') { await new Promise(resolve => setTimeout(resolve, 1000)); status = await this.checkRunStatus(threadId, runId); } } formatResponse(responses) { console.log(responses[0].content); return responses.length > 0 ? responses[0].content : null; } getOpenAIKey() { let key = this.db.keys.OrderBy((x) => x.balance).FirstOrDefault(); if (key == null) { key = this.db.keys.FirstOrDefault(); } if (key == null) { throw new Error("No keys available."); } return key; } async *chunksToLines(chunksAsync) { let previous = ""; for await (const chunk of chunksAsync) { const bufferChunk = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); previous += bufferChunk; let eolIndex; while ((eolIndex = previous.indexOf("\n")) >= 0) { const line = previous.slice(0, eolIndex + 1).trimEnd(); if (line === "data: [DONE]") break; if (line.startsWith("data: ")) yield line; previous = previous.slice(eolIndex + 1); } } } async *linesToMessages(linesAsync) { for await (const line of linesAsync) { const message = line.substring("data :".length); yield message; } } async *streamCompletion(data) { yield* this.linesToMessages(this.chunksToLines(data)); } getInstructions(username) { return `${this.options.instructions} Current date: ${this.getToday()} Current time: ${this.getTime()}${username !== "User" ? `\nName of the user talking to: ${username}` : ""}`; } addConversation(conversationId, userName = "User") { let conversation = { id: conversationId, userName: userName, messages: [], }; this.db.conversations.Add(conversation); return conversation; } getConversation(conversationId, userName = "User") { let conversation = this.db.conversations.Where((conversation) => conversation.id === conversationId).FirstOrDefault(); if (!conversation) { conversation = this.addConversation(conversationId, userName); } else { conversation.lastActive = Date.now(); } conversation.userName = userName; return conversation; } resetConversation(conversationId) { let conversation = this.db.conversations.Where((conversation) => conversation.id == conversationId).FirstOrDefault(); if (conversation) { conversation.messages = []; conversation.lastActive = Date.now(); } return conversation; } async ask(prompt, conversationId = "default", type = 1, function_name, userName = "User") { return await this.askPost((data) => { }, (data) => { }, prompt, conversationId, function_name, userName, type); } async askPost(data, usage, prompt, conversationId = "default", function_name, userName = "User", type = MessageType.User) { return await this.createAndRunThread(prompt, userName, conversationId); } async moderate(prompt, key) { return false; } generatePrompt(conversation, prompt, type = MessageType.User, function_name) { let message = { id: randomUUID(), content: prompt, type: type, date: Date.now(), }; if (type === MessageType.Function && function_name) { message["name"] = function_name; } conversation.messages.push(message); let messages = this.generateMessages(conversation); let promptEncodedLength = this.countTokens(messages); let totalLength = promptEncodedLength + this.options.max_tokens; while (totalLength > this.options.max_conversation_tokens) { conversation.messages.shift(); messages = this.generateMessages(conversation); promptEncodedLength = this.countTokens(messages); totalLength = promptEncodedLength + this.options.max_tokens; } conversation.lastActive = Date.now(); return messages; } generateMessages(conversation) { let messages = []; messages.push({ role: "system", content: this.getInstructions(conversation.userName), }); for (let i = 0; i < conversation.messages.length; i++) { let message = conversation.messages[i]; if (message.type === MessageType.Function) { messages.push({ role: "function", name: message.name || "unknownFunction", content: message.content, }); } else { let role = message.type === MessageType.User ? "user" : "assistant"; messages.push({ role: role, content: message.content, }); } } return messages; } countTokens(messages) { let tokens = 0; for (let message of messages) { if (message.content) { if (typeof message.content === "string") { tokens += encode(message.content).length; } else if (Array.isArray(message.content)) { for (let contentBlock of message.content) { if (contentBlock.type === "text") { tokens += encode(contentBlock.text).length; } else if (contentBlock.type === "image_url") { if (contentBlock.image_url.detail === "low" || !contentBlock.image_url.detail) { tokens += 85; } else if (contentBlock.image_url.detail === "high") { tokens += 765; } else { tokens += 85; } } } } } } return tokens; } getToday() { let today = new Date(); let dd = String(today.getDate()).padStart(2, "0"); let mm = String(today.getMonth() + 1).padStart(2, "0"); let yyyy = today.getFullYear(); return `${yyyy}-${mm}-${dd}`; } getTime() { let today = new Date(); let hours = today.getHours(); let minutes = today.getMinutes(); let ampm = hours >= 12 ? "PM" : "AM"; hours = hours % 12; hours = hours ? hours : 12; minutes = minutes < 10 ? `0${minutes}` : minutes; return `${hours}:${minutes} ${ampm}`; } wait(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } } export default Assistant; //# sourceMappingURL=assistant.js.map