Files
project_main/test/test_llm.py

87 lines
4.8 KiB
Python
Raw Permalink Normal View History

import sys
import os
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_dir)
import erniebot
erniebot.api_type = "qianfan"
erniebot.ak = "jReawMtWhPu0wrxN9Rp1MzZX"
erniebot.sk = "eowS1BqsNgD2i0C9xNnHUVOSNuAzVTh6"
class LLM:
def __init__(self):
self.model = 'ernie-3.5'
2024-08-01 22:54:05 +08:00
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}]
'''
2024-08-01 22:54:05 +08:00
self.prompt += '''请根据上面的示例,解析该任务文本,并返回相应的 JSON 字段。确保 JSON 中包含了键 index action 和 time 以及相应的值'''
self.messages = []
self.resp = None
self.reset()
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())
def make_message(self,content):
return {'role': 'user', 'content': content}
def get_command_json(self,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())
return self.resp.get_result()
if __name__ == "__main__":
lmm_bot = LLM()
while True:
chat = input("输入:")
2024-06-28 21:34:49 +08:00
resp = lmm_bot.get_command_json(chat).replace(' ', '').replace('\n', '').replace('\t', '')
print(eval(resp[7:-3]))