-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_loader.py
More file actions
77 lines (70 loc) · 2.28 KB
/
agent_loader.py
File metadata and controls
77 lines (70 loc) · 2.28 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import yaml
from crewai import Agent, Task
from langchain_openai import ChatOpenAI
from helper import get_openai_api_key
def load_llm():
"""
Load the LLM with the OpenAI key.
"""
api_key = get_openai_api_key()
if not api_key:
raise ValueError("OPENAI_API_KEY not found in environment!")
return ChatOpenAI(
temperature=0.0,
# model="gpt-4o",
model="ft:gpt-4o-mini-2024-07-18:aphrc-ai-api:domain-tuned:CIUzXaVc",
openai_api_key=api_key
)
def load_agents_from_yaml(yaml_path, llm):
"""
Returns a dictionary of agents keyed by their id.
"""
with open(yaml_path, 'r') as f:
data = yaml.safe_load(f)
agent_dict = {}
for a in data.get('agents', []):
agent_obj = Agent(
name=a['id'],
role=a.get('role', ''),
goal=a.get('description', ''),
backstory='',
tools=[],
llm=llm,
verbose=True
)
agent_dict[a['id']] = agent_obj
return agent_dict
def load_tasks_from_yaml(yaml_path, agent_dict):
"""
Returns a list of Task objects.
For each YAML task with multiple agents, creates one Task per agentpip install --upgrade langchain
.
"""
with open(yaml_path, 'r') as f:
data = yaml.safe_load(f)
tasks = []
for t in data.get('tasks', []):
expected_output = t.get('expected_output', 'Provide a clear and detailed answer.')
agent_ids = t.get('agents', [])
if not agent_ids:
# No agents listed
task_obj = Task(
name=t['name'],
description=t['description'],
expected_output=expected_output,
agent=None
)
tasks.append(task_obj)
else:
for agent_id in agent_ids:
if agent_id in agent_dict:
task_obj = Task(
name=f"{t['name']} - {agent_id}",
description=t['description'],
expected_output=expected_output,
agent=agent_dict[agent_id]
)
tasks.append(task_obj)
else:
print(f"Warning: Agent id '{agent_id}' not found in agents.yaml")
return tasks