TutorialDecember 10, 20246 min read

OpenAI Agents SDK with Voice: Build Function-Calling AI Assistants

Learn to integrate voice with OpenAI function calling. Create AI assistants that can speak their responses using GPT-4 and LangVoice.

LT

LangVoice Team

Engineering

OpenAI Agents SDK with Voice: Build Function-Calling AI Assistants

OpenAI Agents SDK with Voice

OpenAI's function calling enables AI to take actions. Adding voice makes interactions natural.

What is Function Calling?

GPT-4 can intelligently decide when to call external functions:

  • Get weather data
  • Search databases
  • Generate speech with LangVoice

Voice Integration Setup

from openai import OpenAI
from langvoice_sdk.tools import LangVoiceOpenAITools

client = OpenAI()
langvoice = LangVoiceOpenAITools(api_key="your-langvoice-key")

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a voice assistant."},
        {"role": "user", "content": "Say hello in a friendly way"}
    ],
    tools=langvoice.get_tools(),
)

# Handle the tool call
if response.choices[0].message.tool_calls:
    for call in response.choices[0].message.tool_calls:
        result = langvoice.handle_call(call)
        langvoice.save_audio_from_result(result, "output.mp3")

Advanced: Streaming Responses

# Stream both text and generate audio
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    stream=True
)

# Collect response and speak
full_response = ""
for chunk in stream:
    if chunk.choices[0].delta.content:
        full_response += chunk.choices[0].delta.content
        print(chunk.choices[0].delta.content, end="")

# Generate audio at the end
audio = langvoice.generate(text=full_response, voice="heart")

Build powerful voice assistants with OpenAI and LangVoice!

Tags

OpenAIfunction callingGPT-4voice assistantAI agentsAPItext to speech

Ready to Transform Your Text to Speech?

Try LangVoice free and experience the most natural AI voices for your content.

Try LangVoice Free

Related Articles

The Complete Guide to AI Voice Generators in 2024
Guide

The Complete Guide to AI Voice Generators in 2024

Discover how AI voice technology has evolved and learn how to choose the best text-to-speech solution for your needs. From podcasts to audiobooks, AI voices are revolutionizing content creation.