Skip to content

Commit

Permalink
clean up v9
Browse files Browse the repository at this point in the history
Former-commit-id: d17c5e7
  • Loading branch information
kyegomez committed Jul 22, 2023
1 parent 658bcac commit 9c02b55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
setup(
name = 'swarms',
packages = find_packages(exclude=[]),
version = '0.9.7',
version = '0.9.8',
license='MIT',
description = 'Swarms - Pytorch',
author = 'Kye Gomez',
Expand Down
19 changes: 12 additions & 7 deletions swarms/swarms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
from swarms.utils.task import Task

class Swarms:
def __init__(self, openai_api_key="", use_vectorstore=True):
def __init__(self, openai_api_key="", use_vectorstore=True, use_async=True):
#openai_api_key: the openai key. Default is empty
if not openai_api_key:
logging.error("OpenAI key is not provided")
raise ValueError("OpenAI API key is required")

self.openai_api_key = openai_api_key
self.use_vectorstore = use_vectorstore
self.use_async = use_async

def initialize_llm(self, llm_class, temperature=0.5):
"""
Expand Down Expand Up @@ -150,7 +151,7 @@ def initialize_boss_node(self, vectorstore, worker_node, llm_class=OpenAI, max_i



async def run_swarms(self, objective):
def run_swarms(self, objective):
"""
Run the swarm with the given objective
Expand All @@ -169,15 +170,19 @@ async def run_swarms(self, objective):

task = boss_node.create_task(objective)
logging.info(f"Running task: {task}")
result = await boss_node.run(task)
if self.use_async:
loop = asyncio.get_event_loop()
result = loop.run_until_complete(boss_node.run(task))
else:
result = boss_node.run(task)
logging.info(f"Completed tasks: {task}")
return result
except Exception as e:
logging.error(f"An error occurred in run_swarms: {e}")
return None

# usage-# usage-
async def swarm(api_key="", objective=""):
def swarm(api_key="", objective=""):
"""
Run the swarm with the given API key and objective.
Expand All @@ -196,8 +201,8 @@ async def swarm(api_key="", objective=""):
logging.error("Invalid objective")
raise ValueError("A valid objective is required")
try:
swarms = Swarms(api_key)
result = await swarms.run_swarms(objective)
swarms = Swarms(api_key, use_async=False) # Turn off async
result = swarms.run_swarms(objective)
if result is None:
logging.error("Failed to run swarms")
else:
Expand Down

0 comments on commit 9c02b55

Please sign in to comment.