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
Ready to Transform Your Text to Speech?
Try LangVoice free and experience the most natural AI voices for your content.
Try LangVoice Free


