参照 DeepSeek开放平台接口文档 首次调用 API 提供的 Python 程序,对 B站UP玄冥雨的开源代码(本地部署) 进行一些修改,主要是访问服务器部分,修改后如下,充值即可尝试。
import json
import re
from openai import OpenAI
def generate_robot_actions(user_command):
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
system_prompt = """你是一个工业机械臂控制专家。请将用户指令解析为标准化动作序列。
预定义动作池:
[抓取] 参数:物体名称
[移动] 参数:目标位置 (坐标/语义位置)
[平移] 参数:方向(前/后/左/右)、距离(米)
[放置] 参数:放置位置
[倾斜] 参数:角度(1-90度)
[等待] 参数:秒数
[循环] 参数:次数
输出要求:
1. 使用JSON数组格式描述动作序列
2. 自动补充合理参数值
3. 位置参数优先使用语义描述
4. 复杂操作拆解为多步骤
"""
try:
response = client.chat.completions.create(
model="deepseek-chat", # V3
# model="deepseek-reasoner", # R1
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_command},
],
stream=False
)
content = response.choices[0].message.content
# 尝试直接把 content 作为 JSON 解析
# 如果大模型确实给出了纯 JSON,就能直接解析成功
try:
return json.loads(content)
except json.JSONDecodeError:
# 如果直接解析失败,再去提取三反引号内的 JSON 片段
pass
# 用正则匹配三反引号包裹的 JSON
pattern = r'```json\s*(.*?)\s*```'
matches = re.findall(pattern, content, flags=re.DOTALL)
if matches:
# 如果找到多段,只解析第一段
json_str = matches[0].strip()
try:
return json.loads(json_str)
except json.JSONDecodeError:
pass
# 如果也没有匹配到或解析失败,就返回空列表
return []
except Exception as e:
print(f"请求失败:{e}")
return []
if __name__ == "__main__":
# test_commands = ["抓取苹果"]
# test_commands = ["抓取桌面上的矿泉水"]
test_commands = ["我渴了"]
# test_commands = ["我想看你跳个舞"]
# test_commands = ["我想吃面包"]
# test_commands = ["请帮我打开抽屉,然后关上抽屉"]
for cmd in test_commands:
print(f"输入指令:{cmd}")
actions = generate_robot_actions(cmd)
print("生成动作序列:")
print(json.dumps(actions, indent=2, ensure_ascii=False))
print("\n" + "=" * 50 + "\n")