Spaces:
Sleeping
Sleeping
File size: 1,703 Bytes
897e33d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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
|