Skip to content

Commit 058f2da

Browse files
authored
Add files via upload
1 parent cc8adf0 commit 058f2da

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
**Using OpenAI Swarm with Hal9: Unlocking New Potential for Enterprise AI**
2+
3+
As enterprises seek innovative ways to harness the power of AI, combining the best tools for flexibility, speed, and collaborative insights becomes essential. OpenAI Swarm is a promising approach that leverages multiple instances of AI models working together in a coordinated manner, effectively mimicking a "swarm intelligence" system. When paired with Hal9—a platform optimized for customizing and deploying generative AI—OpenAI Swarm opens new horizons for enterprise-level solutions.
4+
5+
In this post, we’ll dive into what OpenAI Swarm is, why it’s a valuable asset for enterprise use, and how integrating it with Hal9 can transform your AI-driven applications. We'll also walk through some example code and provide visuals generated with this integration.
6+
7+
**What is OpenAI Swarm?**
8+
9+
OpenAI Swarm is a system designed to run multiple AI models in parallel, each contributing to a collective response based on the input provided. By orchestrating a “swarm” of AI models that analyze the same prompt from various perspectives, OpenAI Swarm generates richer, more nuanced responses. This can be incredibly useful for applications requiring deep analytical insights, creative brainstorming, or decision-making in complex scenarios.
10+
11+
**Why Use OpenAI Swarm with Hal9?**
12+
13+
Hal9 makes it simple for businesses to customize and deploy AI models at scale. By integrating OpenAI Swarm with Hal9, enterprises can harness the combined power of multiple AI models tailored specifically for their unique needs. This setup is especially advantageous in scenarios where a single model may not capture the full spectrum of insights required.
14+
15+
**Code:**
16+
17+
import json
18+
19+
import hal9 as h9
20+
21+
from dotenv import load\_dotenv
22+
23+
from swarm import Swarm, Agent, repl
24+
25+
from recomendations import book\_recommendation, comic\_recommendation, movie\_recommendation
26+
27+
load\_dotenv()
28+
29+
def transfer\_to\_receptionist():
30+
31+
`    `return receptionist
32+
33+
def transfer\_to\_book\_expert():
34+
35+
`    `return book\_expert
36+
37+
def transfer\_to\_comic\_expert():
38+
39+
`    `return comic\_expert
40+
41+
def transfer\_to\_movie\_expert():
42+
43+
`    `return movie\_expert
44+
45+
book\_expert = Agent(
46+
47+
`    `name="Book Expert",
48+
49+
`    `instructions="You are classic books expert, your task is to create book recommendations for the user. If the conversation drifts away from books, return to the receptionist.",
50+
51+
`    `functions=[book\_recommendation, transfer\_to\_receptionist],
52+
53+
)
54+
55+
comic\_expert = Agent(
56+
57+
`    `name="Comic Expert",
58+
59+
`    `instructions="You are an expert in comics, your task is to create comic recommendations for the user. If the conversation drifts away from comics, return to the receptionist.",
60+
61+
`    `functions=[comic\_recommendation, transfer\_to\_receptionist],
62+
63+
)
64+
65+
movie\_expert = Agent(
66+
67+
`    `name="Movie Expert",
68+
69+
`    `instructions="You are an expert in movies, your task is to create movie recommendations for the user. If the conversation drifts away from movies, return to the receptionist.",
70+
71+
`    `functions=[movie\_recommendation, transfer\_to\_receptionist],
72+
73+
)
74+
75+
receptionist = Agent(
76+
77+
`    `name="Receptionist",
78+
79+
`    `instructions="You are a receptionist in a pop culture center. Your task is to figure out if the user would like a random book, comic or movie recommendation.",
80+
81+
`    `functions=[transfer\_to\_book\_expert, transfer\_to\_comic\_expert, transfer\_to\_movie\_expert],
82+
83+
)
84+
85+
client = Swarm()
86+
87+
messages = h9.load('messages', [])
88+
89+
agents = {'Receptionist': receptionist,
90+
91+
`          `'Comic Expert': comic\_expert,
92+
93+
`          `'Movie Expert': movie\_expert}
94+
95+
agent = agents[h9.load('last\_agent', 'Receptionist')]    
96+
97+
user\_input = input()
98+
99+
messages.append({"role": "user", "content": user\_input})
100+
101+
response = client.run(
102+
103+
`    `agent=agent,
104+
105+
`    `messages=messages
106+
107+
)
108+
109+
for message in response.messages:
110+
111+
`    `if message["role"] != "assistant":
112+
113+
`        `continue
114+
115+
`    `print(f"{message['sender']}: ", end=" ")
116+
117+
`    `if message["content"]:
118+
119+
`        `print(message["content"], end = '\n\n')
120+
121+
`    `tool\_calls = message.get("tool\_calls") or []
122+
123+
`    `if len(tool\_calls) > 1:
124+
125+
`        `print('\n\n')
126+
127+
`    `for tool\_call in tool\_calls:
128+
129+
`        `f = tool\_call["function"]
130+
131+
`        `name, args = f["name"], f["arguments"]
132+
133+
`        `arg\_str = json.dumps(json.loads(args)).replace(":", "=")
134+
135+
`        `print(f"calling {name}({arg\_str[1:-1]}) ...", end = '\n\n')
136+
137+
messages.extend(response.messages)
138+
139+
agent = response.agent
140+
141+
h9.save('messages', messages, hidden = True)
142+
143+
h9.save('last\_agent', agent.name, hidden = True)
144+
145+
**Code Explanation**
146+
147+
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.
148+
149+
- **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.
150+
- **Functionality**: Each expert agent provides recommendations in its domain, and if the conversation shifts away, it returns control to the receptionist.
151+
- **Session Management**: The code maintains conversation history and the last active agent, enabling smooth, continuous interactions.
152+
- **Execution**: The system takes user input, selects the relevant agent, generates a response, and logs any tool (function) calls made for recommendations.
153+
154+
This setup allows for interactive, specialized recommendations that adapt to user preferences in real-time.
155+
156+
**Sample Conversation**
157+
158+
![](Aspose.Words.71c44a0c-1802-452f-8239-e78f8287d86b.001.png)

0 commit comments

Comments
 (0)