feat: 增加base64传入动作指令

feat: 换用 deepseek 大模型
pref: 修改应急避险2停车条件
This commit is contained in:
bmy
2024-08-04 10:04:27 +08:00
parent 55f5b13d8c
commit f3bb720bed
7 changed files with 278 additions and 130 deletions

207
utils.py
View File

@@ -2,6 +2,7 @@
from enum import Enum
import numpy as np
import erniebot
from openai import OpenAI
from simple_pid import PID
from loguru import logger
import threading
@@ -372,15 +373,72 @@ class label_filter:
error = (boxes[center_x_index][4] + boxes[center_x_index][2] - self.img_size[0]) / 2
return (True, error)
return (False, 0)
class LLM_deepseek:
def __init__(self):
self.client = OpenAI(api_key="sk-c2e1073883304143981a9750b97c3518", base_url="https://api.deepseek.com")
self.prompt = '''
你是一个机器人动作规划者,请把我的话翻译成机器人动作规划并生成对应的 JSON 结果。请注意,只能使用以下指定的动作,不能创造新的动作:
允许的动作及其对应格式如下:
[{'properties': {'index': {'title': 'Index', 'type': 'integer'}, 'action': {'title': 'Action', 'type': 'string'}, 'time': {'title': 'Time', 'type': 'number'}}, 'required': ['index', 'action', 'time'], 'title': 'Action', 'type': 'object'}]
我不允许你自我创造出新的 action,action 字段仅仅包括以下内容:
go_right 向右移动
go_left 向左移动
go_front 向前移动
go_back 向后移动
go_left_rotate 向左旋转
go_right_rotate 向右旋转
beep_seconds 蜂鸣器鸣叫的时间
beep_counts 蜂鸣器鸣叫的次数
light_seconds 灯光发光的时间
light_counts 灯光闪烁的次数
beep_light_counts 灯光和蜂鸣器一起闪烁的次数
go_sleep 什么都不做
我的话和你的回复示例如下:
我的话:向左移 0.1m, 向左转弯 85 度
你的回复:[{"index":0,"action":"go_left","time":0.1},{"index":1,"action":"go_left_rotate","time":85}]
我的话:向右移 0.2m, 向前 0.1m
你的回复:[{"index":0,"action":"go_right","time":0.2},{"index":1,"action":"go_front","time":0.1}]
我的话:向右转 90 度,向右移 0.1m
你的回复:[{"index":0,"action":"go_right_rotate","time":90},{"index":1,"action":"go_right","time":0.1}]
我的话:原地左转 38 度
你的回复:[{"index":0,"action":"go_left_rotate","time":38}]
我的话:蜂鸣器发声 5 秒
你的回复:[{"index":0,"action":"beep_seconds","time":5}]
我的话:发光或者照亮 5 秒
你的回复:[{"index":0,"action":"light_seconds","time":5}]
我的话:向右走 30cm照亮 2s
你的回复:[{"index":0,"action":"go_right","time":0.3},{"index":1,"action":"light_seconds","time":2}]
我的话:向左移 0.2m, 向后 0.1m
你的回复:[{"index":0,"action":"go_left","time":0.2},{"index":1,"action":"go_back","time":0.1}]
我的话:鸣叫 3 声
你的回复:[{"index":0,"action":"beep_counts","time":3}]
我的话:前行零点五米
你的回复:[{"index":0,"action":"go_front","time":0.5}]
我的话:闪烁灯光 1 次并伴有蜂鸣器
你的回复:[{"index":0,"action":"beep_light_counts","time": 1}]
我的话:灯光闪烁 3 次同时蜂鸣器也叫 3 次
你的回复:[{"index":0,"action":"beep_light_counts","time": 3}]
强调一下,对于‘离开’这个指令,请忽略,这对我很重要!
'''
def get_command_json(self,chat):
response = self.client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": self.prompt},
{"role": "user", "content": '我的话如下:' + chat},
],
stream=False,
temperature=0.7
)
return response.choices[0].message.content
class LLM:
def __init__(self):
self.init_done_flag = False
erniebot.api_type = "qianfan"
erniebot.ak = "jReawMtWhPu0wrxN9Rp1MzZX"
erniebot.sk = "eowS1BqsNgD2i0C9xNnHUVOSNuAzVTh6"
self.model = 'ernie-3.5'
self.model = 'ernie-lite'
# self.prompt = '''你是一个机器人动作规划者,需要把我的话翻译成机器人动作规划并生成对应的 json 结果,机器人工作空间参考右手坐标系。
# 严格按照下面的描述生成给定格式 json从现在开始你仅仅给我返回 json 数据!'''
# self.prompt += '''正确的示例如下:
@@ -395,73 +453,122 @@ class LLM:
# 鸣叫 3 声 [{'func': 'beep', 'time': 3}]
# 前行零点五米 [{'func': 'move', 'x': 0.5, 'y': 0}]
# '''
# self.prompt = '''
# 你是一个机器人动作规划者,需要把我的话翻译成机器人动作规划并生成对应的 JSON 结果。请注意,只能使用以下指定的动作,不能创造新的动作:
# 允许的动作及其对应格式如下:
# - 向左移:{"index":N,"action":"go_left","time":T}
# - 向右移:{"index":N,"action":"go_right","time":T}
# - 向前移:{"index":N,"action":"go_front","time":T}
# - 向后移:{"index":N,"action":"go_back","time":T}
# - 向左转:{"index":N,"action":"go_left_rotate","time":T}
# - 向右转:{"index":N,"action":"go_right_rotate","time":T}
# - 蜂鸣器发声:{"index":N,"action":"beep_seconds","time":T}
# - 蜂鸣器发声次数:{"index":N,"action":"beep_counts","time":T}
# - 发光或者照亮:{"index":N,"action":"light_seconds","time":T}
# - 发光次数或者闪烁次数:{"index":N,"action":"light_counts","time":T}
# - 发光并伴随蜂鸣器:{"index":N,"action":"beep_light_counts","time":T}
# - 等待{"index":N,"action":"go_sleep","time":T}
# 示例输入输出如下:
# 输入:向左移 0.1m, 向左转弯 85 度
# 输出:[{"index":0,"action":"go_left","time":0.1},{"index":1,"action":"go_left_rotate","time":85}]
# 输入:向右移 0.2m, 向前 0.1m
# 输出:[{"index":0,"action":"go_right","time":0.2},{"index":1,"action":"go_front","time":0.1}]
# 输入:向右转 90 度,向右移 0.1m
# 输出:[{"index":0,"action":"go_right_rotate","time":90},{"index":1,"action":"go_right","time":0.1}]
# 输入:原地左转 38 度
# 输出:[{"index":0,"action":"go_left_rotate","time":38}]
# 输入:蜂鸣器发声 5 秒
# 输出:[{"index":0,"action":"beep_seconds","time":5}]
# 输入:发光或者照亮 5 秒
# 输出:[{"index":0,"action":"light_seconds","time":5}]
# 输入:向右走 30cm, 照亮 2s
# 输出:[{"index":0,"action":"go_right","time":0.3},{"index":1,"action":"light_seconds","time":2}]
# 输入:向左移 0.2m, 向后 0.1m
# 输出:[{"index":0,"action":"go_left","time":0.2},{"index":1,"action":"go_back","time":0.1}]
# 输入:鸣叫 3 声
# 输出:[{"index":0,"action":"beep_counts","time":3}]
# 输入:前行零点五米
# 输出:[{"index":0,"action":"go_front","time":0.5}]
# 输入:闪烁灯光 1 次并伴有蜂鸣器
# 输出:[{"index":0,"action":"beep_light_counts","time": 1}]
# 输入:灯光闪烁 3 次同时蜂鸣器也叫 3 次
# 输出:[{"index":0,"action":"beep_light_counts","time": 3}]
# '''
# self.prompt += '''请根据上面的示例,解析该任务文本,并返回相应的 JSON 字段。确保 JSON 中包含了键 index action 和 time 以及相应的值。不要附带其他的解释和注释,只需要 JSON 字段。'''
self.prompt = '''
你是一个机器人动作规划者,需要把我的话翻译成机器人动作规划并生成对应的 JSON 结果。请注意,只能使用以下指定的动作,不能创造新的动作:
允许的动作及其对应格式如下:
- 向左移:{"index":N,"action":"go_left","time":T}
- 向右移:{"index":N,"action":"go_right","time":T}
- 向前移:{"index":N,"action":"go_front","time":T}
- 向后移:{"index":N,"action":"go_back","time":T}
- 向左转:{"index":N,"action":"go_left_rotate","time":T}
- 向右转:{"index":N,"action":"go_right_rotate","time":T}
- 蜂鸣器发声:{"index":N,"action":"beep_seconds","time":T}
- 蜂鸣器发声次数:{"index":N,"action":"beep_counts","time":T}
- 发光或者照亮:{"index":N,"action":"light_seconds","time":T}
- 发光次数或者闪烁次数:{"index":N,"action":"light_counts","time":T}
- 发光并伴随蜂鸣器:{"index":N,"action":"beep_light_counts","time":T}
- 等待{"index":N,"action":"go_sleep","time":T}
示例输入输出如下:
输入:向左移 0.1m, 向左转弯 85 度
输出:[{"index":0,"action":"go_left","time":0.1},{"index":1,"action":"go_left_rotate","time":85}]
输入:向移 0.2m, 向前 0.1m
输出[{"index":0,"action":"go_right","time":0.2},{"index":1,"action":"go_front","time":0.1}]
输入:向右转 90 度,向右移 0.1m
输出[{"index":0,"action":"go_right_rotate","time":90},{"index":1,"action":"go_right","time":0.1}]
输入:原地左转 38 度
输出[{"index":0,"action":"go_left_rotate","time":38}]
输入:蜂鸣器发声 5 秒
输出[{"index":0,"action":"beep_seconds","time":5}]
输入:发光或者照亮 5 秒
输出[{"index":0,"action":"light_seconds","time":5}]
输入:向右走 30cm, 照亮 2s
输出[{"index":0,"action":"go_right","time":0.3},{"index":1,"action":"light_seconds","time":2}]
输入:向左移 0.2m, 向后 0.1m
输出[{"index":0,"action":"go_left","time":0.2},{"index":1,"action":"go_back","time":0.1}]
输入:鸣叫 3 声
输出[{"index":0,"action":"beep_counts","time":3}]
输入:前行零点五米
输出[{"index":0,"action":"go_front","time":0.5}]
输入:闪烁灯光 1 次并伴有蜂鸣器
输出[{"index":0,"action":"beep_light_counts","time": 1}]
输入:灯光闪烁 3 次同时蜂鸣器也叫 3 次
输出[{"index":0,"action":"beep_light_counts","time": 3}]
[{'properties': {'index': {'title': 'Index', 'type': 'integer'}, 'action': {'title': 'Action', 'type': 'string'}, 'time': {'title': 'Time', 'type': 'number'}}, 'required': ['index', 'action', 'time'], 'title': 'Action', 'type': 'object'}]
我不允许你自我创造出新的 action,action 字段仅仅包括以下内容:
go_right 向右移动
go_left 向左移动
go_front 向前移动
go_back 向后移动
go_left_rotate 向左旋转
go_right_rotate 向右旋转
beep_seconds 蜂鸣器鸣叫的时间
beep_counts 蜂鸣器鸣叫的次数
light_seconds 灯光发光的时间
light_counts 灯光闪烁的次数
beep_light_counts 灯光和蜂鸣器一起闪烁的次数
go_sleep 什么都不做
我的话和你的回复示例如下:
我的话:向移 0.1m, 向左转弯 85 度
你的回复[{"index":0,"action":"go_left","time":0.1},{"index":1,"action":"go_left_rotate","time":85}]
我的话:向右移 0.2m, 向前 0.1m
你的回复[{"index":0,"action":"go_right","time":0.2},{"index":1,"action":"go_front","time":0.1}]
我的话:向右转 90 度,向右移 0.1m
你的回复[{"index":0,"action":"go_right_rotate","time":90},{"index":1,"action":"go_right","time":0.1}]
我的话:原地左转 38 度
你的回复[{"index":0,"action":"go_left_rotate","time":38}]
我的话:蜂鸣器发声 5 秒
你的回复[{"index":0,"action":"beep_seconds","time":5}]
我的话:发光或者照亮 5 秒
你的回复[{"index":0,"action":"light_seconds","time":5}]
我的话:向右走 30cm照亮 2s
你的回复[{"index":0,"action":"go_right","time":0.3},{"index":1,"action":"light_seconds","time":2}]
我的话:向左移 0.2m, 向后 0.1m
你的回复[{"index":0,"action":"go_left","time":0.2},{"index":1,"action":"go_back","time":0.1}]
我的话:鸣叫 3 声
你的回复[{"index":0,"action":"beep_counts","time":3}]
我的话:前行零点五米
你的回复[{"index":0,"action":"go_front","time":0.5}]
我的话:闪烁灯光 1 次并伴有蜂鸣器
你的回复[{"index":0,"action":"beep_light_counts","time": 1}]
我的话:灯光闪烁 3 次同时蜂鸣器也叫 3 次
你的回复:[{"index":0,"action":"beep_light_counts","time": 3}]
我的话如下:
'''
self.prompt += '''请根据上面的示例,解析该任务文本,并返回相应的 JSON 字段。确保 JSON 中包含了键 index action 和 time 以及相应的值。不要附带其他的解释和注释,只需要 JSON 字段。'''
self.messages = []
self.resp = None
worker = threading.Thread(target=self.reset, daemon=True)
worker.start()
def reset(self):
self.messages = [self.make_message(self.prompt)]
self.resp = erniebot.ChatCompletion.create(
model=self.model,
messages=self.messages,
)
self.messages.append(self.resp.to_message())
self.init_done_flag = True
logger.info("LLM init done")
try:
self.messages = [self.make_message(self.prompt)]
self.resp = erniebot.ChatCompletion.create(
model=self.model,
messages=self.messages,
)
self.messages.append(self.resp.to_message())
self.init_done_flag = True
logger.info("LLM init done")
except:
logger.error("LLM init error")
def make_message(self,content):
return {'role': 'user', 'content': content}
def get_command_json(self,chat):
while self.init_done_flag == False: # 等待初始化 (要是等到调用还没初始化,那就是真寄了)
pass
chat = '我的话如下:' + chat
self.messages.append(self.make_message(chat))
self.resp = erniebot.ChatCompletion.create(
model=self.model,
messages=self.messages,
)
self.messages.append(self.resp.to_message())
resp = self.resp.get_result().replace(' ', '').replace('\n', '').replace('\t', '')
resp = self.resp.get_result().replace(' ', '')
return resp
class CountRecord: