deekseek本地api调用
时间: 2025-02-24 17:31:05 浏览: 88
### DeepSeek 本地 API 调用方法
对于希望在本地环境中调用 DeepSeek API 的开发者而言,确保已按照官方指南完成本地部署工作[^2]。一旦环境准备就绪,可以利用如下 Python 示例来展示如何发起请求:
```python
import requests
def call_deepseek_api(prompt, api_key):
url = "http://localhost:8000/v1/chat/completions"
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
"model": "deepseek-chat",
"messages": [{"role": "user", "content": prompt}]
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
result = response.json()
return result['choices'][0]['message']['content']
else:
raise Exception(f"Error calling API: {response.text}")
prompt_text = "解释一下量子计算的基本原理是什么?"
try:
answer = call_deepseek_api(prompt_text, "your-local-api-key")
print(answer)
except Exception as e:
print(e)
```
此代码片段展示了向运行于 `localhost` 上的 DeepSeek API 发送 POST 请求的过程,并处理返回的数据。注意 URL 和端口号可能依据实际安装情况有所不同。
为了使上述脚本正常运作,需替换 `"your-local-api-key"` 为真实的本地 API 密钥以及确认服务监听地址是否正确设置为 `http://localhost:8000` 或者其他指定路径。
阅读全文
相关推荐
















