-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Added OpenAI audio agent cookbook example ## Type of change Please check the options that are relevant: - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Model update - [ ] Infrastructure change ## Checklist - [ ] My code follows Phidata's style guidelines and best practices - [ ] I have performed a self-review of my code - [ ] I have added docstrings and comments for complex logic - [ ] My changes generate no new warnings or errors - [ ] I have added cookbook examples for my new addition (if needed) - [ ] I have updated requirements.txt/pyproject.toml (if needed) - [ ] I have verified my changes in a clean environment
- Loading branch information
Showing
7 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.jpg | ||
*.png | ||
*.mp3 | ||
*.wav | ||
*.mp4 | ||
*.mp3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import base64 | ||
import requests | ||
from phi.agent import Agent, RunResponse # noqa | ||
from phi.model.openai import OpenAIChat | ||
|
||
# Fetch the audio file and convert it to a base64 encoded string | ||
url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav" | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
wav_data = response.content | ||
encoded_string = base64.b64encode(wav_data).decode("utf-8") | ||
|
||
# Provide the agent with the audio file and get result as text | ||
agent = Agent( | ||
model=OpenAIChat(id="gpt-4o-audio-preview", modalities=["text"]), | ||
markdown=True, | ||
) | ||
agent.print_response("What is in this audio?", audio={"data": encoded_string, "format": "wav"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import base64 | ||
import requests | ||
from phi.agent import Agent, RunResponse # noqa | ||
from phi.model.openai import OpenAIChat | ||
from phi.utils.audio import write_audio_to_file | ||
|
||
# Fetch the audio file and convert it to a base64 encoded string | ||
url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav" | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
wav_data = response.content | ||
encoded_string = base64.b64encode(wav_data).decode("utf-8") | ||
|
||
# Provide the agent with the audio file and audio configuration and get result as text + audio | ||
agent = Agent( | ||
model=OpenAIChat( | ||
id="gpt-4o-audio-preview", modalities=["text", "audio"], audio={"voice": "alloy", "format": "wav"} | ||
), | ||
markdown=True, | ||
) | ||
agent.print_response("What is in this audio?", audio={"data": encoded_string, "format": "wav"}) | ||
|
||
# Save the response audio to a file | ||
if agent.run_response.response_audio is not None and "data" in agent.run_response.response_audio: | ||
write_audio_to_file(audio=agent.run_response.response_audio["data"], filename="tmp/dog.wav") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters