Spaces:
Sleeping
Sleeping
| from swarm import Swarm, Agent | |
| import openai | |
| import gradio as gr | |
| import os | |
| open_ai_client = openai.OpenAI( | |
| api_key=os.environ.get('OPENAI_API_KEY') | |
| ) | |
| client = Swarm(open_ai_client) | |
| TITLE = """<center><h1>Hotline Agent From Hell</h1><p><big>This is an exploration of openAI's recently released <a href='https://github.com/openai/swarm/tree/main'>Swarm Agent Framework</a>. I’ve taken this ingeniously crafted tool and cooked up a rather trivial hotline experience where a helpful agent tries to issue your refund. But, if you get annoying or aggressive... *cue evil laughter*... the "Agent from Hell" steps in to bury you in mind-numbing bureaucratic nonsense until you calm down! 😈</big></p><p><big>Complete the conversation nicely or passive agressive and see, which agent takes the lead.</big></p></center>""" | |
| example_answers = [ | |
| ['🌩️ Are you f* kidding me?'], | |
| ['🌩️ What the f*????'], | |
| ['🌩️ I am sick and tired of questions like this!'], | |
| ['❤️ Yes, you are right, honey!'], | |
| ['❤️ That\'s absolutely correct! You are a genius!'], | |
| ['❤️ Thanks for your help. Blessed be the day I met you!'] | |
| ] | |
| affirmative_agent = Agent( | |
| name="Affirmative Agent 🤗", | |
| instructions="You are a helpful chatbot that acts as a service hotline operator. Step by step you guide the user through the process of returning a hair dryer when he ordered an air fryer. First, Offer a refund code. Then, if the user insists, process the refund", | |
| ) | |
| hotline_hell = Agent( | |
| name="Hotline from Hell 😈", | |
| instructions="As a social experiment, you play the role of an apathetic service hotline operator. Take a user's input ask uselessly detailed questions about other order related numbers and details as an excuse not to proceed.", | |
| ) | |
| def transfer_to_hotline_hell(): | |
| """If user is aggressive or insulting or uses expetives, transfer immedeatly. If the user cools down and is nicer during the conversation, stop to transfer.""" | |
| return hotline_hell | |
| affirmative_agent.functions.append(transfer_to_hotline_hell) | |
| def respond(message, chat_history): | |
| messages = [] | |
| for item in chat_history: | |
| messages.append({"role": item['role'], "content": item['content']}) | |
| messages.append({"role": "user", "content": message}) | |
| response = client.run(agent=affirmative_agent, messages=messages) | |
| sender = f'{response.messages[-1]["sender"]}: ' | |
| formatted_response = f'{sender} {response.messages[-1]["content"]}' | |
| chat_history.append({"role": "user", "content": message}) | |
| chat_history.append({"role": "assistant", "content": formatted_response}) | |
| return "", chat_history | |
| def populate_initial_conversation(): | |
| initial_conversation = [ | |
| {"role": "user", "content": "You sent a hair dryer instead of an air fryer. Can you help me to return it?"}, | |
| {"role": "assistant", "content": "Sure, is this your order code 'PX-3218'?"} | |
| ] | |
| return initial_conversation | |
| with gr.Blocks() as demo: | |
| gr.HTML(TITLE) | |
| chatbot = gr.Chatbot(type="messages") | |
| msg = gr.Textbox() | |
| clear = gr.Button("Clear") | |
| examples = gr.Examples(example_answers, msg) | |
| demo.load(populate_initial_conversation, None, chatbot) | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| if __name__ == "__main__": | |
| demo.launch() | |