-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from elder-plinius/master
AutoTemp
- Loading branch information
Showing
5 changed files
with
222 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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: {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 | ||
) | ||
) |
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,22 @@ | ||
from swarms.models import OpenAIChat | ||
from swarms.models.autotemp import AutoTemp | ||
|
||
# Your OpenAI API key | ||
api_key = "" | ||
|
||
autotemp_agent = AutoTemp( | ||
api_key=api_key, | ||
alt_temps=[0.4, 0.6, 0.8, 1.0, 1.2], | ||
auto_select=False, | ||
# model_version="gpt-3.5-turbo" # Specify the model version if needed | ||
) | ||
|
||
# Define the task and temperature string | ||
task = "Generate a short story about a lost civilization." | ||
temperature_string = "0.4,0.6,0.8,1.0,1.2," | ||
|
||
# Run the AutoTempAgent | ||
result = autotemp_agent.run(task, temperature_string) | ||
|
||
# Print the result | ||
print(result) |
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,110 @@ | ||
import os | ||
from termcolor import colored | ||
from swarms.models import OpenAIChat | ||
from swarms.models.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( | ||
f"\nTopic Selection Task 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( | ||
f"\nInitial Draft 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( | ||
f"\nReview 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( | ||
f"\nDistribution 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( | ||
f"\nFinal Blog 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() |
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,23 @@ | ||
import os | ||
from swarms.swarms.blog_gen import BlogGen | ||
|
||
|
||
def main(): | ||
api_key = os.getenv("OPENAI_API_KEY") | ||
if not api_key: | ||
raise ValueError("OPENAI_API_KEY environment variable not set.") | ||
|
||
blog_topic = input("Enter the topic for the blog generation: ") | ||
|
||
blog_generator = BlogGen(api_key, blog_topic) | ||
blog_generator.TOPIC_SELECTION_SYSTEM_PROMPT = ( | ||
blog_generator.TOPIC_SELECTION_SYSTEM_PROMPT.replace( | ||
"{{BLOG_TOPIC}}", blog_topic | ||
) | ||
) | ||
|
||
blog_generator.run_workflow() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.