[REFACTOR][playground]

pull/378/head
Kye 1 year ago
parent e8681b223c
commit 06a6172a99

@ -1,90 +0,0 @@
import re
from swarms.models.openai_models import OpenAIChat
class AutoTemp:
"""
AutoTemp is a tool for automatically selecting the best temperature setting for a given task.
It generates responses at different temperatures, evaluates them, and ranks them based on quality.
"""
def __init__(
self,
api_key,
default_temp=0.0,
alt_temps=None,
auto_select=True,
max_workers=6,
):
self.api_key = api_key
self.default_temp = default_temp
self.alt_temps = (
alt_temps if alt_temps else [0.4, 0.6, 0.8, 1.0, 1.2, 1.4]
)
self.auto_select = auto_select
self.max_workers = max_workers
self.llm = OpenAIChat(
openai_api_key=self.api_key, temperature=self.default_temp
)
def evaluate_output(self, output, temperature):
print(f"Evaluating output at temperature {temperature}...")
eval_prompt = f"""
Evaluate the following output which was generated at a temperature setting of {temperature}. Provide a precise score from 0.0 to 100.0, considering the following criteria:
- Relevance: How well does the output address the prompt or task at hand?
- Clarity: Is the output easy to understand and free of ambiguity?
- Utility: How useful is the output for its intended purpose?
- Pride: If the user had to submit this output to the world for their career, would they be proud?
- Delight: Is the output likely to delight or positively surprise the user?
Be sure to comprehensively evaluate the output, it is very important for my career. Please answer with just the score with one decimal place accuracy, such as 42.0 or 96.9. Be extremely critical.
Output to evaluate:
---
{output}
---
"""
score_text = self.llm(eval_prompt, temperature=0.5)
score_match = re.search(r"\b\d+(\.\d)?\b", score_text)
return (
round(float(score_match.group()), 1)
if score_match
else 0.0
)
def run(self, prompt, temperature_string):
print("Starting generation process...")
temperature_list = [
float(temp.strip())
for temp in temperature_string.split(",")
if temp.strip()
]
outputs = {}
scores = {}
for temp in temperature_list:
print(f"Generating at temperature {temp}...")
output_text = self.llm(prompt, temperature=temp)
if output_text:
outputs[temp] = output_text
scores[temp] = self.evaluate_output(output_text, temp)
print("Generation process complete.")
if not scores:
return "No valid outputs generated.", None
sorted_scores = sorted(
scores.items(), key=lambda item: item[1], reverse=True
)
best_temp, best_score = sorted_scores[0]
best_output = outputs[best_temp]
return (
f"Best AutoTemp Output (Temp {best_temp} | Score:"
f" {best_score}):\n{best_output}"
if self.auto_select
else "\n".join(
f"Temp {temp} | Score: {score}:\n{outputs[temp]}"
for temp, score in sorted_scores
)
)

