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:

In order to integrate OpenAI with your agent, you need an OpenAI account with API access. You can create an account at OpenAI. Once you have an account, you need to create an API key. You can do this by going to the 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 block. Here you can enter your API key and select the OpenAI model you want to use.

1. Configuring OpenAI Block

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” block).
  • Assistant Reply: {{Assistant Message}} (Generated by the OpenAI block).
  1. 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:


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:

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 block returned error”

  • Cause: Misconfigured block 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 Blocks

Tips:

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

Validation and Testing

Before deployment, ensure:

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