dora-robomaster / operators /chatgpt_op.py
haixuantao's picture
Modify data organization
c94c99e
raw
history blame
No virus
3.17 kB
import os
from openai import OpenAI
def ask_gpt(prompt, raw):
client = OpenAI()
prompt = (
"this is a python code :\n"
+ "```python\n"
+ raw
+ "```\n"
+ prompt
+ "Format your response by: Showing the whole modified code. No explanation is required. Only code."
)
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
)
answer = response.choices[0].message.content
return prompt, answer
def extract_command(gptCommand):
blocks = []
temp = ""
writing = False
for line in gptCommand.splitlines():
if line == "```":
writing = False
blocks.append(temp)
temp = ""
if writing:
temp += line
temp += "\n"
if line == "```python":
writing = True
return blocks
def save_as(content, path):
# use at the end of replace_2 as save_as(end_result, "file_path")
with open(path, "w") as file:
file.write(content)
import pyarrow as pa
from dora import DoraStatus
class Operator:
"""
Infering object from images
"""
def on_event(
self,
dora_event,
send_output,
) -> DoraStatus:
# todo: remove this
return DoraStatus.CONTINUE
if dora_event["type"] == "INPUT":
input = dora_event["value"][0].as_py()
with open(input["path"], "r", encoding="utf8") as f:
raw = f.read()
print("--- Asking chatGPT ", flush=True)
prompt, response = ask_gpt(input["query"], raw)
blocks = extract_command(response)
print(response, flush=True)
print(blocks[0], input["path"], flush=True)
send_output(
"output_file",
pa.array(
[
{
"raw": blocks[0],
"path": input["path"],
"response": response,
"prompt": prompt,
}
]
),
dora_event["metadata"],
)
return DoraStatus.CONTINUE
if __name__ == "__main__":
op = Operator()
# Path to the current file
current_file_path = __file__
# Directory of the current file
current_directory = os.path.dirname(current_file_path)
path = current_directory + "/planning_op.py"
with open(path, "r", encoding="utf8") as f:
raw = f.read()
op.on_event(
{
"type": "INPUT",
"id": "tick",
"value": pa.array(
[
{
"raw": raw,
"path": path,
"query": "Can you change the RGB to change according to the object distances",
}
]
),
"metadata": [],
},
print,
)