OpenAI

Updated

Integrate an OpenAI LLM into Conversational AI Engine.

OpenAI provides frontier models ranging from lightweight to high-capability.

Sample configuration

The following examples show how to configure OpenAI LLM when starting a conversational AI agent.

from agora_agent import Agent, OpenAI

# client is your configured Agora client
agent = (
    Agent(client)
    .with_stt(...)  # configure your STT vendor
    .with_llm(OpenAI(
        api_key='your-openai-key',  # omit api_key and base_url to use Agora-managed mode
        base_url='https://api.openai.com/v1/chat/completions',
        model='gpt-4o-mini',
        system_messages=[{'role': 'system', 'content': 'You are a helpful chatbot.'}],
        greeting_message='Hello, how can I assist you?',
        failure_message='Please hold on a second.',
        max_history=32,
    ))
    .with_tts(...)  # configure your TTS vendor
)
import { Agent, OpenAI } from 'agora-agents';

// client is your configured Agora client
const agent = new Agent({ client })
  .withStt(/* configure your STT vendor */)
  .withLlm(new OpenAI({
    apiKey: 'your-openai-key',  // omit apiKey and url to use Agora-managed mode
    url: 'https://api.openai.com/v1/chat/completions',
    model: 'gpt-4o-mini',
    systemMessages: [{ role: 'system', content: 'You are a helpful chatbot.' }],
    greetingMessage: 'Hello, how can I assist you?',
    failureMessage: 'Please hold on a second.',
    maxHistory: 32,
  }))
  .withTts(/* configure your TTS vendor */);
import "github.com/AgoraIO/agora-agents-go/v2/agentkit/vendors"

// client is your configured Agora client
agent := agentkit.NewAgent(client).WithStt(/* configure your STT vendor */).
  WithLlm(
    vendors.NewOpenAI(vendors.OpenAIOptions{
        APIKey:  "your-openai-key",  // omit APIKey and BaseURL to use Agora-managed mode
        BaseURL: "https://api.openai.com/v1/chat/completions",
        Model:   "gpt-4o-mini",
        SystemMessages: []map[string]interface{}{
            {"role": "system", "content": "You are a helpful chatbot."},
        },
        GreetingMessage: "Hello, how can I assist you?",
        FailureMessage:  "Please hold on a second.",
        MaxHistory:      agora.Int(32),
    }),
).WithTts(/* configure your TTS vendor */)
  • Managed mode

    To use an OpenAI LLM with Agora-managed credentials, set credential_mode to "managed" in the llm block. When using managed mode, api_key is not required. You can still use the llm field to configure additional settings such as system_messages and greeting_message. For more information, see Use managed mode.

    "llm": {
      "credential_mode": "managed",
      "vendor": "openai",
      "style": "openai",
      "url": "https://api.openai.com/v1/chat/completions",
      "params": {
        "model": "gpt-4.1-mini"
      },
      "system_messages": [
        {
          "role": "system",
          "content": "You are a helpful chatbot."
        }
      ],
      "max_history": 32,
      "greeting_message": "Hello, how can I assist you?",
      "failure_message": "Please hold on a second."
    }
  • BYOK

    "llm": {
        "url": "https://api.openai.com/v1/chat/completions",
        "api_key": "<your_llm_key>",
        "system_messages": [
          {
            "role": "system",
            "content": "You are a helpful chatbot."
          }
        ],
        "max_history": 32,
        "greeting_message": "Hello, how can I assist you?",
        "failure_message": "Please hold on a second.",
        "params": {
          "model": "gpt-4o-mini"
        }
      }

Key parameters

The following parameters are required when using your own API key (BYOK). When using managed mode, api_key is not required. For more information, see Use managed mode.

llm
api_keystring

Create or manage your API key from OpenAI organization settings. Required when using your own API key (BYOK).

urlstring

Use the completions endpoint.

paramsobject
modelstring

Refer to OpenAI models for available models.

For complete llm configuration options, see LLM parameters. For OpenAI-specific options and model capabilities, see the OpenAI API documentation.