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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="zh-CN">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Chat with AI</title>
|
| 7 |
+
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
|
| 8 |
+
<style>
|
| 9 |
+
body {
|
| 10 |
+
font-family: 'Roboto', sans-serif;
|
| 11 |
+
background-color: #f0f2f5;
|
| 12 |
+
margin: 0;
|
| 13 |
+
padding: 0;
|
| 14 |
+
display: flex;
|
| 15 |
+
justify-content: center;
|
| 16 |
+
align-items: center;
|
| 17 |
+
min-height: 100vh;
|
| 18 |
+
}
|
| 19 |
+
.container {
|
| 20 |
+
background-color: white;
|
| 21 |
+
border-radius: 8px;
|
| 22 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 23 |
+
padding: 2rem;
|
| 24 |
+
width: 90%;
|
| 25 |
+
max-width: 800px;
|
| 26 |
+
}
|
| 27 |
+
h1 {
|
| 28 |
+
color: #333;
|
| 29 |
+
text-align: center;
|
| 30 |
+
margin-bottom: 1.5rem;
|
| 31 |
+
}
|
| 32 |
+
#question-form {
|
| 33 |
+
display: flex;
|
| 34 |
+
margin-bottom: 1.5rem;
|
| 35 |
+
}
|
| 36 |
+
#user-input {
|
| 37 |
+
flex-grow: 1;
|
| 38 |
+
padding: 0.75rem;
|
| 39 |
+
font-size: 1rem;
|
| 40 |
+
border: 1px solid #ddd;
|
| 41 |
+
border-radius: 4px 0 0 4px;
|
| 42 |
+
outline: none;
|
| 43 |
+
transition: border-color 0.3s;
|
| 44 |
+
}
|
| 45 |
+
#user-input:focus {
|
| 46 |
+
border-color: #4CAF50;
|
| 47 |
+
}
|
| 48 |
+
#submit-btn {
|
| 49 |
+
padding: 0.75rem 1.5rem;
|
| 50 |
+
font-size: 1rem;
|
| 51 |
+
background-color: #4CAF50;
|
| 52 |
+
color: white;
|
| 53 |
+
border: none;
|
| 54 |
+
border-radius: 0 4px 4px 0;
|
| 55 |
+
cursor: pointer;
|
| 56 |
+
transition: background-color 0.3s;
|
| 57 |
+
}
|
| 58 |
+
#submit-btn:hover {
|
| 59 |
+
background-color: #45a049;
|
| 60 |
+
}
|
| 61 |
+
#submit-btn:disabled {
|
| 62 |
+
background-color: #cccccc;
|
| 63 |
+
cursor: not-allowed;
|
| 64 |
+
}
|
| 65 |
+
#response-area {
|
| 66 |
+
background-color: #f9f9f9;
|
| 67 |
+
border: 1px solid #ddd;
|
| 68 |
+
border-radius: 4px;
|
| 69 |
+
padding: 1rem;
|
| 70 |
+
min-height: 100px;
|
| 71 |
+
max-height: 400px;
|
| 72 |
+
overflow-y: auto;
|
| 73 |
+
}
|
| 74 |
+
.loading {
|
| 75 |
+
text-align: center;
|
| 76 |
+
color: #666;
|
| 77 |
+
}
|
| 78 |
+
.response-content {
|
| 79 |
+
white-space: pre-wrap;
|
| 80 |
+
word-wrap: break-word;
|
| 81 |
+
}
|
| 82 |
+
.error {
|
| 83 |
+
color: #d32f2f;
|
| 84 |
+
font-weight: bold;
|
| 85 |
+
}
|
| 86 |
+
</style>
|
| 87 |
+
</head>
|
| 88 |
+
<body>
|
| 89 |
+
<div class="container">
|
| 90 |
+
<h1>Chat with AI</h1>
|
| 91 |
+
<form id="question-form">
|
| 92 |
+
<input type="text" id="user-input" placeholder="请输入您的问题" required>
|
| 93 |
+
<button type="submit" id="submit-btn">提交</button>
|
| 94 |
+
</form>
|
| 95 |
+
<div id="response-area">
|
| 96 |
+
<p class="response-content">响应将显示在这里...</p>
|
| 97 |
+
</div>
|
| 98 |
+
</div>
|
| 99 |
+
|
| 100 |
+
<script>
|
| 101 |
+
const API_KEYS = {
|
| 102 |
+
"deepseek": "sk-ighvkxrzaajsbshwyndcytoaeesupsgbtkzisameodq",
|
| 103 |
+
"qwen": "sk-ighvkxrzaawyndcytoaeessgbtkzisameodqympgn bbbb",
|
| 104 |
+
"2": "sk-RcmTwpmXe4k7SENS730aC80e061a481b8cD3C7290bDc26D2"
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
const BASE_URLS = {
|
| 108 |
+
"deepseek": "https://api.siliconflow.cn",
|
| 109 |
+
"qwen": "https://api.siliconflow.cn",
|
| 110 |
+
"2": "https://api.freegpt.art"
|
| 111 |
+
};
|
| 112 |
+
|
| 113 |
+
const MODELS = {
|
| 114 |
+
"deepseek": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
|
| 115 |
+
"qwen_math": "gpt-4-turbo",
|
| 116 |
+
"2": "gpt-4-turbo",
|
| 117 |
+
"3": "claude-3-5-sonnet-20240620",
|
| 118 |
+
"4": "gpt-4o",
|
| 119 |
+
"w": "claude-3-haiku-20240307"
|
| 120 |
+
};
|
| 121 |
+
|
| 122 |
+
async function sendChatRequest(userMessage, apiKey, model, baseUrl) {
|
| 123 |
+
const url = `${baseUrl}/v1/chat/completions`;
|
| 124 |
+
const headers = {
|
| 125 |
+
"accept": "application/json",
|
| 126 |
+
"content-type": "application/json",
|
| 127 |
+
"Authorization": `Bearer ${apiKey}`
|
| 128 |
+
};
|
| 129 |
+
const payload = {
|
| 130 |
+
"model": model,
|
| 131 |
+
"messages": [
|
| 132 |
+
{
|
| 133 |
+
"role": "user",
|
| 134 |
+
"content": userMessage
|
| 135 |
+
}
|
| 136 |
+
],
|
| 137 |
+
"stream": true,
|
| 138 |
+
"max_tokens": 4012,
|
| 139 |
+
"temperature": 0.6,
|
| 140 |
+
"top_p": 0.7,
|
| 141 |
+
"top_k": 50,
|
| 142 |
+
"frequency_penalty": 0,
|
| 143 |
+
"n": 1
|
| 144 |
+
};
|
| 145 |
+
|
| 146 |
+
try {
|
| 147 |
+
const response = await fetch(url, {
|
| 148 |
+
method: 'POST',
|
| 149 |
+
headers: headers,
|
| 150 |
+
body: JSON.stringify(payload)
|
| 151 |
+
});
|
| 152 |
+
|
| 153 |
+
if (!response.ok) {
|
| 154 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
return response.body.getReader();
|
| 158 |
+
} catch (error) {
|
| 159 |
+
console.error("Request error:", error);
|
| 160 |
+
return null;
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
async function processUserInput(userInput, onChunk) {
|
| 165 |
+
const classificationPrompt = `接下来我想请你充当问题的分类器,我会向你发送问题,如果你认为这个问题属于以下任何一类,请直接输出后面的对应标记,不要多加描述,如果不属于任何一类,请直接解这个问题,不要多加描述
|
| 166 |
+
推理及常识或日常级数学问题:【{4t}】
|
| 167 |
+
一般数学问题:【{q2math}】
|
| 168 |
+
代码问题:【{c3.5s}】
|
| 169 |
+
写作:【{c3o}】。
|
| 170 |
+
问题:(${userInput}),请分类`;
|
| 171 |
+
|
| 172 |
+
const deepseekReader = await sendChatRequest(classificationPrompt, API_KEYS["2"], MODELS.deepseek, BASE_URLS["2"]);
|
| 173 |
+
|
| 174 |
+
if (deepseekReader) {
|
| 175 |
+
const decoder = new TextDecoder();
|
| 176 |
+
let buffer = '';
|
| 177 |
+
let classification = '';
|
| 178 |
+
|
| 179 |
+
while (true) {
|
| 180 |
+
const { done, value } = await deepseekReader.read();
|
| 181 |
+
if (done) break;
|
| 182 |
+
buffer += decoder.decode(value, { stream: true });
|
| 183 |
+
const lines = buffer.split('\n');
|
| 184 |
+
buffer = lines.pop();
|
| 185 |
+
for (const line of lines) {
|
| 186 |
+
if (line.startsWith('data: ')) {
|
| 187 |
+
const data = line.slice(6);
|
| 188 |
+
if (data === '[DONE]') break;
|
| 189 |
+
try {
|
| 190 |
+
const parsed = JSON.parse(data);
|
| 191 |
+
const chunk = parsed.choices[0].delta.content;
|
| 192 |
+
if (chunk) {
|
| 193 |
+
classification += chunk;
|
| 194 |
+
}
|
| 195 |
+
} catch (error) {
|
| 196 |
+
console.error('Error parsing streaming response:', error);
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
console.log("DeepSeek Classification:", classification);
|
| 203 |
+
|
| 204 |
+
let reader;
|
| 205 |
+
switch (classification.trim()) {
|
| 206 |
+
case "【{q2math}】":
|
| 207 |
+
reader = await sendChatRequest(userInput, API_KEYS["2"], MODELS.qwen_math, BASE_URLS["2"]);
|
| 208 |
+
break;
|
| 209 |
+
case "【{4t}】":
|
| 210 |
+
reader = await sendChatRequest("请一步步来,先思考再给结论:" + userInput, API_KEYS["2"], MODELS["2"], BASE_URLS["2"]);
|
| 211 |
+
break;
|
| 212 |
+
case "【{c3.5s}】":
|
| 213 |
+
reader = await sendChatRequest(userInput, API_KEYS["2"], MODELS["3"], BASE_URLS["2"]);
|
| 214 |
+
break;
|
| 215 |
+
case "【{c3o}】":
|
| 216 |
+
reader = await sendChatRequest(userInput, API_KEYS["2"], MODELS.w, BASE_URLS["2"]);
|
| 217 |
+
break;
|
| 218 |
+
default:
|
| 219 |
+
reader = await sendChatRequest(userInput, API_KEYS["2"], MODELS["4"], BASE_URLS["2"]);
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
if (reader) {
|
| 223 |
+
const decoder = new TextDecoder();
|
| 224 |
+
let buffer = '';
|
| 225 |
+
|
| 226 |
+
while (true) {
|
| 227 |
+
const { done, value } = await reader.read();
|
| 228 |
+
if (done) break;
|
| 229 |
+
buffer += decoder.decode(value, { stream: true });
|
| 230 |
+
const lines = buffer.split('\n');
|
| 231 |
+
buffer = lines.pop();
|
| 232 |
+
for (const line of lines) {
|
| 233 |
+
if (line.startsWith('data: ')) {
|
| 234 |
+
const data = line.slice(6);
|
| 235 |
+
if (data === '[DONE]') {
|
| 236 |
+
return;
|
| 237 |
+
}
|
| 238 |
+
try {
|
| 239 |
+
const parsed = JSON.parse(data);
|
| 240 |
+
const chunk = parsed.choices[0].delta.content;
|
| 241 |
+
if (chunk) {
|
| 242 |
+
onChunk(chunk);
|
| 243 |
+
}
|
| 244 |
+
} catch (error) {
|
| 245 |
+
console.error('Error parsing streaming response:', error);
|
| 246 |
+
}
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
}
|
| 250 |
+
} else {
|
| 251 |
+
onChunk("抱歉,无法获取响应。");
|
| 252 |
+
}
|
| 253 |
+
} else {
|
| 254 |
+
onChunk("无法从 DeepSeek-V2-Chat 获取分类。");
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
document.getElementById('question-form').addEventListener('submit', async function(e) {
|
| 259 |
+
e.preventDefault();
|
| 260 |
+
const userInput = document.getElementById('user-input').value;
|
| 261 |
+
const responseArea = document.getElementById('response-area');
|
| 262 |
+
const submitBtn = document.getElementById('submit-btn');
|
| 263 |
+
|
| 264 |
+
responseArea.innerHTML = '<p class="response-content"><strong>响应:</strong><br></p>';
|
| 265 |
+
submitBtn.disabled = true;
|
| 266 |
+
|
| 267 |
+
try {
|
| 268 |
+
await processUserInput(userInput, (chunk) => {
|
| 269 |
+
const responseContent = responseArea.querySelector('.response-content');
|
| 270 |
+
responseContent.innerHTML += chunk;
|
| 271 |
+
responseArea.scrollTop = responseArea.scrollHeight;
|
| 272 |
+
});
|
| 273 |
+
} catch (error) {
|
| 274 |
+
console.error('Error:', error);
|
| 275 |
+
responseArea.innerHTML = '<p class="error">抱歉,处理您的请求时出现错误。</p>';
|
| 276 |
+
} finally {
|
| 277 |
+
submitBtn.disabled = false;
|
| 278 |
+
}
|
| 279 |
+
});
|
| 280 |
+
</script>
|
| 281 |
+
</body>
|
| 282 |
+
</html>
|