@ -1,21 +1,90 @@
from autotemp import AutoTemp import re
from swarms.models.openai_models import OpenAIChat
# Your OpenAI API key
api_key = ""
autotemp_agent = AutoTemp( class AutoTemp:
api_key=api_key, """
alt_temps=[0.4, 0.6, 0.8, 1.0, 1.2], AutoTemp is a tool for automatically selecting the best temperature setting for a given task.
auto_select=False, It generates responses at different temperatures, evaluates them, and ranks them based on quality.
# model_version="gpt-3.5-turbo" # Specify the model version if needed """
)
# Define the task and temperature string def __init__(
task = "Generate a short story about a lost civilization." self,
temperature_string = "0.4,0.6,0.8,1.0,1.2," api_key,
default_temp=0.0,
alt_temps=None,
auto_select=True,
max_workers=6,
):
self.api_key = api_key
self.default_temp = default_temp
self.alt_temps = (
alt_temps if alt_temps else [0.4, 0.6, 0.8, 1.0, 1.2, 1.4]
)
self.auto_select = auto_select
self.max_workers = max_workers
self.llm = OpenAIChat(
openai_api_key=self.api_key, temperature=self.default_temp
)
# Run the AutoTempAgent def evaluate_output(self, output, temperature):
result = autotemp_agent.run(task, temperature_string) print(f"Evaluating output at temperature {temperature}...")
eval_prompt = f"""
Evaluate the following output which was generated at a temperature setting of {temperature}. Provide a precise score from 0.0 to 100.0, considering the following criteria:
# Print the result - Relevance: How well does the output address the prompt or task at hand?
print(result) - Clarity: Is the output easy to understand and free of ambiguity?
- Utility: How useful is the output for its intended purpose?
- Pride: If the user had to submit this output to the world for their career, would they be proud?
- Delight: Is the output likely to delight or positively surprise the user?
Be sure to comprehensively evaluate the output, it is very important for my career. Please answer with just the score with one decimal place accuracy, such as 42.0 or 96.9. Be extremely critical.
Output to evaluate:
---
{output}
---
"""
score_text = self.llm(eval_prompt, temperature=0.5)
score_match = re.search(r"\b\d+(\.\d)?\b", score_text)
return (
round(float(score_match.group()), 1)
if score_match
else 0.0
)
def run(self, prompt, temperature_string):
print("Starting generation process...")
temperature_list = [
float(temp.strip())
for temp in temperature_string.split(",")
if temp.strip()
]
outputs = {}
scores = {}
for temp in temperature_list:
print(f"Generating at temperature {temp}...")
output_text = self.llm(prompt, temperature=temp)
if output_text:
outputs[temp] = output_text
scores[temp] = self.evaluate_output(output_text, temp)
print("Generation process complete.")
if not scores:
return "No valid outputs generated.", None
sorted_scores = sorted(
scores.items(), key=lambda item: item[1], reverse=True
)
best_temp, best_score = sorted_scores[0]
best_output = outputs[best_temp]
return (
f"Best AutoTemp Output (Temp {best_temp} | Score:"
f" {best_score}):\n{best_output}"
if self.auto_select
else "\n".join(
f"Temp {temp} | Score: {score}:\n{outputs[temp]}"
for temp, score in sorted_scores
)
)

