Schmitz005's picture
Create agents.py
897e33d verified
raw
history blame
1.7 kB
import yaml
import openai
import os
# βœ… Set your OpenAI API key β€” use one of the two options below:
# πŸ”’ Option 1: Load from environment variable (recommended)
openai.api_key = "sk-z4C5B-O9hxSANw9drYaooVsp7SdXPw4Z29LUGkJEQhT3BlbkFJ0o-Gh0ALs1R5Zhiuchz-tn3bB_HizEuCx7s2xO_acA"
# πŸ§ͺ Optional: fail early if key not set
if not openai.api_key:
raise ValueError("❌ No OpenAI API key found. Set the environment variable OPENAI_API_KEY.")
# πŸ”“ Option 2 (for development only): Hardcode the key
# openai.api_key = "sk-...your full key..."
# βœ… Load agent configurations from YAML
def load_agents(path="agents/config.yaml"):
with open(path, "r") as f:
return yaml.safe_load(f)
# βœ… Run each agent on the input document
def run_agents_on_text(agent_cfgs, doc):
responses = {}
for agent in agent_cfgs.get("agents", []):
name = agent.get("name", "UnnamedAgent")
system_msg = agent.get("persona", "You are a helpful assistant.")
instructions = agent.get("instructions", "Please analyze this document.")
user_prompt = f"{instructions}\n\nDocument:\n{doc[:4000]}" # Use full text or chunk
try:
completion = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_msg},
{"role": "user", "content": user_prompt}
],
max_tokens=300,
temperature=0.7
)
responses[name] = completion.choices[0].message["content"].strip()
except Exception as e:
responses[name] = f"⚠️ Error calling OpenAI: {str(e)}"
return responses