Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Issue #20: Environment Variable Loading #25

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions praisonai/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def __init__(self, topic="Movie Story writing about AI", agent_file="test.yaml",
self.topic = topic
self.agent_file = agent_file
self.framework = framework
if not os.getenv("OPENAI_API_KEY"):
raise EnvironmentError("The OPENAI_API_KEY environment variable is not set.")
self.client = instructor.patch(
OpenAI(
base_url=self.config_list[0]['base_url'],
Expand All @@ -41,20 +43,34 @@ def __init__(self, topic="Movie Story writing about AI", agent_file="test.yaml",
mode=instructor.Mode.JSON,
)

import time

def generate(self):
response = self.client.chat.completions.create(
model=self.config_list[0]['model'],
response_model=TeamStructure,
max_retries=10,
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output complex team structures."},
{"role": "user", "content": self.get_user_content()}
]
)
json_data = json.loads(response.model_dump_json())
self.convert_and_save(json_data)
full_path = os.path.abspath(self.agent_file)
return full_path
retry_delay = 1 # Start with a 1 second delay
max_retries = 5 # Maximum number of retries
retries = 0

while retries < max_retries:
try:
response = self.client.chat.completions.create(
model=self.config_list[0]['model'],
response_model=TeamStructure,
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output complex team structures."},
{"role": "user", "content": self.get_user_content()}
]
)
json_data = json.loads(response.model_dump_json())
self.convert_and_save(json_data)
full_path = os.path.abspath(self.agent_file)
return full_path
except openai.error.RateLimitError:
print(f"RateLimitError: Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # Exponential backoff
retries += 1

raise Exception("Max retries exceeded for API call")

def convert_and_save(self, json_data):
"""Converts the provided JSON data into the desired YAML format and saves it to a file.
Expand Down Expand Up @@ -93,16 +109,16 @@ def convert_and_save(self, json_data):
yaml.dump(yaml_data, f, allow_unicode=True, sort_keys=False)

def get_user_content(self):
user_content = """Generate a team structure for \"""" + self.topic + """\" task.
user_content = """Generate a team structure for \"""" + self.topic + """\" task.
No Input data will be provided to the team.
The team will work in sequence. First role will pass the output to the next role, and so on.
The last role will generate the final output.
Think step by step.
With maximum 3 roles, each with 1 task. Include role goals, backstories, task descriptions, and expected outputs.
List of Available Tools: CodeDocsSearchTool, CSVSearchTool, DirectorySearchTool, DOCXSearchTool, DirectoryReadTool, FileReadTool, TXTSearchTool, JSONSearchTool, MDXSearchTool, PDFSearchTool, RagTool, ScrapeElementFromWebsiteTool, ScrapeWebsiteTool, WebsiteSearchTool, XMLSearchTool, YoutubeChannelSearchTool, YoutubeVideoSearchTool.
Only use Available Tools. Do Not use any other tools.
Example Below:
Use below example to understand the structure of the output.
Only use Available Tools. Do Not use any other tools.
Example Below:
Use below example to understand the structure of the output.
The final role you create should satisfy the provided task: """ + self.topic + """.
{
"roles": {
Expand Down Expand Up @@ -134,6 +150,6 @@ def get_user_content(self):
"""
return user_content


# generator = AutoGenerator(framework="crewai", topic="Create a snake game in python")
# print(generator.generate())
# print(generator.generate())
22 changes: 15 additions & 7 deletions praisonai/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dotenv import load_dotenv
from crewai import Agent, Task, Crew
load_dotenv()
print(f"Loaded environment variables: OPENAI_MODEL_NAME={os.environ.get('OPENAI_MODEL_NAME')}, OPENAI_API_KEY={os.environ.get('OPENAI_API_KEY')}, OPENAI_API_BASE={os.environ.get('OPENAI_API_BASE')}")
import autogen
import gradio as gr
import argparse
Expand Down Expand Up @@ -41,10 +42,10 @@ def main(self):
return
invocation_cmd = "praisonai"
version_string = f"PraisonAI version {__version__}"

if args.framework:
self.framework = args.framework

ui = args.ui

if args.agent_file:
Expand All @@ -56,34 +57,42 @@ def main(self):
else:
full_path = os.path.abspath(self.agent_file)
self.agent_file = full_path

if args.auto or args.init:
temp_topic = ' '.join(args.auto) if args.auto else ' '.join(args.init)
self.topic = temp_topic
elif self.auto or self.init: # Use the auto attribute if args.auto is not provided
self.topic = self.auto

if args.auto or self.auto:
if not os.environ.get('OPENAI_API_KEY'):
print("Error: OPENAI_API_KEY is not set. Please check your .env file.")
sys.exit(1)
print(f"Debug: OPENAI_API_KEY={os.environ.get('OPENAI_API_KEY')}")
self.agent_file = "test.yaml"
generator = AutoGenerator(topic=self.topic , framework=self.framework, agent_file=self.agent_file)
self.agent_file = generator.generate()
agents_generator = AgentsGenerator(self.agent_file, self.framework, self.config_list)
result = agents_generator.generate_crew_and_kickoff()
return result
elif args.init or self.init:
if not os.environ.get('OPENAI_API_KEY'):
print("Error: OPENAI_API_KEY is not set. Please check your .env file.")
sys.exit(1)
print(f"Debug: OPENAI_API_KEY={os.environ.get('OPENAI_API_KEY')}")
self.agent_file = "agents.yaml"
generator = AutoGenerator(topic=self.topic , framework=self.framework, agent_file=self.agent_file)
self.agent_file = generator.generate()
print("File {} created successfully".format(self.agent_file))
return "File {} created successfully".format(self.agent_file)

if ui:
self.create_gradio_interface()
else:
agents_generator = AgentsGenerator(self.agent_file, self.framework, self.config_list)
result = agents_generator.generate_crew_and_kickoff()
return result

def parse_args(self):
parser = argparse.ArgumentParser(prog="praisonai", description="praisonAI command-line interface")
parser.add_argument("--framework", choices=["crewai", "autogen"], default="crewai", help="Specify the framework")
Expand Down Expand Up @@ -161,4 +170,3 @@ def generate_crew_and_kickoff_interface(auto_args, framework):
# description: Create a storyboard for the movie script about a cat in Mars.
# expected_output: A detailed storyboard for the movie about a cat in Mars.
# dependencies: []

Loading