microsoft / PromptCraft-Robotics

Community for applying LLMs to robotics and a robot simulator with ChatGPT integration
https://aka.ms/ChatGPT-Robotics
MIT License
1.84k stars 195 forks source link

Use official OpenAI ChatGPT API #6

Closed yuan-alex closed 1 year ago

yuan-alex commented 1 year ago

Instead of relying on an unofficial method to access ChatGPT, this PR uses the official API to avoid potential issues or errors that might arise from using an unsupported approach.

However, this also introduces a new challenge: setting a system message. This issue was not addressed in the original paper, so I decided to use one of the system prompts that were used in ChatGPT as a baseline.

yuan-alex commented 1 year ago

@microsoft-github-policy-service agree

saihv commented 1 year ago

@yuan-alex Thanks a lot for adding this integration! We have also recently started testing the API internally.

I am adding a commit with a suggestion on how the 'system' context can be set - essentially, using the first part of the current prompt as the system message, informing ChatGPT how it should act. Feel free to propose any modifications.

yuan-alex commented 1 year ago

@saihv Hey Sai! The multi-line string for the system prompt contains both blanks and newlines. To address this, I suggest adding a new folder specifically for system prompts, which we can read from.

Also, with the new system prompt, ChatGPT responds only with the code and no code-blocks. I've pasted one of the examples below.

# Command the drone to move up 10 units

aw.takeoff() # take off the drone
current_position = aw.get_drone_position() # get current position of the drone
destination = current_position + [0, 0, -10] # add 10 to the Z coordinate
aw.fly_to(destination) # fly the drone to the new position

The new response using your system prompt is cleaner (and saves on token costs!) but it does mean we get less of an explanation from the model. The code block extraction function can be removed but I'm curious on what your take is.

yuan-alex commented 1 year ago

Just tested combining the existing system prompt and first user prompt into one big system prompt and it performs very well. It seems like using both the first user prompts and system prompt to set instructions for ChatGPT causes some confusion and degraded performance.

machinekoder commented 1 year ago

Might be worth taking a look at temperature and other parameters of the official API as well. Temperature 0.0 would be most likely very appropriate for robot tasks that include generating code to make the output more deterministic.

yuan-alex commented 1 year ago

Adding a simple response example is enough to remind ChatGPT isn't in a python shell and is supposed to respond with an explanation.

The following is an example of how you would respond:

Me: move 10 units up
You: ```python
aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1], aw.get_drone_position()[2]+10])

This code uses the fly_to() function to move the drone to a new position that is 10 units up from the current position. It does this by getting the current position of the drone using get_drone_position() and then creating a new list with the same X and Y coordinates, but with the Z coordinate increased by 10. The drone will then fly to this new position using fly_to().

machinekoder commented 1 year ago

@yuan-alex Wouldn't it be better to package the query/response into separate user and assistant type messages with the official API? e.g.

user: 
move 10 units up
assistant: 
"""python
aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1], aw.get_drone_position()[2]+10])
"""

This code uses the `fly_to()` function to move the drone to a new position that is 10 units up from the current position. It does this by getting the current position of the drone using `get_drone_position()` and then creating a new list with the same X and Y coordinates, but with the Z coordinate increased by 10. The drone will then fly to this new position using `fly_to()`.
yuan-alex commented 1 year ago

@machinekoder I haven't encountered any significant performance improvement but I'm adding it anyways since it's been generally recommended.

yuan-alex commented 1 year ago

@saihv Can I get a review on this PR? Looks like some folks are having issues with the current codebase because of changes to the revChatGPT proxy. Should fix #8. thanks

machinekoder commented 1 year ago

@yuan-alex Excellent, makes this more reproducible outside this project as well.

saihv commented 1 year ago

@yuan-alex Thanks for developing this interface! Merged.

yungchentang commented 9 months ago

Hi @yuan-alex , thanks for your commit update; however, I still got some connection issues (WARNING:urllib3.connectionpool:Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000271F1C7E2B0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond')': /v1/chat/completions and openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000271F1C7E670>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))), do you have any idea about this problem? thank you!