Spaces:
Sleeping
Sleeping
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 | |