Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-feature-automate-reference-docs-generation.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
これはインタラクティブなノートブックです。ローカルで実行するか、以下のリンクを使用できます:
音声データでWeaveを使用する方法:OpenAIの例
このデモでは、OpenAIのチャット完了APIとGPT 4o Audio Previewを使用して、テキストプロンプトに対する音声応答を生成し、Weaveでこれらを追跡します。
高度なユースケースでは、OpenAI Realtime APIを活用してリアルタイムで音声をストリーミングします。以下のサムネイルをクリックしてビデオデモを表示するか、ここをクリックしてください。
セットアップ
OpenAI(openai)とWeave(weave)の依存関係、およびAPIキー管理の依存関係set-envをインストールすることから始めます。
%%capture
!pip install openai
!pip install weave
!pip install set-env-colab-kaggle-dotenv -q # for env var
python
%%capture
# Temporary workaround to fix bug in openai:
# TypeError: Client.__init__() got an unexpected keyword argument 'proxies'
# See https://community.openai.com/t/error-with-openai-1-56-0-client-init-got-an-unexpected-keyword-argument-proxies/1040332/15
!pip install "httpx<0.28"
次に、OpenAIとWeaveに必要なAPIキーを読み込みます。ここでは、Google Colabのシークレットキーマネージャーと互換性のあるset_envを使用します。これはColabの特定のgoogle.colab.userdataの代替です。参照:ここで使用方法の説明を確認できます。
# Set environment variables.
from set_env import set_env
_ = set_env("OPENAI_API_KEY")
_ = set_env("WANDB_API_KEY")
最後に必要なライブラリをインポートします。
import base64
import os
import time
import wave
import numpy as np
from IPython.display import display
from openai import OpenAI
import weave
音声ストリーミングと保存の例
ここでは、音声モダリティを有効にしたOpenAIの完了エンドポイントへの呼び出しをセットアップします。まず、OpenAIクライアントを作成し、Weaveプロジェクトを開始します。
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
weave.init("openai-audio-chat")
次に、OpenAIの完了リクエストを定義し、Weaveデコレータ(op)を追加します。
ここでは、関数prompt_endpont_and_log_traceを定義します。この関数には主に3つのステップがあります:
-
テキストと音声の入出力をサポートする
GPT 4o Audio Previewモデルを使用して完了オブジェクトを作成します。
- モデルにさまざまなアクセントでゆっくりと13まで数えるよう指示します。
- 完了を「ストリーム」に設定します。
-
ストリーミングデータがチャンクごとに書き込まれる新しい出力ファイルを開きます。
-
Weaveがトレースに音声データを記録できるように、音声ファイルへのオープンファイルハンドラを返します。
SAMPLE_RATE = 22050
@weave.op()
def prompt_endpoint_and_log_trace(system_prompt=None, user_prompt=None):
if not system_prompt:
system_prompt = "You're the fastest counter in the world"
if not user_prompt:
user_prompt = "Count to 13 super super slow, enunciate each number with a dramatic flair, changing up accents as you go along. British, French, German, Spanish, etc."
# Request from the OpenAI API with audio modality
completion = client.chat.completions.create(
model="gpt-4o-audio-preview",
modalities=["text", "audio"],
audio={"voice": "fable", "format": "pcm16"},
stream=True,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
)
# Open a wave file for writing
with wave.open("./output.wav", "wb") as wav_file:
wav_file.setnchannels(1) # Mono
wav_file.setsampwidth(2) # 16-bit
wav_file.setframerate(SAMPLE_RATE) # Sample rate (adjust if needed)
# Write chunks as they are streamed in from the API
for chunk in completion:
if (
hasattr(chunk, "choices")
and chunk.choices is not None
and len(chunk.choices) > 0
and hasattr(chunk.choices[0].delta, "audio")
and chunk.choices[0].delta.audio.get("data") is not None
):
# Decode the base64 audio data
audio_data = base64.b64decode(chunk.choices[0].delta.audio.get("data"))
# Write the current chunk to the wave file
wav_file.writeframes(audio_data)
# Return the file to Weave op
return wave.open("output.wav", "rb")
テスト
以下のセルを実行します。システムとユーザープロンプトは出力音声とともにWeaveトレースに保存されます。
セルを実行した後、「🍩」絵文字の横にあるリンクをクリックしてトレースを表示します。
from IPython.display import Audio
# Call the function to write the audio stream
prompt_endpoint_and_log_trace(
system_prompt="You're the fastest counter in the world",
user_prompt="Count to 13 super super slow, enunciate each number with a dramatic flair, changing up accents as you go along. British, French, German, Spanish, etc.",
)
# Display the updated audio stream
display(Audio("output.wav", rate=SAMPLE_RATE, autoplay=True))
高度な使用法:WeaveによるリアルタイムオーディオAPI