skatingP / test.py
sp103107's picture
Upload 6 files
a38b3f5 verified
Raw History Blame Contribute Delete
3.33 kB
import logging
import openai
import os
from dotenv import load_dotenv
from swarm import Swarm, Agent
# Load environment variables from the .env file
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
# Load OpenAI API key from environment variables
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
raise ValueError("OpenAI API key is missing from the environment.")
# Initialize the Swarm instance (not used for this test)
swarm_system = Swarm()
# Mock agent functions
def mock_nasa_agent():
return {'title': 'Jupiter and Saturn aligned'}
def mock_moon_agent():
return {'moon_phase': 'New Moon'}
def mock_tides_agent():
return [{'v': 2.1}]
def generate_aries_prediction():
"""Generate horoscope prediction for Aries."""
try:
# Call mock agents
planetary_data = mock_nasa_agent()
moon_data = mock_moon_agent()
tide_data = mock_tides_agent()
# Extract necessary data
planetary_title = planetary_data.get('title', 'Unknown planetary influence')
moon_phase = moon_data.get('moon_phase', 'Unknown phase')
tide_level = tide_data[0].get('v', 'N/A') if isinstance(tide_data, list) else "N/A"
# Build the prompt for OpenAI API
prompt = (
f"Generate a detailed horoscope for Aries:\n"
f"- Moon phase: {moon_phase}\n"
f"- Planetary alignment: {planetary_title}\n"
f"- Tide level: {tide_level} meters\n\n"
f"Include:\n"
f"1. Current Prediction\n"
f"2. Future Prediction\n"
f"3. Personalized Advice\n"
)
# Call the OpenAI API to generate predictions
response = openai.Completion.create(
model="text-davinci-003", # Use text-davinci-003
prompt=prompt,
max_tokens=300,
temperature=0.7
)
# Parse the predictions (adjust this based on the model's output)
predictions = response.choices[0].text.strip().split("\n\n")
current_prediction = predictions[0] if predictions else "No current prediction available."
future_prediction = predictions[1] if len(predictions) > 1 else "No future prediction available."
# Save horoscope to a file
os.makedirs("/app/reports", exist_ok=True)
with open("/app/reports/aries_horoscope.txt", "w") as file:
file.write("Aries 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 Aries prediction: {str(e)}")
return {"error": str(e)}
# Define the Aries agent (not used for this test)
aries_agent = Agent(
name="AriesAgent",
instructions="Generate horoscope predictions for Aries using planetary, moon, and tide data.",
functions=[generate_aries_prediction]
)
# Test the Aries Agent directly
if __name__ == "__main__":
logging.info("Testing Aries Agent...")
# Call the Aries agent's function directly
result = generate_aries_prediction()
print(result)