> ## Documentation Index
> Fetch the complete documentation index at: https://docs.release0.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to integrate your Agent with OpenAI

> Learn how to configure OpenAI’s GPT model for chat completions, tool integrations, and conversation history management.

This guide explains how to configure a chat completion flow with OpenAI’s GPT model, integrate tools (e.g., a weather function), and manage conversation history.

### **Prerequisites**:

To integrate OpenAI with your agent, you’ll need an OpenAI account with API access. You can create an account at [OpenAI](https://platform.openai.com/signup).
Once you have an account, you need to create an API key. You can do this by going to the [API keys](https://platform.openai.com/account/api-keys) section of your **OpenAI account** and clicking on the **Create API Key** button. This will generate a new API key that you can use to authenticate your requests to the OpenAI API.
Once you have your API key, you can add it to your agent by configuring the  **OpenAI** element. Here you can enter your API key and select the OpenAI model you want to use.

> **Note**: If you are using the family model **GPT-5**, there is an additional requirement you need to fullfill in your **OpenAI account**. Your organization must be verified to stream this model. Please go to: [https://platform.openai.com/settings/organization/general](https://platform.openai.com/settings/organization/general) and click on **Verify Organization**. If you just verified, it can take up to 15 minutes for access to propagate.

<iframe width="560" height="315" src="https://www.youtube.com/embed/a4TMzobl-m0" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowFullScreen />

## 1. Configuring OpenAI element

### Steps:

1. **Select Model**: Use `gpt-3.5-turbo` for optimal cost-performance balance.
2. **Provide Messages**:
   * Create a variable for storing **chat history** (e.g., `Chat history`).
   * Append user and assistant messages sequentially to ensure conversational context.

#### Example Chat Flow:

* **User Input**: `{{User Message}}` (Captured via a “user input text” element).
* **Assistant Reply**: `{{Assistant Message}}` (Generated by the OpenAI element).

3. **Set Variables**:
   * Define the variable for appending the assistant response (`Append {{Assistant Message}} in Chat history`).

### Use case example. Creating a chatbot agent for sales support:

<iframe width="560" height="315" src="https://www.youtube.com/embed/S6hfIxkdlRc" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowFullScreen />

***

## 2. Using Tools: Adding Functions

### Purpose:

Functions allow GPT to execute server-side code, such as fetching real-time weather data.

### Example Function: `getWeather`

* **Name**: `getWeather`
* **Description**: *Get the weather of the provided city*.
* **Parameter**:
  * **Name**: `city`
  * **Type**: `string`
  * **Description**: *The name of the city*.
  * **Required**: Enabled.

#### Code Example:

```javascript theme={null}
if (city === 'Cancun') {
  return 'Sunny and warm';
} else {
  return 'Rainy and cold';
}
```

**Note**: Replace the static logic with an API call for real-world data retrieval.

***

## 3. Setting Up Conversation History

### Variables:

* **Chat history**:
  * Value: Append both user and assistant messages.
  * Example:
    * User Message: `"What is the weather in Cancun?"`
    * Assistant Response: `"Sunny and warm"`.

#### Diagram Flow:

1. **Collect User Message**:
   * Append user input to the `Chat history` variable.
2. **ChatGPT Reply**:
   * Generate the assistant message and append it to `Chat history`.

***

## 4. Integrating Speech and Transcription

### **Create Speech**:

* Converts text into an **audio URL**.
* **Limitation**: Audio URLs expire after 7 days. Download them promptly if needed.

### **Create Transcription**:

* Converts audio inputs into text for further processing.

***

## 5. Troubleshooting Common Issues

### Error: “OpenAI element returned error”

* **Cause**: Misconfigured element settings.
* **Fix**:
  1. Ensure an OpenAI account is selected.
  2. Verify at least one message or Dialogue input is provided.

### Issue: Empty Messages

* **Cause**: Free quota exceeded.
* **Fix**: Add a payment method to your OpenAI account.

***

## 6. Using Multiple OpenAI elements

### Tips:

1. **Sequential Processing**: Ensure all elements compute before rendering the output.
2. **Formatting**: Avoid adding unnecessary prefixes or suffixes that disrupt conversational flow.

***

## 7. Advanced Features: RAG (Retrieval-Augmented Generation)

👉 See our [RAG agent example](https://release0.com/blog/how-to-create-an-agent-with-rag-in-under-5minutes-in-release0) for practical setup tips.

## Validation and Testing

Before deployment, ensure:

* All **functions** execute as expected.
* **Chat history** updates without errors.
* Responses are relevant and correctly formatted.
