skatingP / agents /LibraAgent.py
sp103107's picture
agents
21f953b verified
Raw History Blame Contribute Delete
3.23 kB
import logging
import openai
import os
from dotenv import load_dotenv # Importing to load environment variables
from swarm import Agent, Swarm
# Load environment variables from .env file
load_dotenv() # Load environment variables from the .env file
# Configure logging
logging.basicConfig(level=logging.INFO)
# Load OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")
# Check if the API key was loaded successfully
if not openai.api_key:
raise ValueError("OpenAI API key is missing from the environment.")
def generate_libra_prediction():
swarm = Swarm() # Correct way to create a Swarm instance
try:
# Fetch data from agents
planetary_data = swarm.call("NASAAgent")
moon_phase_data = swarm.call("MoonAgent")
tide_data = swarm.call("TidesAgent")
# Extract relevant information
current_planetary_title = planetary_data.get('title', 'Unknown planetary influence')
current_moon_phase = moon_phase_data.get('moon_phase', 'Unknown phase')
current_tide_level = tide_data[0].get('v', 'N/A') if isinstance(tide_data, list) else "N/A"
# Build the prompt for OpenAI
prompt = (
f"Generate a detailed horoscope report for Libra using the following data:\n"
f"- Current Moon phase: {current_moon_phase}\n"
f"- Planetary alignment: {current_planetary_title}\n"
f"- Tide level: {current_tide_level} meters\n\n"
f"Include the following sections:\n"
f"1. **Current Prediction**: How the current planetary and moon data affect Libra today.\n"
f"2. **Future Prediction**: Forecast what Libra might experience tomorrow.\n"
f"3. **Recommendations**: Personalized advice for Libra focusing on emotional, personal, and professional life.\n"
f"The tone should be inspirational and motivational."
)
# Call OpenAI to generate predictions
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=300,
temperature=0.7
)
predictions = response.choices[0].message['content'].strip().split("\n\n")
current_prediction = predictions[0] if len(predictions) > 0 else "No current prediction available."
future_prediction = predictions[1] if len(predictions) > 1 else "No future prediction available."
# Save horoscope to a file
with open("libra_horoscope.txt", "w") as file:
file.write("Libra Horoscope Report\n")
file.write("=======================\n")
file.write(f"Current Prediction:\n{current_prediction}\n\n")
file.write(f"Future Prediction:\n{future_prediction}\n")
return {"current_prediction": current_prediction, "future_prediction": future_prediction}
except Exception as e:
logging.error(f"Error generating Libra prediction: {str(e)}")
return {"error": f"Unable to generate prediction: {str(e)}"}
libra_agent = Agent(
name="LibraAgent",
instructions="Generate detailed horoscope reports for Libra using OpenAI.",
functions=[generate_libra_prediction]
)