from groq import Groq
import json
from honeyhive import HoneyHiveTracer, trace
HoneyHiveTracer.init(
api_key="MY_HONEYHIVE_API_KEY",
project="MY_HONEYHIVE_PROJECT_NAME",
)
client = Groq(
api_key="MY_GROQ_API_KEY",
)
def evaluate_post(post: str) -> dict:
evaluation_prompt = f"""
Evaluate the following blog post based on these criteria (rate each from 1-5):
1. Engagement: How well does it capture and maintain reader interest?
2. Clarity: How clear and well-structured is the content?
3. Value: How informative and valuable is the content?
Blog post:
{post}
Respond in this exact JSON format:
{{
"engagement": <score>,
"clarity": <score>,
"value": <score>,
"total": <sum of scores>
}}
"""
response = client.chat.completions.create(
messages=[{"role": "user", "content": evaluation_prompt}],
model="llama3-8b-8192",
response_format={"type": "json_object"}
)
# Parse the response as a dictionary
return json.loads(response.choices[0].message.content)
@trace
def generate_blog_post(topic: str) -> dict:
prompt = f"Write a compelling blog post about {topic}. Make it engaging and informative."
response = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama3-8b-8192",
)
# Evaluate the generated post right away
post = response.choices[0].message.content
evaluation = evaluate_post(post)
return {
"content": post,
"evaluation": evaluation
}
def main():
# Topics for blog posts
topics = [
"The Future of AI in Healthcare",
"Sustainable Living in 2024",
"Digital Privacy in the Modern Age",
"The Rise of Remote Work",
"Mindfulness and Technology Balance"
]
# Generate blog posts
print("Generating blog posts...")
posts = [generate_blog_post(topic) for topic in topics]
# Find the highest-rated post
best_post_index = max(range(len(posts)), key=lambda i: posts[i]['evaluation']['total'])
print("\nEvaluation Results:")
for i, post in enumerate(posts):
print(f"\nPost {i+1}: {topics[i]}")
print(f"Engagement: {post['evaluation']['engagement']}")
print(f"Clarity: {post['evaluation']['clarity']}")
print(f"Value: {post['evaluation']['value']}")
print(f"Total Score: {post['evaluation']['total']}")
print("\n=== Best Rated Blog Post ===")
print(f"Topic: {topics[best_post_index]}")
print(posts[best_post_index]['content'])
print(posts[best_post_index]['evaluation'])
main()