@ -1,138 +0,0 @@
import os
from termcolor import colored
from swarms.models import OpenAIChat
from autotemp import AutoTemp
from swarms.structs import SequentialWorkflow
class BlogGen:
def __init__(
self,
api_key,
blog_topic,
temperature_range: str = "0.4,0.6,0.8,1.0,1.2",
): # Add blog_topic as an argument
self.openai_chat = OpenAIChat(
openai_api_key=api_key, temperature=0.8
)
self.auto_temp = AutoTemp(api_key)
self.temperature_range = temperature_range
self.workflow = SequentialWorkflow(max_loops=5)
# Formatting the topic selection prompt with the user's topic
self.TOPIC_SELECTION_SYSTEM_PROMPT = f"""
Given the topic '{blog_topic}', generate an engaging and versatile blog topic. This topic should cover areas related to '{blog_topic}' and might include aspects such as current events, lifestyle, technology, health, and culture related to '{blog_topic}'. Identify trending subjects within this realm. The topic must be unique, thought-provoking, and have the potential to draw in readers interested in '{blog_topic}'.
"""
self.DRAFT_WRITER_SYSTEM_PROMPT = """
Create an engaging and comprehensive blog article of at least 1,000 words on '{{CHOSEN_TOPIC}}'. The content should be original, informative, and reflective of a human-like style, with a clear structure including headings and sub-headings. Incorporate a blend of narrative, factual data, expert insights, and anecdotes to enrich the article. Focus on SEO optimization by using relevant keywords, ensuring readability, and including meta descriptions and title tags. The article should provide value, appeal to both knowledgeable and general readers, and maintain a balance between depth and accessibility. Aim to make the article engaging and suitable for online audiences.
"""
self.REVIEW_AGENT_SYSTEM_PROMPT = """
Critically review the drafted blog article on '{{ARTICLE_TOPIC}}' to refine it to high-quality content suitable for online publication. Ensure the article is coherent, factually accurate, engaging, and optimized for search engines (SEO). Check for the effective use of keywords, readability, internal and external links, and the inclusion of meta descriptions and title tags. Edit the content to enhance clarity, impact, and maintain the authors voice. The goal is to polish the article into a professional, error-free piece that resonates with the target audience, adheres to publication standards, and is optimized for both search engines and social media sharing.
"""
self.DISTRIBUTION_AGENT_SYSTEM_PROMPT = """
Develop an autonomous distribution strategy for the blog article on '{{ARTICLE_TOPIC}}'. Utilize an API to post the article on a popular blog platform (e.g., WordPress, Blogger, Medium) commonly used by our target audience. Ensure the post includes all SEO elements like meta descriptions, title tags, and properly formatted content. Craft unique, engaging social media posts tailored to different platforms to promote the blog article. Schedule these posts to optimize reach and engagement, using data-driven insights. Monitor the performance of the distribution efforts, adjusting strategies based on engagement metrics and audience feedback. Aim to maximize the article's visibility, attract a diverse audience, and foster engagement across digital channels.
"""
def run_workflow(self):
try:
# Topic generation using OpenAIChat
topic_result = self.openai_chat.generate(
[self.TOPIC_SELECTION_SYSTEM_PROMPT]
)
topic_output = topic_result.generations[0][0].text
print(
colored(
(
"\nTopic Selection Task"
f" Output:\n----------------------------\n{topic_output}\n"
),
"white",
)
)
chosen_topic = topic_output.split("\n")[0]
print(
colored("Selected topic: " + chosen_topic, "yellow")
)
# Initial draft generation with AutoTemp
initial_draft_prompt = (
self.DRAFT_WRITER_SYSTEM_PROMPT.replace(
"{{CHOSEN_TOPIC}}", chosen_topic
)
)
auto_temp_output = self.auto_temp.run(
initial_draft_prompt, self.temperature_range
)
initial_draft_output = auto_temp_output # Assuming AutoTemp.run returns the best output directly
print(
colored(
(
"\nInitial Draft"
f" Output:\n----------------------------\n{initial_draft_output}\n"
),
"white",
)
)
# Review process using OpenAIChat
review_prompt = self.REVIEW_AGENT_SYSTEM_PROMPT.replace(
"{{ARTICLE_TOPIC}}", chosen_topic
)
review_result = self.openai_chat.generate([review_prompt])
review_output = review_result.generations[0][0].text
print(
colored(
(
"\nReview"
f" Output:\n----------------------------\n{review_output}\n"
),
"white",
)
)
# Distribution preparation using OpenAIChat
distribution_prompt = (
self.DISTRIBUTION_AGENT_SYSTEM_PROMPT.replace(
"{{ARTICLE_TOPIC}}", chosen_topic
)
)
distribution_result = self.openai_chat.generate(
[distribution_prompt]
)
distribution_output = distribution_result.generations[0][
0
].text
print(
colored(
(
"\nDistribution"
f" Output:\n----------------------------\n{distribution_output}\n"
),
"white",
)
)
# Final compilation of the blog
final_blog_content = f"{initial_draft_output}\n\n{review_output}\n\n{distribution_output}"
print(
colored(
(
"\nFinal Blog"
f" Content:\n----------------------------\n{final_blog_content}\n"
),
"green",
)
)
except Exception as e:
print(colored(f"An error occurred: {str(e)}", "red"))
if __name__ == "__main__":
api_key = os.environ["OPENAI_API_KEY"]
blog_generator = BlogGen(api_key)
blog_generator.run_workflow()

