-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added swarm demo. * modifying github workflows * Added new job: deploying swarm * fixing jobs
- Loading branch information
1 parent
23ca5d1
commit e5c6659
Showing
5 changed files
with
312 additions
and
51 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
import hal9 as h9 | ||
from dotenv import load_dotenv | ||
from swarm import Swarm, Agent, repl | ||
from recomendations import book_recommendation, comic_recommendation, movie_recommendation | ||
|
||
load_dotenv() | ||
|
||
def transfer_to_receptionist(): | ||
return receptionist | ||
|
||
def transfer_to_book_expert(): | ||
return book_expert | ||
|
||
def transfer_to_comic_expert(): | ||
return comic_expert | ||
|
||
def transfer_to_movie_expert(): | ||
return movie_expert | ||
|
||
book_expert = Agent( | ||
name="Book Expert", | ||
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.", | ||
functions=[book_recommendation, transfer_to_receptionist], | ||
) | ||
|
||
comic_expert = Agent( | ||
name="Comic Expert", | ||
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.", | ||
functions=[comic_recommendation, transfer_to_receptionist], | ||
) | ||
|
||
movie_expert = Agent( | ||
name="Movie Expert", | ||
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.", | ||
functions=[movie_recommendation, transfer_to_receptionist], | ||
) | ||
|
||
receptionist = Agent( | ||
name="Receptionist", | ||
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.", | ||
functions=[transfer_to_book_expert, transfer_to_comic_expert, transfer_to_movie_expert], | ||
) | ||
|
||
client = Swarm() | ||
messages = h9.load('messages', []) | ||
|
||
agents = {'Receptionist': receptionist, | ||
'Comic Expert': comic_expert, | ||
'Movie Expert': movie_expert} | ||
agent = agents[h9.load('last_agent', 'Receptionist')] | ||
|
||
user_input = input() | ||
messages.append({"role": "user", "content": user_input}) | ||
|
||
response = client.run( | ||
agent=agent, | ||
messages=messages | ||
) | ||
|
||
for message in response.messages: | ||
if message["role"] != "assistant": | ||
continue | ||
|
||
print(f"{message['sender']}: ", end=" ") | ||
|
||
if message["content"]: | ||
print(message["content"], end = '\n\n') | ||
|
||
tool_calls = message.get("tool_calls") or [] | ||
if len(tool_calls) > 1: | ||
print('\n\n') | ||
|
||
for tool_call in tool_calls: | ||
f = tool_call["function"] | ||
name, args = f["name"], f["arguments"] | ||
arg_str = json.dumps(json.loads(args)).replace(":", "=") | ||
print(f"calling {name}({arg_str[1:-1]}) ...", end = '\n\n') | ||
|
||
messages.extend(response.messages) | ||
agent = response.agent | ||
|
||
h9.save('messages', messages, hidden = True) | ||
h9.save('last_agent', agent.name, hidden = True) |
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,96 @@ | ||
import random | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
def book_recommendation(): | ||
""" | ||
Creates a random book recommendation from Project Gutenberg's top 100 eBooks list. | ||
This function scrapes Project Gutenberg's top 100 eBooks page, selects a random book from the list, | ||
and returns its title and a link to its Project Gutenberg page. | ||
Returns | ||
------- | ||
tuple of str | ||
A tuple containing: | ||
- title : str | ||
The title of the randomly selected book. | ||
- full_link : str | ||
The URL link to the selected book's page on Project Gutenberg. | ||
""" | ||
response = requests.get('https://www.gutenberg.org/browse/scores/top') | ||
soup = BeautifulSoup(response.text, 'html.parser') | ||
|
||
ebooks_section = soup.find_all('ol') | ||
|
||
ebook = random.choice(ebooks_section[0].find_all('li')) | ||
|
||
title = ebook.text.strip() | ||
full_link = f"https://www.gutenberg.org{ebook.find('a')['href']}" | ||
|
||
return (title, full_link) | ||
|
||
def comic_recommendation(): | ||
""" | ||
Returns a random comic recommendation from the Digital Comic Museum's top 100 comics page. | ||
This function scrapes the Digital Comic Museum's page for the top 100 most downloaded comics, | ||
parses the HTML response, collects the titles and their corresponding download links, | ||
and returns a random comic from the list. | ||
Returns | ||
------- | ||
tuple of str | ||
A tuple containing: | ||
- title : str | ||
The title of the randomly selected comic. | ||
- link : str | ||
The full URL to the comic's page on Digital Comic Museum. | ||
""" | ||
response = requests.get("https://digitalcomicmuseum.com/stats.php?ACT=topdl&start=0&limit=100") | ||
soup = BeautifulSoup(response.text, 'html.parser') | ||
|
||
comics_list = [] | ||
for comic in soup.find_all('a', href=True): | ||
if '.php' in comic['href']: | ||
title = comic.text.strip() | ||
link = f"https://digitalcomicmuseum.com/{comic['href']}" | ||
|
||
# Append to list | ||
if title and link: | ||
comics_list.append((title, link)) | ||
|
||
return random.choice(comics_list) | ||
|
||
|
||
def movie_recommendation(): | ||
""" | ||
Returns a random movie recommendation from the Public Domain Torrents website. | ||
This function scrapes the Public Domain Torrents page listing all movies, | ||
parses the HTML response, collects the titles and their corresponding download links, | ||
and returns a random movie from the list. | ||
Returns | ||
------- | ||
tuple of str | ||
A tuple containing: | ||
- title : str | ||
The title of the randomly selected movie. | ||
- link : str | ||
The full URL to the movie's page on Public Domain Torrents. | ||
""" | ||
response = requests.get("https://www.publicdomaintorrents.info/nshowcat.html?category=ALL") | ||
soup = BeautifulSoup(response.text, 'html.parser') | ||
|
||
movies_list = [] | ||
for movie in soup.find_all('a', href=True): | ||
if 'movieid' in movie['href']: | ||
title = movie.text.strip() | ||
link = f"https://digitalcomicmuseum.com/{movie['href']}" | ||
|
||
# Append to list | ||
if title and link: | ||
movies_list.append((title, link)) | ||
|
||
return random.choice(movies_list) |
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 @@ | ||
swarm @ git+https://github.com/openai/swarm@main |