Created
November 12, 2024 06:30
-
-
Save xianminx/a270b0f1017eddfc71b17468624a444e to your computer and use it in GitHub Desktop.
Swarm cannot support multi agents to work concurrently.
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
from typing import List | |
from swarm import Agent | |
from swarm.repl.repl import run_demo_loop | |
# Simulated music player and lyrics database | |
music_library = { | |
"Bohemian Rhapsody": { | |
"artist": "Queen", | |
"lyrics": "Is this the real life? Is this just fantasy?...", | |
}, | |
"Imagine": { | |
"artist": "John Lennon", | |
"lyrics": "Imagine there's no heaven, it's easy if you try...", | |
}, | |
"Billie Jean": { | |
"artist": "Michael Jackson", | |
"lyrics": "She was more like a beauty queen from a movie scene...", | |
}, | |
} | |
def play_music(song_name: str) -> str: | |
if song_name in music_library: | |
return f"Now playing: {song_name} by {music_library[song_name]['artist']}" | |
return f"Song '{song_name}' not found in the library." | |
def show_lyrics(song_name: str) -> str: | |
if song_name in music_library: | |
return f"Lyrics for {song_name}:\n\n{music_library[song_name]['lyrics']}" | |
return f"Lyrics for '{song_name}' not found." | |
# Player Agent | |
player_agent = Agent( | |
name="Music Player", | |
instructions=""" | |
You are a music player agent. Your job is to play the requested song using the play_music function. | |
""", | |
functions=[play_music], | |
model="gpt-4o-mini", | |
) | |
# Script Agent | |
script_agent = Agent( | |
name="Lyrics Display", | |
instructions=""" | |
You are a lyrics display agent. Your job is to show the lyrics of the requested song using the show_lyrics function. | |
""", | |
functions=[show_lyrics], | |
model="gpt-4o-mini", | |
) | |
def transfer_to_player_agent(): | |
return player_agent | |
def transfer_to_lyrics_agent(): | |
return script_agent | |
triage_agent = Agent( | |
name="Music Triage", | |
instructions=""" | |
You are a music triage agent. Your job is to understand the user's request and decide which agents to use. | |
Use the choose_agents function to select the appropriate agent(s) based on the user's request. | |
If the request is specifically for playing music, use transfer_to_player_agent. | |
If the request is specifically for showing lyrics, use transfer_to_lyrics_agent. | |
""", | |
functions=[transfer_to_player_agent, transfer_to_lyrics_agent], | |
model="gpt-4o-mini", | |
) | |
def main(): | |
print("Welcome to the Music Agent Demo!") | |
print("You can ask to play songs, show lyrics, or both.") | |
print("Type 'quit' to exit.") | |
run_demo_loop(triage_agent, stream=True, debug=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment