提交 c6ac4f79 编写于 作者: P practicer2015

Thu Apr 17 21:44:00 CST 2025 inscode

上级 3bf8e579
......@@ -8,30 +8,49 @@ app = Flask(__name__)
API_URL = "https://api.deepseek.com"
API_KEY = os.getenv('DeepSeek_API_KEY')
def call_deepseek(prompt, max_new_tokens=150):
def call_deepseek(prompt, model="deepseek-chat", max_tokens=2048, temperature=0.7):
"""
调用 DeepSeek API 生成文本
:param prompt: 输入的提示文本
:param max_new_tokens: 生成的最大新 token 数
:return: 生成的文本
调用 DeepSeek API 的函数
参数:
- prompt: 输入的提示文本
- model: 使用的模型名称(默认为 deepseek-chat)
- max_tokens: 生成的最大token数
- temperature: 控制生成随机性的参数(0-1)
返回:
- API的响应内容
"""
# API端点(请根据实际API文档替换)
api_url = "https://api.deepseek.com/v1/chat/completions"
# 你的API密钥(请替换为实际的密钥)
api_key = "sk-b36c4122bb874a3f83f250d2e9c8a705"
# 请求头
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
"Authorization": f"Bearer {api_key}"
}
data = {
"prompt": prompt,
"max_new_tokens": max_new_tokens
# 请求体
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": max_tokens,
"temperature": temperature
}
try:
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
response.raise_for_status()
result = response.json().get("generated_text")
return result
except requests.RequestException as e:
print(f"调用 DeepSeek API 时请求出错: {e}")
except (KeyError, json.JSONDecodeError) as e:
print(f"解析 DeepSeek API 响应时出错: {e}")
# 发送POST请求
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status() # 检查是否有错误
# 返回API响应
return response.json()
except requests.exceptions.RequestException as e:
print(f"请求出错: {e}")
return None
@app.route('/generate', methods=['POST'])
......@@ -52,3 +71,4 @@ def generate_text():
if __name__ == "__main__":
app.run(debug=True)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册