Spaces:
Sleeping
Sleeping
DeepSeek API
Browse files
app.py
CHANGED
|
@@ -6,39 +6,43 @@ import os
|
|
| 6 |
from typing import Optional
|
| 7 |
import tempfile
|
| 8 |
import requests
|
|
|
|
| 9 |
|
| 10 |
# DeepSeek API配置
|
| 11 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY", "")
|
| 12 |
-
DEEPSEEK_BASE_URL = "https://
|
| 13 |
|
| 14 |
def check_api_key():
|
| 15 |
"""检查API Key是否配置"""
|
| 16 |
if not DEEPSEEK_API_KEY:
|
| 17 |
raise ValueError("⚠️ 请在 Space Settings 中配置 DEEPSEEK_API_KEY!\n\n前往:Settings → Repository secrets → New secret")
|
| 18 |
|
| 19 |
-
def call_deepseek_api(prompt, model="deepseek-
|
| 20 |
-
"""使用
|
| 21 |
check_api_key()
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"stream": False # 简化处理,不使用流式
|
| 35 |
-
}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# 预置的Leaderboard数据
|
| 44 |
LEADERBOARD_DATA = [
|
|
|
|
| 6 |
from typing import Optional
|
| 7 |
import tempfile
|
| 8 |
import requests
|
| 9 |
+
from openai import OpenAI
|
| 10 |
|
| 11 |
# DeepSeek API配置
|
| 12 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY", "")
|
| 13 |
+
DEEPSEEK_BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
| 14 |
|
| 15 |
def check_api_key():
|
| 16 |
"""检查API Key是否配置"""
|
| 17 |
if not DEEPSEEK_API_KEY:
|
| 18 |
raise ValueError("⚠️ 请在 Space Settings 中配置 DEEPSEEK_API_KEY!\n\n前往:Settings → Repository secrets → New secret")
|
| 19 |
|
| 20 |
+
def call_deepseek_api(prompt, model="deepseek-r1-distill-llama-8b", temperature=0.1, stream=False):
|
| 21 |
+
"""使用 OpenAI 客户端调用 DeepSeek API"""
|
| 22 |
check_api_key()
|
| 23 |
|
| 24 |
+
client = OpenAI(
|
| 25 |
+
api_key=DEEPSEEK_API_KEY,
|
| 26 |
+
base_url=DEEPSEEK_BASE_URL,
|
| 27 |
+
)
|
| 28 |
|
| 29 |
+
completion = client.chat.completions.create(
|
| 30 |
+
model=model,
|
| 31 |
+
messages=[{"role": "user", "content": prompt}],
|
| 32 |
+
temperature=temperature,
|
| 33 |
+
stream=stream
|
| 34 |
+
)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
if stream:
|
| 37 |
+
# 流式响应处理
|
| 38 |
+
response_content = ""
|
| 39 |
+
for chunk in completion:
|
| 40 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 41 |
+
response_content += chunk.choices[0].delta.content
|
| 42 |
+
return response_content
|
| 43 |
+
else:
|
| 44 |
+
# 非流式响应
|
| 45 |
+
return completion.choices[0].message.content
|
| 46 |
|
| 47 |
# 预置的Leaderboard数据
|
| 48 |
LEADERBOARD_DATA = [
|