> For the complete documentation index, see [llms.txt](https://campus-rover.gitbook.io/lab-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://campus-rover.gitbook.io/lab-notebook/zzz/chatgpt.md).

# Basic Chatgpt ROS interface

by Kirsten Tapalla - Spring 2023\
\
This is a quick guide to connecting to ChatGPT and getting it to generate a message that you can publish to a topic for use in other nodes.<br>

## Necessary Imports:

* `import requests`: used to connect to the internet and post the url to connect to ChatGPT
* `import rospy`: used to publish the response from ChatGPT to a topic
* `from std_msgs.msg import String`: used to publish the response as a ros String message

## Node Initialization and Variables/Parameters:

* Since you want to publish the message into a topic, you will have to initialize a rospy node withing your file using `rospy.init_node('ENTER-NODE-NAME-HERE')`.
* You will also want to initialze a rospy Publisher with the name of the topic you would like to publish the data to, for example: `text_pub = rospy.Publisher('/chatgpt_text', String, queue_size=10)`.
* If you would like to be able to change the prompt you are passing it when running the node, you should add a line allowing you to do this by initializing an input string to get the parameter with the name of what you would like to use to specify the input message you would like to pass it.
  * For example, do this by writing `input_string = rospy.get_param('~chatgpt_prompt')`
  * When setting up your launch file later on, you will want to include a line to handle this argument. This can be done by including `arg name="chatgpt_prompt" default="ENTER-YOUR-DEFAULT-PROMPT-HERE"` into your launch file. You can set the default to whatever default prompt you would like to be passed if you are not giving it a specific one.
* You will also want to add this line into your code to to specify the URL going that will be used to connect to ChatGPT: `url = 'https://api.openai.com/v1/completions'`. Since the chat/text completions model is what we are using the get the output responses, the URL specifies 'completions' at the end.

## ChatGPT Information:

### Headers:

To be able to access ChatGPT, you will need to include the following information in your 'header' dictionary: Content-Type and Authorization. Below is an example of what yours might look like:\
`headers = {`\
&#x20;   `'Content-Type': 'application/json',`\
&#x20;   `'Authorization': 'Bearer INSERT-YOUR-OPENAI-API-KEY-HERE',`\
`}`<br>

### Data:

This will include the information you will want to pass into ChatGPT. The only required field will be specifying the model you want to use, but since you are passing in a prompt, you will also want to include that as well. You will be able to specify the maximum amount of tokens you want ChatGPT to generate, but the best way to get the full output messeage from ChatGPT is to enter the maximum amount for the model you are using. For example, as you can see below I am using the 'text-davinci-003' model, and the maximum tokens that this model can generate is 2048. Furthermore, you can adjust the sampling temperature, which will determine how creative the output of ChatGPT will be. The range goes between 0-2, and higher values will cause it to be more random, while lower values will cause it to be more focused and deterministic. An example of a request body you can make is shown below:\
`data = {`\
&#x20;   `'model': 'text-davinci-003',`\
&#x20;   `'prompt': input_string,`\
&#x20;   `'max_tokens': 2048,`\
&#x20;   `'temperature': 0.5,`\
`}`<br>

## Getting and Publishing the Response

To get the response for your input message from ChatGPT, include the following line in your code `response = requests.post(url, headers=headers, json=data)`. Note that if you are using different names for your variables, you will want to pass in those names in place of 'headers' and 'data'.\
\
To publish your output, you will want to make sure that your request went through. If it did, you will be able to get the output from the json file that was returned in the response variable. An example of how to do this is shown below:\
\
`if response.status_code == 200:`\
&#x20;   `generated_text = response.json()['choices'][0]['text']`\
&#x20;   `text_pub.publish(generated_text)`\
\
By doing all of the steps above, you will be able to connect to ChatGPT, pass it a prompt, and publish its response to that prompt to a topic in ros.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://campus-rover.gitbook.io/lab-notebook/zzz/chatgpt.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
