-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskill_traversal.py
More file actions
240 lines (200 loc) · 7.29 KB
/
skill_traversal.py
File metadata and controls
240 lines (200 loc) · 7.29 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""
Skill Graph Traversal for QuantTradingOS.
Queries pgvector to find relevant skill nodes given a task context.
Injects skill context into agent calls via the orchestrator.
"""
import os
import re
import sys
from pathlib import Path
try:
from dotenv import load_dotenv
load_dotenv(Path(__file__).resolve().parent / ".env")
except ImportError:
pass
# Add data-ingestion-service to path for db.connection
_svc = Path(__file__).resolve().parent.parent / "data-ingestion-service"
if str(_svc) not in sys.path:
sys.path.insert(0, str(_svc))
import openai
EMBEDDING_MODEL = "text-embedding-3-small"
EMBEDDING_DIMENSIONS = 1536
DEFAULT_TOP_K = 5
DEFAULT_MIN_SIMILARITY = 0.75
RELATED_NODE_SIMILARITY_THRESHOLD = 0.65
def get_embedding(text: str) -> list[float]:
"""Generate embedding for a query string."""
client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
response = client.embeddings.create(
model=EMBEDDING_MODEL,
input=text.replace("\n", " "),
dimensions=EMBEDDING_DIMENSIONS,
)
return response.data[0].embedding
def traverse_skill_graph(
task_context: str,
agent_name: str | None = None,
top_k: int = DEFAULT_TOP_K,
include_shared: bool = True,
min_similarity: float = DEFAULT_MIN_SIMILARITY,
) -> list[dict]:
"""
Given a task context, return the most relevant skill nodes.
Parameters:
- task_context: natural language description of current task
- agent_name: if provided, prioritize nodes owned by this agent
- top_k: number of nodes to return
- include_shared: whether to include shared-skills nodes
- min_similarity: minimum cosine similarity threshold
Returns list of dicts: node_name, agent_name, node_path, content, similarity_score
"""
from db.connection import get_connection
embedding = get_embedding(task_context)
embedding_str = str(embedding)
conn = get_connection()
cursor = conn.cursor()
category_filter = ""
if not include_shared:
category_filter = "AND category != 'shared'"
cursor.execute(f"""
SELECT
agent_name,
node_name,
node_path,
category,
content,
related_nodes,
1 - (embedding <=> %(embedding)s::vector) AS similarity_score
FROM skill_nodes
WHERE 1 - (embedding <=> %(embedding)s::vector) >= %(min_similarity)s
{category_filter}
ORDER BY embedding <=> %(embedding)s::vector
LIMIT %(top_k)s
""", {
"embedding": embedding_str,
"min_similarity": min_similarity,
"top_k": top_k * 2,
})
rows = cursor.fetchall()
results = []
for row in rows:
score = float(row["similarity_score"])
if agent_name and row["agent_name"] == agent_name:
score = min(1.0, score * 1.15)
results.append({
"agent_name": row["agent_name"],
"node_name": row["node_name"],
"node_path": row["node_path"],
"category": row["category"],
"content": row["content"],
"related_nodes": row["related_nodes"] or [],
"similarity_score": score,
})
results = sorted(results, key=lambda x: x["similarity_score"], reverse=True)[:top_k]
related_paths = set()
for result in results:
related_paths.update(result["related_nodes"])
if related_paths:
cursor.execute("""
SELECT agent_name, node_name, node_path, category, content, related_nodes,
1 - (embedding <=> %(embedding)s::vector) AS similarity_score
FROM skill_nodes
WHERE node_path = ANY(%(paths)s)
AND 1 - (embedding <=> %(embedding)s::vector) >= %(threshold)s
""", {
"embedding": embedding_str,
"paths": list(related_paths),
"threshold": RELATED_NODE_SIMILARITY_THRESHOLD,
})
related_rows = cursor.fetchall()
existing_paths = {r["node_path"] for r in results}
for row in related_rows:
if row["node_path"] not in existing_paths:
results.append({
"agent_name": row["agent_name"],
"node_name": row["node_name"],
"node_path": row["node_path"],
"category": row["category"],
"content": row["content"],
"related_nodes": row["related_nodes"] or [],
"similarity_score": float(row["similarity_score"]),
"via_related_nodes": True,
})
cursor.close()
conn.close()
return results
def get_shared_context(regime: str | None = None, portfolio_state: dict | None = None) -> list[dict]:
"""
Load shared context nodes: current_regime, portfolio_state, recent_decisions,
and verified_performance nodes.
"""
from db.connection import get_connection
conn = get_connection()
cursor = conn.cursor()
cursor.execute("""
SELECT agent_name, node_name, node_path, category, content, related_nodes
FROM skill_nodes
WHERE category = 'shared'
ORDER BY node_name
""")
rows = cursor.fetchall()
cursor.close()
conn.close()
return [{
"agent_name": row["agent_name"],
"node_name": row["node_name"],
"node_path": row["node_path"],
"category": row["category"],
"content": row["content"],
"related_nodes": row["related_nodes"] or [],
"similarity_score": 1.0,
} for row in rows]
def get_verified_performance(agent_name: str) -> dict:
"""
Read the verified_performance node for a given agent from shared-skills/.
Returns skill percentile and calibration info for dynamic weighting.
"""
from db.connection import get_connection
agent_map = {
"Market-Regime-Agent": "regime_agent",
"Sentiment-Shift-Alert-Agent": "sentiment_agent",
"Equity-Insider-Intelligence-Agent": "insider_agent",
}
node_name = agent_map.get(agent_name)
if not node_name:
return {"skill_percentile": 0.5, "verified": False}
conn = get_connection()
cursor = conn.cursor()
cursor.execute("""
SELECT content FROM skill_nodes
WHERE node_path LIKE %(path_pattern)s
AND category = 'shared'
LIMIT 1
""", {"path_pattern": f"%verified_performance/{node_name}%"})
row = cursor.fetchone()
cursor.close()
conn.close()
if not row:
return {"skill_percentile": 0.5, "verified": False}
match = re.search(r"Skill Percentile:\s*([\d.]+)", row["content"])
percentile = float(match.group(1)) if match else 0.5
return {
"skill_percentile": percentile,
"verified": True,
"content": row["content"],
}
def format_skill_context(nodes: list[dict]) -> str:
"""
Format retrieved skill nodes into a clean string for injection
into an agent's context window.
"""
if not nodes:
return ""
lines = ["--- SKILL CONTEXT ---"]
for node in nodes:
score = node.get("similarity_score", 0)
via = " (via related nodes)" if node.get("via_related_nodes") else ""
lines.append(f"\n### {node['node_name']} [{node['agent_name']}] (relevance: {score:.2f}){via}")
lines.append(node["content"])
lines.append("---")
return "\n".join(lines)