-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
260 lines (218 loc) · 7.63 KB
/
Copy pathapp.py
File metadata and controls
260 lines (218 loc) · 7.63 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import json
import os
import re
import urllib.request
from pathlib import Path
from typing import Any
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
AGENT_NAME = "nlip-knowledge-agent"
MODEL_NAME = os.getenv("NLIP_MODEL_NAME", "llama-4-scout")
JETSTREAM_CHAT_URL = os.getenv("JETSTREAM_CHAT_URL", "")
JETSTREAM_API_KEY = os.getenv("JETSTREAM_API_KEY", "")
CONFIDENTIAL_AGENT_URL = os.getenv("CONFIDENTIAL_AGENT_URL", "")
KNOWLEDGE_BASE_PATH = Path(os.getenv("KNOWLEDGE_BASE_PATH", "data/knowledge_base.json"))
class NLIPMessage(BaseModel):
messageType: str | None = None
format: str
subformat: str
content: Any
submessages: list[dict[str, Any]] = Field(default_factory=list)
app = FastAPI(title="NLIP Knowledge Agent")
app.add_middleware(
CORSMiddleware,
allow_origins=os.getenv("CORS_ALLOW_ORIGINS", "*").split(","),
allow_credentials=False,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
)
def nlip_response(content: Any, subformat: str = "json") -> dict[str, Any]:
return {
"messageType": "response",
"format": "json",
"subformat": subformat,
"content": content,
}
def extract_query(message: NLIPMessage) -> str:
if isinstance(message.content, str):
return message.content
if isinstance(message.content, dict):
for key in ("query", "prompt", "text"):
value = message.content.get(key)
if isinstance(value, str):
return value
raise HTTPException(
status_code=400,
detail="NLIP content must be text or a JSON object with query/prompt/text.",
)
def load_knowledge_base() -> list[dict[str, Any]]:
if not KNOWLEDGE_BASE_PATH.exists():
return []
return json.loads(KNOWLEDGE_BASE_PATH.read_text())
def tokenize(text: str) -> set[str]:
return set(re.findall(r"[a-zA-Z0-9-]+", text.lower()))
def retrieve(query: str, limit: int = 4) -> list[dict[str, Any]]:
query_terms = tokenize(query)
if not query_terms:
return []
scored = []
for item in load_knowledge_base():
text = item.get("text", "")
terms = tokenize(text)
score = len(query_terms & terms) / max(1, len(query_terms))
if score:
scored.append((score, item))
scored.sort(key=lambda pair: pair[0], reverse=True)
results = []
for score, item in scored[:limit]:
result = dict(item)
result["score"] = score
results.append(result)
return results
def ask_llm(query: str, contexts: list[dict[str, Any]]) -> str | None:
if not JETSTREAM_CHAT_URL:
return None
context_text = "\n\n".join(
f"Source: {item.get('source', 'unknown')}\n{item.get('text', '')}"
for item in contexts
)
prompt = (
"You are an NLIP knowledge assistant. NLIP means Natural Language "
"Interaction Protocol. Answer the user's NLIP question using the provided sources. "
"If the sources are insufficient, say so. Include source names when useful.\n\n"
f"Sources:\n{context_text}\n\nQuestion: {query}\n\nAnswer:"
)
if "chat/completions" in JETSTREAM_CHAT_URL:
body = {
"model": MODEL_NAME,
"messages": [
{"role": "system", "content": "You are an NLIP knowledge assistant."},
{"role": "user", "content": prompt},
],
"temperature": 0.2,
"max_tokens": 400,
}
else:
body = {
"model": MODEL_NAME,
"prompt": prompt,
"temperature": 0.1,
"max_tokens": 180,
"stop": ["\nThe answer", "\nThe question", "\nWe need", "\n\nThe user", "assistantfinal"],
}
headers = {"Content-Type": "application/json"}
if JETSTREAM_API_KEY:
headers["Authorization"] = f"Bearer {JETSTREAM_API_KEY}"
req = urllib.request.Request(
JETSTREAM_CHAT_URL,
data=json.dumps(body).encode("utf-8"),
headers=headers,
method="POST",
)
with urllib.request.urlopen(req, timeout=60) as response:
payload = json.loads(response.read().decode("utf-8"))
choice = payload["choices"][0]
if "message" in choice:
answer = choice["message"]["content"]
else:
answer = choice.get("text", "").strip()
for marker in ("assistantfinal", "assistant final", "Final answer:"):
if marker in answer:
answer = answer.split(marker, 1)[1].strip()
if "\n\n" in answer and answer.lower().startswith("the user asks"):
answer = answer.split("\n\n", 1)[1].strip()
for starter in ("NLIP stands", "NLIP, or", "Natural Language Interaction Protocol"):
idx = answer.find(starter)
if idx > 0:
answer = answer[idx:].strip()
break
for bad_tail in (
"\n```",
"```json",
"\nThe answer",
"\nThe question",
"\nQuestion:",
"\nWe need",
"\nIt looks like",
"\nIt appears",
" ... ...",
"......",
):
if bad_tail in answer:
answer = answer.split(bad_tail, 1)[0].strip()
if "\n\n" in answer:
answer = answer.split("\n\n", 1)[0].strip()
return answer
def call_confidential_agent(query: str) -> dict[str, Any] | None:
if not CONFIDENTIAL_AGENT_URL:
return None
message = {
"messageType": "request",
"format": "text",
"subformat": "english",
"content": query,
}
req = urllib.request.Request(
CONFIDENTIAL_AGENT_URL,
data=json.dumps(message).encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=15) as response:
return json.loads(response.read().decode("utf-8"))
@app.get("/health")
def health() -> dict[str, Any]:
return {
"status": "ok",
"agent": AGENT_NAME,
"model": MODEL_NAME,
"knowledge_chunks": len(load_knowledge_base()),
"confidential_agent_configured": bool(CONFIDENTIAL_AGENT_URL),
}
@app.post("/nlip")
def handle_nlip(message: NLIPMessage) -> dict[str, Any]:
query = extract_query(message)
contexts = retrieve(query)
try:
answer = ask_llm(query, contexts)
except Exception as error:
answer = None
llm_error = str(error)
else:
llm_error = None
if not answer:
if contexts:
answer = "I found potentially relevant NLIP source material, but no LLM answer was generated."
else:
answer = "The current knowledge base does not contain enough information."
extra: dict[str, Any] = {}
query_lower = query.lower()
if "confidential" in query_lower or "project context" in query_lower:
try:
confidential_response = call_confidential_agent(query)
except Exception as error:
extra["confidential_agent_error"] = str(error)
else:
if confidential_response is not None:
extra["confidential_agent_response"] = confidential_response
if llm_error:
extra["llm_error"] = llm_error
return nlip_response(
{
"agent": AGENT_NAME,
"answer": answer,
"received_query": query,
"model": MODEL_NAME,
"sources": [
{
"source": item.get("source"),
"url": item.get("url"),
"score": item.get("score"),
}
for item in contexts
],
"extra": extra,
},
subformat="nlip-agent-response",
)