@ -1,25 +1,138 @@
import os import os
from blog_gen import BlogGen from termcolor import colored
from swarms.models import OpenAIChat
from autotemp import AutoTemp
from swarms.structs import SequentialWorkflow
def main(): class BlogGen:
api_key = os.getenv("OPENAI_API_KEY") def __init__(
if not api_key: self,
raise ValueError( api_key,
"OPENAI_API_KEY environment variable not set." blog_topic,
temperature_range: str = "0.4,0.6,0.8,1.0,1.2",
): # Add blog_topic as an argument
self.openai_chat = OpenAIChat(
openai_api_key=api_key, temperature=0.8
) )
self.auto_temp = AutoTemp(api_key)
self.temperature_range = temperature_range
self.workflow = SequentialWorkflow(max_loops=5)
blog_topic = input("Enter the topic for the blog generation: ") # Formatting the topic selection prompt with the user's topic
self.TOPIC_SELECTION_SYSTEM_PROMPT = f"""
Given the topic '{blog_topic}', generate an engaging and versatile blog topic. This topic should cover areas related to '{blog_topic}' and might include aspects such as current events, lifestyle, technology, health, and culture related to '{blog_topic}'. Identify trending subjects within this realm. The topic must be unique, thought-provoking, and have the potential to draw in readers interested in '{blog_topic}'.
"""
blog_generator = BlogGen(api_key, blog_topic) self.DRAFT_WRITER_SYSTEM_PROMPT = """
blog_generator.TOPIC_SELECTION_SYSTEM_PROMPT = ( Create an engaging and comprehensive blog article of at least 1,000 words on '{{CHOSEN_TOPIC}}'. The content should be original, informative, and reflective of a human-like style, with a clear structure including headings and sub-headings. Incorporate a blend of narrative, factual data, expert insights, and anecdotes to enrich the article. Focus on SEO optimization by using relevant keywords, ensuring readability, and including meta descriptions and title tags. The article should provide value, appeal to both knowledgeable and general readers, and maintain a balance between depth and accessibility. Aim to make the article engaging and suitable for online audiences.
blog_generator.TOPIC_SELECTION_SYSTEM_PROMPT.replace( """
"{{BLOG_TOPIC}}", blog_topic
self.REVIEW_AGENT_SYSTEM_PROMPT = """
Critically review the drafted blog article on '{{ARTICLE_TOPIC}}' to refine it to high-quality content suitable for online publication. Ensure the article is coherent, factually accurate, engaging, and optimized for search engines (SEO). Check for the effective use of keywords, readability, internal and external links, and the inclusion of meta descriptions and title tags. Edit the content to enhance clarity, impact, and maintain the authors voice. The goal is to polish the article into a professional, error-free piece that resonates with the target audience, adheres to publication standards, and is optimized for both search engines and social media sharing.
"""
self.DISTRIBUTION_AGENT_SYSTEM_PROMPT = """
Develop an autonomous distribution strategy for the blog article on '{{ARTICLE_TOPIC}}'. Utilize an API to post the article on a popular blog platform (e.g., WordPress, Blogger, Medium) commonly used by our target audience. Ensure the post includes all SEO elements like meta descriptions, title tags, and properly formatted content. Craft unique, engaging social media posts tailored to different platforms to promote the blog article. Schedule these posts to optimize reach and engagement, using data-driven insights. Monitor the performance of the distribution efforts, adjusting strategies based on engagement metrics and audience feedback. Aim to maximize the article's visibility, attract a diverse audience, and foster engagement across digital channels.
"""
def run_workflow(self):
try:
# Topic generation using OpenAIChat
topic_result = self.openai_chat.generate(
[self.TOPIC_SELECTION_SYSTEM_PROMPT]
)
topic_output = topic_result.generations[0][0].text
print(
colored(
(
"\nTopic Selection Task"
f" Output:\n----------------------------\n{topic_output}\n"
),
"white",
) )
) )
blog_generator.run_workflow() chosen_topic = topic_output.split("\n")[0]
print(
colored("Selected topic: " + chosen_topic, "yellow")
)
# Initial draft generation with AutoTemp
initial_draft_prompt = (
self.DRAFT_WRITER_SYSTEM_PROMPT.replace(
"{{CHOSEN_TOPIC}}", chosen_topic
)
)
auto_temp_output = self.auto_temp.run(
initial_draft_prompt, self.temperature_range
)
initial_draft_output = auto_temp_output # Assuming AutoTemp.run returns the best output directly
print(
colored(
(
"\nInitial Draft"
f" Output:\n----------------------------\n{initial_draft_output}\n"
),
"white",
)
)
# Review process using OpenAIChat
review_prompt = self.REVIEW_AGENT_SYSTEM_PROMPT.replace(
"{{ARTICLE_TOPIC}}", chosen_topic
)
review_result = self.openai_chat.generate([review_prompt])
review_output = review_result.generations[0][0].text
print(
colored(
(
"\nReview"
f" Output:\n----------------------------\n{review_output}\n"
),
"white",
)
)
# Distribution preparation using OpenAIChat
distribution_prompt = (
self.DISTRIBUTION_AGENT_SYSTEM_PROMPT.replace(
"{{ARTICLE_TOPIC}}", chosen_topic
)
)
distribution_result = self.openai_chat.generate(
[distribution_prompt]
)
distribution_output = distribution_result.generations[0][
0
].text
print(
colored(
(
"\nDistribution"
f" Output:\n----------------------------\n{distribution_output}\n"
),
"white",
)
)
# Final compilation of the blog
final_blog_content = f"{initial_draft_output}\n\n{review_output}\n\n{distribution_output}"
print(
colored(
(
"\nFinal Blog"
f" Content:\n----------------------------\n{final_blog_content}\n"
),
"green",
)
)
except Exception as e:
print(colored(f"An error occurred: {str(e)}", "red"))
if __name__ == "__main__": if __name__ == "__main__":
main() api_key = os.environ["OPENAI_API_KEY"]
blog_generator = BlogGen(api_key)
blog_generator.run_workflow()

