Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
技术与健康
deep backend
提交
c6ac4f79
D
deep backend
项目概览
技术与健康
/
deep backend
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deep backend
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
c6ac4f79
编写于
4月 17, 2025
作者:
P
practicer2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Thu Apr 17 21:44:00 CST 2025 inscode
上级
3bf8e579
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
39 addition
and
19 deletion
+39
-19
main.py
main.py
+39
-19
未找到文件。
main.py
浏览文件 @
c6ac4f79
...
@@ -8,31 +8,50 @@ app = Flask(__name__)
...
@@ -8,31 +8,50 @@ app = Flask(__name__)
API_URL
=
"https://api.deepseek.com"
API_URL
=
"https://api.deepseek.com"
API_KEY
=
os
.
getenv
(
'DeepSeek_API_KEY'
)
API_KEY
=
os
.
getenv
(
'DeepSeek_API_KEY'
)
def
call_deepseek
(
prompt
,
m
ax_new_tokens
=
150
):
def
call_deepseek
(
prompt
,
m
odel
=
"deepseek-chat"
,
max_tokens
=
2048
,
temperature
=
0.7
):
"""
"""
调用 DeepSeek API 生成文本
调用 DeepSeek API 的函数
:param prompt: 输入的提示文本
:param max_new_tokens: 生成的最大新 token 数
参数:
:return: 生成的文本
- 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
=
{
headers
=
{
"Content-Type"
:
"application/json"
,
"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
:
try
:
response
=
requests
.
post
(
API_URL
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
# 发送POST请求
response
.
raise_for_status
()
response
=
requests
.
post
(
api_url
,
headers
=
headers
,
json
=
payload
)
result
=
response
.
json
().
get
(
"generated_text"
)
response
.
raise_for_status
()
# 检查是否有错误
return
result
except
requests
.
RequestException
as
e
:
# 返回API响应
print
(
f
"调用 DeepSeek API 时请求出错:
{
e
}
"
)
return
response
.
json
()
except
(
KeyError
,
json
.
JSONDecodeError
)
as
e
:
print
(
f
"解析 DeepSeek API 响应时出错:
{
e
}
"
)
except
requests
.
exceptions
.
RequestException
as
e
:
return
None
print
(
f
"请求出错:
{
e
}
"
)
return
None
@
app
.
route
(
'/generate'
,
methods
=
[
'POST'
])
@
app
.
route
(
'/generate'
,
methods
=
[
'POST'
])
def
generate_text
():
def
generate_text
():
...
@@ -51,4 +70,5 @@ def generate_text():
...
@@ -51,4 +70,5 @@ def generate_text():
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
app
.
run
(
debug
=
True
)
app
.
run
(
debug
=
True
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录