Skip to content

Commit

Permalink
Added swarm demo. (#439)
Browse files Browse the repository at this point in the history
* Added swarm demo.

* modifying github workflows

* Added new job: deploying swarm

* fixing jobs
  • Loading branch information
diegoarceof authored Oct 29, 2024
1 parent 23ca5d1 commit e5c6659
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 51 deletions.
96 changes: 68 additions & 28 deletions .github/workflows/apps-devel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,73 @@ on:
- apps/**

jobs:
devel:
setup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.10
uses: actions/setup-python@v1
with:
python-version: '3.10'
- name: Install Hal9
run: pip install hal9
- name: Deploy Echo
run: hal9 deploy apps/echo --name ECHO --access public --url https://api.devel.hal9.com --title Echo --description "Echoes user input"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN_DEVEL }}
- name: Deploy Hal9
run: hal9 deploy apps/hal9 --name hal9 --access public --url https://api.devel.hal9.com --title Hal9 --description "Conversations and content creation"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN_DEVEL }}
- name: Deploy flux
run: hal9 deploy apps/flux --name flux --access public --url https://api.devel.hal9.com --title Flux --description "Photorealistic content creation"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN_DEVEL }}
- name: Deploy Browser
run: hal9 deploy apps/browser --name browser --access unlisted --url https://api.devel.hal9.com --title Browser --description "Capable of browsing the web"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN_DEVEL }}
- name: Deploy OpenAI
run: hal9 deploy apps/openai --name openai --access public --url https://api.devel.hal9.com --title OpenAI --description "Makes use of OpenAI O1"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN_DEVEL }}
- uses: actions/checkout@v1
- name: Set up Python 3.10
uses: actions/setup-python@v1
with:
python-version: '3.10'
- name: Install Hal9
run: pip install hal9

deploy_echo:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/echo') || contains(github.event.commits.*.modified, 'apps/echo')
steps:
- name: Deploy Echo
run: hal9 deploy apps/echo --name ECHO --access public --title Echo --description "Echoes user input"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}

deploy_hal9:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/hal9') || contains(github.event.commits.*.modified, 'apps/hal9')
steps:
- name: Deploy Hal9
run: hal9 deploy apps/hal9 --name hal9 --access public --title Hal9 --description "Conversations and content creation"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}

deploy_flux:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/flux') || contains(github.event.commits.*.modified, 'apps/flux')
steps:
- name: Deploy Flux
run: hal9 deploy apps/flux --name flux --access public --title Flux --description "Photorealistic content creation"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}

deploy_browser:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/browser') || contains(github.event.commits.*.modified, 'apps/browser')
steps:
- name: Deploy Browser
run: hal9 deploy apps/browser --name browser --access unlisted --title Browser --description "Capable of browsing the web"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}

deploy_openai:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/openai') || contains(github.event.commits.*.modified, 'apps/openai')
steps:
- name: Deploy OpenAI
run: hal9 deploy apps/openai --name openai --access public --title OpenAI --description "Makes use of OpenAI O1"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}

deploy_swarm:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/swarm') || contains(github.event.commits.*.modified, 'apps/swarm')
steps:
- name: Deploy Swarm
run: hal9 deploy apps/swarm --name swarm --access public --title Swarm --description "Uses OpenAI's Swarm."
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}
86 changes: 63 additions & 23 deletions .github/workflows/apps-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,73 @@ on:
- apps/**

jobs:
prod:
setup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.10
uses: actions/setup-python@v1
with:
python-version: '3.10'
- name: Install Hal9
run: pip install hal9
- name: Deploy Echo
run: hal9 deploy apps/echo --name ECHO --access public --title Echo --description "Echoes user input"
env:
- uses: actions/checkout@v1
- name: Set up Python 3.10
uses: actions/setup-python@v1
with:
python-version: '3.10'
- name: Install Hal9
run: pip install hal9

deploy_echo:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/echo') || contains(github.event.commits.*.modified, 'apps/echo')
steps:
- name: Deploy Echo
run: hal9 deploy apps/echo --name ECHO --access public --title Echo --description "Echoes user input"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}
- name: Deploy Hal9
run: hal9 deploy apps/hal9 --name hal9 --access public --title Hal9 --description "Conversations and content creation"
env:

deploy_hal9:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/hal9') || contains(github.event.commits.*.modified, 'apps/hal9')
steps:
- name: Deploy Hal9
run: hal9 deploy apps/hal9 --name hal9 --access public --title Hal9 --description "Conversations and content creation"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}

deploy_flux:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/flux') || contains(github.event.commits.*.modified, 'apps/flux')
steps:
- name: Deploy Flux
run: hal9 deploy apps/flux --name flux --access public --title Flux --description "Photorealistic content creation"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}
- name: Deploy Flux
run: hal9 deploy apps/flux --name flux --access public --title Flux --description "Photorealistic content creation"
env:

deploy_browser:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/browser') || contains(github.event.commits.*.modified, 'apps/browser')
steps:
- name: Deploy Browser
run: hal9 deploy apps/browser --name browser --access unlisted --title Browser --description "Capable of browsing the web"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}
- name: Deploy Browser
run: hal9 deploy apps/browser --name browser --access unlisted --title Browser --description "Capable of browsing the web"
env:

deploy_openai:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/openai') || contains(github.event.commits.*.modified, 'apps/openai')
steps:
- name: Deploy OpenAI
run: hal9 deploy apps/openai --name openai --access public --title OpenAI --description "Makes use of OpenAI O1"
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}
- name: Deploy OpenAI
run: hal9 deploy apps/openai --name openai --access public --title OpenAI --description "Makes use of OpenAI O1"
env:

deploy_swarm:
runs-on: ubuntu-latest
needs: setup
if: contains(github.event.commits.*.added, 'apps/swarm') || contains(github.event.commits.*.modified, 'apps/swarm')
steps:
- name: Deploy Swarm
run: hal9 deploy apps/swarm --name swarm --access public --title Swarm --description "Uses OpenAI's Swarm."
env:
HAL9_TOKEN: ${{ secrets.HAL9_TOKEN }}
84 changes: 84 additions & 0 deletions apps/swarm/app.py
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)
96 changes: 96 additions & 0 deletions apps/swarm/recomendations.py
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)
1 change: 1 addition & 0 deletions apps/swarm/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
swarm @ git+https://github.com/openai/swarm@main

0 comments on commit e5c6659

Please sign in to comment.