openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.38.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/realtime/realtime.py

54lines · modeblame

3d3d16abstainless-app[bot]9 months ago1#!/usr/bin/env rye run python
2import asyncio
3
4from openai import AsyncOpenAI
5
6# Azure OpenAI Realtime Docs
7
8# How-to: https://learn.microsoft.com/azure/ai-services/openai/how-to/realtime-audio
9# Supported models and API versions: https://learn.microsoft.com/azure/ai-services/openai/how-to/realtime-audio#supported-models
10# Entra ID auth: https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity
11
12
13async def main() -> None:
14"""The following example demonstrates how to configure OpenAI to use the Realtime API.
15For an audio example, see push_to_talk_app.py and update the client and model parameter accordingly.
16
17When prompted for user input, type a message and hit enter to send it to the model.
18Enter "q" to quit the conversation.
19"""
20
21client = AsyncOpenAI()
22async with client.realtime.connect(
23model="gpt-realtime",
24) as connection:
25await connection.session.update(
26session={
27"output_modalities": ["text"],
28"model": "gpt-realtime",
29"type": "realtime",
30}
31)
32while True:
33user_input = input("Enter a message: ")
34if user_input == "q":
35break
36
37await connection.conversation.item.create(
38item={
39"type": "message",
40"role": "user",
41"content": [{"type": "input_text", "text": user_input}],
42}
43)
44await connection.response.create()
45async for event in connection:
46if event.type == "response.output_text.delta":
47print(event.delta, flush=True, end="")
48elif event.type == "response.output_text.done":
49print()
50elif event.type == "response.done":
51break
52
53
54asyncio.run(main())