@ -1,7 +0,0 @@
from swarms.models.fuyu import Fuyu
fuyu = Fuyu()
# This is the default image, you can change it to any image you want
out = fuyu("What is this image?", "images/swarms.jpeg")
print(out)

@ -1,7 +1,7 @@
from swarms.models.fuyu import Fuyu from swarms.models.fuyu import Fuyu
img = "dalle3.jpeg"
fuyu = Fuyu() fuyu = Fuyu()
fuyu("What is this image", img) # This is the default image, you can change it to any image you want
out = fuyu("What is this image?", "images/swarms.jpeg")
print(out)

@ -1,48 +0,0 @@
from swarms import OpenAIChat, Agent, Task, SequentialWorkflow
# Example usage
llm = OpenAIChat(
temperature=0.5,
max_tokens=3000,
)
# Initialize the Agent with the language agent
agent1 = Agent(
agent_name="John the writer",
llm=llm,
max_loops=0,
dashboard=False,
)
task1 = Task(
agent=agent1,
description="Write a 1000 word blog about the future of AI",
)
# Create another Agent for a different task
agent2 = Agent("Summarizer", llm=llm, max_loops=1, dashboard=False)
task2 = Task(
agent=agent2,
description="Summarize the generated blog",
)
# Create the workflow
workflow = SequentialWorkflow(
name="Blog Generation Workflow",
description=(
"A workflow to generate and summarize a blog about the future"
" of AI"
),
max_loops=1,
autosave=True,
dashboard=False,
)
# Add tasks to the workflow
workflow.add(tasks=[task1, task2])
# Run the workflow
workflow.run()
# # Output the results
# for task in workflow.tasks:
# print(f"Task: {task.description}, Result: {task.result}")

@ -1,52 +1,48 @@
import os from swarms import OpenAIChat, Agent, Task, SequentialWorkflow
from swarms.models import OpenAIChat
from swarms.structs import Agent
from swarms.structs.sequential_workflow import SequentialWorkflow
from dotenv import load_dotenv
load_dotenv() # Example usage
# Load the environment variables
api_key = os.getenv("OPENAI_API_KEY")
# Initialize the language agent
# Initialize the language model
llm = OpenAIChat( llm = OpenAIChat(
temperature=0.5, temperature=0.5,
model_name="gpt-4", max_tokens=3000,
openai_api_key=api_key,
max_tokens=4000,
) )
# Initialize the Agent with the language agent
# Initialize the agent with the language agent
agent1 = Agent( agent1 = Agent(
agent_name="John the writer",
llm=llm, llm=llm,
max_loops=1, max_loops=0,
dashboard=False,
)
task1 = Task(
agent=agent1,
description="Write a 1000 word blog about the future of AI",
) )
# Create another agent for a different task # Create another Agent for a different task
agent2 = Agent(llm=llm, max_loops=1) agent2 = Agent("Summarizer", llm=llm, max_loops=1, dashboard=False)
task2 = Task(
agent=agent2,
description="Summarize the generated blog",
)
# Create the workflow # Create the workflow
workflow = SequentialWorkflow(max_loops=1) workflow = SequentialWorkflow(
name="Blog Generation Workflow",
# Add tasks to the workflow description=(
workflow.add( "A workflow to generate and summarize a blog about the future"
agent1, " of AI"
"Generate a 10,000 word blog on health and wellness.", ),
max_loops=1,
autosave=True,
dashboard=False,
) )
# Suppose the next task takes the output of the first task as input # Add tasks to the workflow
workflow.add( workflow.add(tasks=[task1, task2])
agent2,
"Summarize the generated blog",
)
# Run the workflow # Run the workflow
workflow.run() workflow.run()
# Output the results # # Output the results
for task in workflow.tasks: # for task in workflow.tasks:
print(f"Task: {task.description}, Result: {task.result}") # print(f"Task: {task.description}, Result: {task.result}")

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save