|
| 1 | +--- |
| 2 | +title: "Using OpenAI Swarm with Hal9: Unlocking New Potential for Enterprise AI" |
| 3 | +authors: "Karan Saxena" |
| 4 | +tags: |
| 5 | + - "AI" |
| 6 | + - "Swarm Intelligence" |
| 7 | +description: "Explore the integration of OpenAI Swarm with Hal9 to |
| 8 | +unlock innovative AI capabilities" |
| 9 | +--- |
| 10 | + |
| 11 | +## Using OpenAI Swarm with Hal9: Unlocking New Potential for EnterpriseAI |
| 12 | + |
| 13 | +As enterprises seek innovative ways to harness the power of AI, |
| 14 | +combining the best tools for flexibility, speed, and collaborative |
| 15 | +insights becomes essential. OpenAI Swarm is a promising approach that |
| 16 | +leverages multiple instances of AI models working together in a |
| 17 | +coordinated manner, effectively mimicking a \"swarm intelligence\" |
| 18 | +system. When paired with Hal9---a platform optimized for customizing and |
| 19 | +deploying generative AI---OpenAI Swarm opens new horizons for |
| 20 | +enterprise-level solutions. |
| 21 | + |
| 22 | +In this post, we'll dive into what OpenAI Swarm is, why it's a valuable |
| 23 | +asset for enterprise use, and how integrating it with Hal9 can transform |
| 24 | +your AI-driven applications. We\'ll also walk through some example code |
| 25 | +and provide visuals generated with this integration. |
| 26 | + |
| 27 | +### What is OpenAI Swarm? |
| 28 | + |
| 29 | +OpenAI Swarm is a system designed to run multiple AI models in parallel, |
| 30 | +each contributing to a collective response based on the input provided. |
| 31 | +By orchestrating a "swarm" of AI models that analyze the same prompt |
| 32 | +from various perspectives, OpenAI Swarm generates richer, more nuanced |
| 33 | +responses. This can be incredibly useful for applications requiring deep |
| 34 | +analytical insights, creative brainstorming, or decision-making in |
| 35 | +complex scenarios. |
| 36 | + |
| 37 | +### Why Use OpenAI Swarm with Hal9? |
| 38 | + |
| 39 | +Hal9 makes it simple for businesses to customize and deploy AI models at |
| 40 | +scale. By integrating OpenAI Swarm with Hal9, enterprises can harness |
| 41 | +the combined power of multiple AI models tailored specifically for their |
| 42 | +unique needs. This setup is especially advantageous in scenarios where a |
| 43 | +single model may not capture the full spectrum of insights required. |
| 44 | + |
| 45 | +### Example Code |
| 46 | + |
| 47 | +```python |
| 48 | +import json import hal9 as h9 from dotenv import |
| 49 | +load_dotenv from swarm import Swarm, Agent, repl from recommendations |
| 50 | +import book_recommendation, comic_recommendation, movie_recommendation |
| 51 | + |
| 52 | +load_dotenv() |
| 53 | + |
| 54 | +Define transfer functions def transfer_to_receptionist(): return receptionist |
| 55 | + |
| 56 | +def transfer_to_book_expert(): return book_expert |
| 57 | + |
| 58 | +def transfer_to_comic_expert(): return comic_expert |
| 59 | + |
| 60 | +def transfer_to_movie_expert(): return movie_expert |
| 61 | + |
| 62 | +#Define expert agents book_expert = Agent( name="Book Expert", instructions="You are a classic books expert. Provide bookrecommendations. |
| 63 | +#If the conversation drifts away, return to the receptionist.", functions=[book_recommendation, transfer_to_receptionist], ) |
| 64 | + |
| 65 | +comic_expert = Agent( name=\"Comic Expert\", instructions=\"You are an |
| 66 | +expert in comics. Provide comic recommendations. If the conversation |
| 67 | +drifts away, return to the receptionist.\", |
| 68 | +functions=\[comic_recommendation, transfer_to_receptionist\], ) |
| 69 | + |
| 70 | +movie_expert = Agent( name=\"Movie Expert\", instructions=\"You are an |
| 71 | +expert in movies. Provide movie recommendations. If the conversation |
| 72 | +drifts away, return to the receptionist.\", |
| 73 | +functions=\[movie_recommendation, transfer_to_receptionist\], ) |
| 74 | + |
| 75 | +receptionist = Agent( name=\"Receptionist\", instructions=\"You are a |
| 76 | +receptionist. Direct users to a book, comic, or movie expert based on |
| 77 | +their input.\", functions=\[transfer_to_book_expert, |
| 78 | +transfer_to_comic_expert, transfer_to_movie_expert\], ) |
| 79 | + |
| 80 | +client = Swarm() messages = h9.load(\'messages\', \[\]) agents = |
| 81 | +{\'Receptionist\': receptionist, \'Comic Expert\': comic_expert, \'Movie |
| 82 | +Expert\': movie_expert} agent = agents\[h9.load(\'last_agent\', |
| 83 | +\'Receptionist\')\] |
| 84 | + |
| 85 | +\# Handle user input user_input = input() messages.append({\"role\": |
| 86 | +\"user\", \"content\": user_input}) |
| 87 | + |
| 88 | +response = client.run(agent=agent, messages=messages) |
| 89 | + |
| 90 | +for message in response.messages: if message\[\"role\"\] == |
| 91 | +\"assistant\": print(f\"{message\[\'sender\'\]}: |
| 92 | +{message\[\'content\'\]}\") |
| 93 | + |
| 94 | +messages.extend(response.messages) agent = response.agent |
| 95 | + |
| 96 | +h9.save(\'messages\', messages, hidden=True) h9.save(\'last_agent\', |
| 97 | +agent.name, hidden=True) |
| 98 | +``` |
| 99 | + |
| 100 | +## Code Explanation |
| 101 | + |
| 102 | +This code is a recommendation system using a "swarm" of AI agents on the Hal9 platform, each specializing in a category: books, comics, or movies. |
| 103 | + |
| 104 | +#### • Agents: There are four agents: receptionist, book_expert, comic_expert, and movie_expert. The receptionist determines the user’s interest and directs them to the appropriate expert. |
| 105 | +#### • Functionality: Each expert agent provides recommendations in its domain, and if the conversation shifts away, it returns control to the receptionist. |
| 106 | +#### • Session Management: The code maintains conversation history and the last active agent, enabling smooth, continuous interactions. |
| 107 | +#### • Execution: The system takes user input, selects the relevant agent, generates a response, and logs any tool (function) calls made for recommendations. |
| 108 | +This setup allows for interactive, specialized recommendations that adapt to user preferences in real-time. |
| 109 | + |
| 110 | + |
| 111 | +## Sample Conversation |
| 112 | + |
0 commit comments