Skip to content

Commit

Permalink
finished minimal example
Browse files Browse the repository at this point in the history
  • Loading branch information
mhubii committed May 26, 2023
1 parent 85d6ed6 commit d9bac5e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ This demo demonstrates how `ChatGPT` can be used to call into `ROS` services, sp
`ROS` is interfaced via WebSockets through [rosbridge_suite](https://github.com/RobotWebTools/rosbridge_suite). `ChatGPT`:
- Calls into WebSockets
- Does **not** execute any code on your machine
- Is given knowledge about the API via [turtlesim_api.json](turtlesim_api.json)
- Is given knowledge about the API via [turtlesim_msgs/srv](turtlesim_msgs/srv)


Prompt:

```shell
Move the turtle left by 2, then rotate 180 degrees, and move back to (5, 5).
Finally, spawn a turtle named turtle2 at (10, 10) and remove turtle1.
Move turtle1 left by 2, then rotate 180 degrees, and move back to (5, 5). Finally, spawn a turtle named turtle2 at (10, 10) and remove turtle1.
```

Result:
Expand Down
20 changes: 11 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import argparse
import json
import roslibpy
from glob import glob

from prompt import get_service, OpenAIInterface
import roslibpy
from prompt import OpenAIInterface, append_service


def args_factory() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--key", type=str, required=True, help="OpenAI API key.")
parser.add_argument(
"--api", type=str, default="turtlesim_api.json", help="Path to API JSON file."
"--api", type=str, default="turtlesim_msgs/srv", help="Path to API JSON file."
)
parser.add_argument("--host", type=str, default="localhost", help="ROS host.")
parser.add_argument("--port", type=int, default=9090, help="ROS port.")
Expand All @@ -24,9 +25,10 @@ def main() -> None:
args = args_factory()

# load the API
api = None
with open(args.api, "r") as f:
api = json.load(f)
api = []
for api_file in glob(f"{args.api}/*.json"):
with open(api_file, "r") as f:
api.append(json.load(f))

# configure your interface
interface = OpenAIInterface(api=api, key=args.key)
Expand All @@ -49,17 +51,17 @@ def main() -> None:
for call in generated_api_calls:
# get required service (in case they changed)
print("Getting required service. This might take some time...")
services = get_service(ros_client, call["name"], services)
services = append_service(ros_client, call["service"], services)
print("Done.")

try:
print(
"Calling service {} with args {}".format(
call["name"], call["args"]
call["service"], call["args"]
)
)
input("Press Enter to continue...")
service = services[call["name"]]
service = services[call["service"]]
request = roslibpy.ServiceRequest(call["args"])
service.call(request)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion prompt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .utils import get_service
from .openai_interface import OpenAIInterface
from .utils import append_service
2 changes: 2 additions & 0 deletions prompt/openai_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from typing import Any, Dict, List

import openai


Expand All @@ -10,6 +11,7 @@ def __init__(self, api: Any, key: str) -> None:
self.system_prompt_ = f"\
Use this JSON schema to achieve the user's goals:\n\
{str(api)}\n\
Respond as a list of JSON objects.\
Do not include explanations or conversation in the response.\
"
self.chat_history_ = []
Expand Down
3 changes: 2 additions & 1 deletion prompt/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Dict

import roslibpy


def get_service(
def append_service(
client: roslibpy.Ros, name: str, services: Dict[str, roslibpy.Service]
) -> Dict[str, roslibpy.Service]:
"""Update current services with the required one.
Expand Down

0 comments on commit d9bac5e

Please sign in to comment.