{"repo": "bombap/nodejs-rat", "files": {"src/ModelChain.js": {"4ba5220cb24dc4d30a1115545e9fe08b3afd88fc": {"t": 1785745544.9269378, "n": 3611, "h": "48838172a2bac346", "c": "import { anthropic } from '@ai-sdk/anthropic';\nimport { streamText } from 'ai';\nimport OpenAI from 'openai';\nimport chalk from 'chalk';\n\nexport class ModelChain {\n constructor() {\n // Khởi tạo DeepSeek client\n this.deepseek_client = new OpenAI({\n apiKey: process.env.DEEPSEEK_API_KEY,\n baseURL: \"https://api.deepseek.com\"\n });\n\n this.deepseek_messages = [];\n this.claude_messages = [];\n this.current_model = \"claude-3-5-sonnet-20241022\"\n this.show_reasoning = true;\n }\n\n setModel(modelName) {\n this.current_model = modelName;\n }\n\n getModelDisplayName() {\n return this.current_model;\n }\n\n async getDeepseekReasoning(userInput) {\n const startTime = Date.now();\n this.deepseek_messages.push({ role: \"user\", content: userInput });\n\n if (this.show_reasoning) {\n console.log(chalk.blue(\"\\nReasoning Process\"));\n }\n\n const messages = this.deepseek_messages.map((msg) => ({\n role: msg.role === \"user\" ? \"user\" : \"assistant\",\n content: msg.content,\n }));\n\n const response = await this.deepseek_client.chat.completions.create({\n model: \"deepseek-reasoner\",\n messages,\n max_tokens: 1,\n stream: true\n });\n\n let reasoningContent = \"\";\n let finalContent = \"\";\n\n for await (const chunk of response) {\n if (chunk.choices[0]?.delta?.reasoning_content) {\n const reasoningPiece = chunk.choices[0].delta.reasoning_content;\n reasoningContent += reasoningPiece;\n if (this.show_reasoning) {\n process.stdout.write(chalk.cyan(reasoningPiece));\n }\n } else if (chunk.choices[0].delta.content) {\n finalContent += chunk.choices[0].delta.content;\n }\n }\n\n const elapsedTime = (Date.now() - startTime) / 1000;\n const timeStr = elapsedTime >= 60\n ? `${(elapsedTime / 60).toFixed(1)} minutes`\n : `${elapsedTime.toFixed(1)} seconds`;\n\n console.log(chalk.yellow(`\\n\\nThought for ${timeStr}`));\n\n if (this.show_reasoning) {\n console.log(\"\\n\");\n }\n return reasoningContent;\n }\n\n async getResponse(userInput, reasoning) {\n const messages = [\n { role: \"user\", content: userInput },\n {\n role: \"assistant\",\n content: `${reasoning}`\n }\n ];\n\n process.stdout.write(chalk.green(this.getModelDisplayName()));\n\n try {\n const result = streamText({\n model: anthropic(this.current_model),\n messages,\n maxTokens: 8000,\n stream: true\n });\n\n let fullResponse = '';\n\n for await (const chunk of result.textStream) {\n process.stdout.write(chalk.green(chunk));\n fullResponse += chunk;\n }\n\n this.claude_messages.push(\n { role: \"user\", content: userInput },\n { role: \"assistant\", content: fullResponse }\n );\n\n this.deepseek_messages.push({\n role: \"assistant\",\n content: fullResponse\n });\n\n console.log(\"\\n\");\n return fullResponse;\n\n } catch (error) {\n console.log(chalk.red(`\\nError in response: ${error.message}`));\n return \"Error occurred while getting response\";\n }\n }\n} "}}}}