- 在 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,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)})
- 最终输入结果:

