js 怎么调用 deepseekAPI
【代码】js 怎么调用 deepseekAPI。
·
- 在 deepseek 开放平台登录,创建 API keys

- 在 js 中利用 fetch 方法调用 API
API 文档

async function callDeepSeekAPI(messages, model = 'deepseek-chat') {
const url = 'https://api.deepseek.com/chat/completions'
const response = await fetch(url, {
method: 'POST',
headers: {
Authorization: 'Bearer sk-xxxxx',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: model,
messages: messages,
// stream: true,
max_tokens: 4096,
temperature: 0.6
})
})
if (!response.ok) {
throw new Error(`API调用失败: ${response.status}`)
}
return await response.json()
}
// 使用示例
const messages = [{ role: 'user', content: '你好,请介绍一下自己' }]
callDeepSeekAPI(messages, 'deepseek-chat')
.then(result => {
console.log(result)
console.log(result.choices[0].message.content)
})
.catch(error => {
console.error('错误:', error)
})
- 最终输入结果:


更多推荐



所有评论(0)