发布于 ,更新于 
文章摘要
加载中...
此内容由AI根据文章生成,完全没经过人工审核,仅用于文章内容的解释与总结

如何在诺基亚上愉快的使用DeepSeek

由于nokia的浏览器只能访问静态网页,无法加载js,所以我们用Cloudflare worker代理向api发送请求,再渲染为静态页面返回给浏览器即可。

下面是Cloudflare worker的代码实现。
注意要绑定KV,名字为CACHE,你也可以自己更改

注:

  1. 可能存在部分网络情况下无法访问Cloudflare的情况,对此我的办法是走vercel反代,当然会有1min限时的问题但我也没想出别的办法。
  2. 连续对话功能没有实现,以后再做吧
  3. deepseek可能会花过多时间思考,导致请求超时。这属于是硬伤,可能只有换成别的轻量模型会好点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const CACHE_TTL = 864000; // 缓存有效期24小时(单位:秒)

function createHTML(title, content) {
return `<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
<meta charset="UTF-8">
<style>
body {
font-family: -apple-system, system-ui, sans-serif;
max-width: 800px;
margin: 1rem auto;
padding: 0 1rem;
line-height: 1.5
}
.box {
background: #f8f9fa;
padding: 1rem;
margin: 1rem 0;
border-radius: 4px;
border: 1px solid #dee2e6
}
.cache-link {
color: #0d6efd;
word-break: break-all
}
</style>
</head>
<body>
${content}
</body>
</html>`;
}

// 转义HTML特殊字符
function escapeHTML(str) {
return str.replace(/[&<>"']/g, m =>
({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[m]
);
}
/* * * * * * * * * * * *
* micromarkdown .js *
* Version 0.3.4 *
* License: MIT *
* Simon Waldherr *
* * * * * * * * * * * */
function markdownToHtml(markdown) {
const lines = markdown.split('\n');
let html = [];
let inList = false;
let currentParagraph = [];

const flushParagraph = () => {
if (currentParagraph.length > 0) {
html.push(`<p>${processInline(currentParagraph.join('<br>'))}</p>`);
currentParagraph = [];
}
};

const flushList = () => {
if (inList) {
html.push('</ul>');
inList = false;
}
};

const processInline = (text) => {
return text
.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>');
};

for (const line of lines) {
// 处理标题
const headingMatch = line.match(/^(#+)\s+(.*)/);
if (headingMatch) {
flushParagraph();
flushList();
const level = Math.min(headingMatch[1].length, 6);
html.push(`<h${level}>${processInline(headingMatch[2])}</h${level}>`);
continue;
}

// 处理列表
const listItemMatch = line.match(/^[*\-]\s+(.*)/);
if (listItemMatch) {
flushParagraph();
if (!inList) {
html.push('<ul>');
inList = true;
}
html.push(`<li>${processInline(listItemMatch[1])}</li>`);
continue;
} else if (inList) {
flushList();
}

// 处理空行
if (line.trim() === '') {
flushParagraph();
continue;
}

// 收集段落内容
currentParagraph.push(line.trim());
}

// 处理剩余内容
flushParagraph();
flushList();

return html.join('\n');
}


/* /micromarkdown */
async function handleRequest(request,env) {
const url = new URL(request.url);
const params = url.searchParams;

// 优先检查缓存请求
const id = params.get('id');
let cacheId = params.get('cache_id');
if (cacheId) {
const cachedData = await env.CACHE.get(cacheId);
if (cachedData) {
const { question, convertedAnswer } = JSON.parse(cachedData);
return new Response(createHTML(
`缓存内容 #${cacheId}`,
`<h1>缓存内容</h1>
<div class="box">
<h3>原始问题:</h3>
<div>${question}</div>
<h3>AI回复:</h3>
<div>${convertedAnswer}</div>
</div>
<p><a href="${url.origin}">返回新提问</a></p>`
), { headers: { 'Content-Type': 'text/html' } });
}
return new Response(createHTML(
'缓存不存在',
'<h1>缓存不存在或已过期</h1><p><a href="/">返回</a></p>'
), { status: 404, headers: { 'Content-Type': 'text/html' } });
}
if (id) {
cacheId = id;
const cachedData = await env.CACHE.get(cacheId);
if (cachedData) {
const { question, convertedAnswer } = JSON.parse(cachedData);
return new Response(createHTML(
`#${cacheId} ${question}`,
`<h1>缓存内容</h1>
<div class="box">
<h3>原始问题:</h3>
<div>${question}</div>
<h3>AI回复:</h3>
<div>${convertedAnswer}</div>
</div>
<p><a href="${url.origin}">返回新提问</a></p>`
), { headers: { 'Content-Type': 'text/html' } });
}
}
// 处理新请求
const question = params.get('content');
if (!question) {
return new Response(createHTML('JavaScript-free DeepSeek','<h2>我是 DeepSeek-R1,不该问的别问</h2><p>我有可能可以帮你写代码、读文件、写作各种创意内容,请把你的任务交给我吧~</p><p>提示:上为content,下为id,注意区分</p><form method="GET" action="/"><input type="text" name="content" placeholder="向 DeepSeek-R1 询问任何问题"><input type="text" name="id" placeholder="请输入ID"><button type="submit">发送</button></form><p>Created by Keee</p>'), { status: 400, headers: { 'Content-Type': 'text/html' } });
}
try {
// 调用OpenAI API
const response = await fetch('https://integrate.api.nvidia.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ***`
},
body: JSON.stringify({
model: "deepseek-ai/deepseek-r1",
messages: [{ role: "user", content: question }],
temperature:0.6,
top_p:0.7,
stream:false
})
});

const data = await response.json();
const answer = data.choices[0].message.content;
const convertedAnswer = markdownToHtml(answer);
// 生成缓存ID并存储

let cacheId = Date.now().toString(36) + Math.random().toString(36).substr(2, 5)
if(id) cacheId = id
await env.CACHE.put(cacheId, JSON.stringify({ question, convertedAnswer }), {
expirationTtl: CACHE_TTL
});

return new Response(createHTML(
question,
`<h1>Deepseek-R1</h1>
<div class="box">
<h3>问题:</h3>
<div>${escapeHTML(question)}</div>
<h3>AI回复:</h3>
<div>${convertedAnswer}</div>
</div>
<p>缓存链接:<br>
<a class="cache-link" href="?cache_id=${cacheId}">${url.origin}/?cache_id=${cacheId}</a>
</p>`
), { headers: { 'Content-Type': 'text/html' } });

} catch (error) {
return new Response(createHTML(
'发生错误',
`<h1>请求处理失败</h1>
<div class="box">${escapeHTML(error.message)}</div>`
), { status: 500, headers: { 'Content-Type': 'text/html' } });
}
}

export default {
async fetch(request, env) {
try {
return await handleRequest(request, env);
} catch (err) {
return new Response(createHTML(
'服务器错误',
`<h1>服务器内部错误</h1><div class="box">${escapeHTML(err.message)}</div>`
), { status: 500, headers: { 'Content-Type': 'text/html' } });
}
}
};

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

本站由 @Keee 创建,使用 Stellaris 作为主题。

Hexo 强力驱动