1 Star 0 Fork 0

phpervip/deep-chat-node

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
main.js 4.47 KB
Copy Edit Raw Blame History
phpervip authored 5 months ago . second commit
const fs = require('fs').promises;
const readline = require('readline');
const axios = require('axios');
const moment = require('moment');
class DeepSeekChat {
constructor() {
this.url = 'https://api.siliconflow.cn/v1/chat/completions';
this.apiKey = 'Your-Api-Key'; // 替换为你的 API Key,如何获取自己的 API Key? 在文章下面有链接
this.logFile = 'conversation.txt';
}
async saveToFile(content, isQuestion = false) {
const timestamp = moment().format('YYYY-MM-DD HH:mm:ss');
const text = isQuestion
? `\n[${timestamp}] Question:\n${content}\n\n[${timestamp}] Answer:\n`
: content;
await fs.appendFile(this.logFile, text);
}
async chat() {
// 创建命令行接口
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// 使用 Promise 封装问题输入
const question = (prompt) => new Promise((resolve) => rl.question(prompt, resolve));
try {
while (true) {
const userInput = await question('\n请输入您的问题 (输入 q 退出): ');
if (userInput.trim().toLowerCase() === 'q') {
console.log('程序已退出');
break;
}
// 保存问题
await this.saveToFile(userInput, true);
// 准备请求数据
const data = {
model: 'deepseek-ai/DeepSeek-V3',
messages: [
{
role: 'user',
content: userInput
}
],
stream: true,
max_tokens: 2048,
temperature: 0.7,
top_p: 0.7,
top_k: 50,
frequency_penalty: 0.5,
n: 1,
response_format: {
type: 'text'
}
};
try {
// 发送流式请求
const response = await axios({
method: 'post',
url: this.url,
data: data,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
responseType: 'stream'
});
// 处理流式响应
response.data.on('data', async (chunk) => {
const lines = chunk.toString().split('\n');
for (const line of lines) {
if (line.trim() === '') continue;
if (line.trim() === 'data: [DONE]') continue;
if (line.startsWith('data: ')) {
try {
const json = JSON.parse(line.slice(6));
if (json.choices[0].delta.content) {
const content = json.choices[0].delta.content;
process.stdout.write(content);
await this.saveToFile(content);
}
} catch (e) {
continue;
}
}
}
});
// 等待响应完成
await new Promise((resolve) => {
response.data.on('end', async () => {
console.log('\n----------------------------------------');
await this.saveToFile('\n----------------------------------------\n');
resolve();
});
});
} catch (error) {
const errorMsg = `请求错误: ${error.message}\n`;
console.error(errorMsg);
await this.saveToFile(errorMsg);
}
}
} finally {
rl.close();
}
}
}
// 运行程序
async function main() {
const chatbot = new DeepSeekChat();
await chatbot.chat();
}
main().catch(console.error);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/phpervip/deep-chat-node.git
git@gitee.com:phpervip/deep-chat-node.git
phpervip
deep-chat-node
deep-chat-node
master

Search