Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
263 lines (230 loc) · 10 KB
/
Copy pathapp.py
File metadata and controls
263 lines (230 loc) · 10 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
261
262
263
"""FastAPI proxy that exposes MiniCS retrieval-augmented chat as an
OpenAI-compatible API.
Request flow for ``POST /v1/chat/completions``:
client ──► /v1/chat/completions
│ 1. extract the last user message as the query
│ 2. hybrid retrieval (vector + graph [+ rerank]) ◄─ MiniCS index
│ 3. inject the retrieved context as grounding
│ 4. forward to the configured LLM endpoint
▼
OpenAI-shaped response + ``minics`` metadata block
The ``minics`` metadata block (absent from upstream, added by the proxy)
contains the MiniCS version, the model that answered and everything needed to
trace the answer back to the source documents.
Grounding can be controlled per request with an optional top-level ``minics``
object in the request body::
{
"model": "...", "messages": [...],
"minics": {"use_rag": true, "use_graph": true, "use_grounding": true,
"top_k": 5, "document_ids": [1, 2]}
}
"""
from __future__ import annotations
import json
import logging
import time
from typing import Any
from fastapi import FastAPI
from fastapi.responses import JSONResponse, StreamingResponse
from minics import __version__
from minics.llm.client import LLMError, build_openai_client
from minics.proxy.schemas import (
ChatCompletionRequest,
MinicsOptions,
ModelListResponse,
ModelObject,
)
from minics.services.context import AppContext, get_context
LOGGER = logging.getLogger("minics.proxy")
#: Injected above the caller's messages when grounding is active.
GROUNDING_TEMPLATE = """Use the reference material below to answer.
- Cite sources inline as [n] matching the numbered references.
- If the material does not contain the answer, say so and answer from general knowledge.
- Never contradict the reference material.
References:
{context}
"""
def create_proxy_app(ctx: AppContext | None = None) -> FastAPI:
"""Build the proxy FastAPI application."""
ctx = ctx or get_context()
app = FastAPI(
title="MiniCS Proxy",
version=__version__,
description=(
"OpenAI-compatible endpoint backed by MiniCS hybrid retrieval "
"(RAG + knowledge graph + optional rerank)."
),
)
# ------------------------------------------------------------------
# helpers
# ------------------------------------------------------------------
def _last_user_text(messages: list[dict[str, Any]]) -> str:
for message in reversed(messages):
if message.get("role") == "user":
content = message.get("content") or ""
if isinstance(content, list): # multimodal content parts
content = " ".join(
part.get("text", "") for part in content if isinstance(part, dict)
)
return str(content).strip()
return ""
def _retrieve(query: str, options: MinicsOptions):
"""Run retrieval; returns ``(result, citations)`` — both may be empty."""
if not query or not options.use_grounding:
return None, []
if not ctx.config.is_ready():
return None, []
if not (options.use_rag or options.use_graph):
return None, []
try:
result = ctx.retriever.retrieve(
query,
top_k=options.top_k,
use_rag=options.use_rag,
use_graph=options.use_graph,
document_ids=options.document_ids or None,
)
except Exception as exc: # noqa: BLE001 - never fail the chat on retrieval
LOGGER.warning("retrieval failed: %s", exc)
return None, []
return result, result.citations()
def _metadata(result, options: MinicsOptions, model: str) -> dict[str, Any]:
"""Build the ``minics`` block attached to every response."""
block: dict[str, Any] = {
"version": __version__,
"model": model,
"grounded": bool(result is not None and result.chunks),
}
if result is None:
block["retrieval"] = {"used": False}
return block
documents: dict[int, str] = {}
for chunk in result.chunks:
if chunk.document_id:
documents.setdefault(chunk.document_id, chunk.document_title)
block["retrieval"] = {
"used": bool(result.chunks),
"query": result.query,
"used_rag": result.used_rag,
"used_graph": result.used_graph,
"rerank": bool(result.diagnostics.get("rerank")),
"top_k": options.top_k,
"chunks": len(result.chunks),
"documents": [{"id": doc_id, "title": title} for doc_id, title in documents.items()],
"citations": [c.model_dump() for c in result.citations()],
"diagnostics": result.diagnostics,
}
return block
def _grounded_messages(
messages: list[dict[str, Any]], result, options: MinicsOptions
) -> list[dict[str, Any]]:
if result is None or not result.chunks:
return messages
context = result.context_text(max_chars=options.max_context_chars)
grounding = GROUNDING_TEMPLATE.format(context=context)
forwarded = [dict(m) for m in messages]
for message in forwarded:
if message.get("role") == "system":
message["content"] = f"{grounding}\n\n{message.get('content', '')}".strip()
break
else:
forwarded.insert(0, {"role": "system", "content": grounding})
return forwarded
def _upstream_payload(request: ChatCompletionRequest, options: MinicsOptions) -> dict[str, Any]:
payload = request.model_dump(exclude_none=True, exclude={"minics", "stream", "messages"})
payload["model"] = payload.get("model") or ctx.config.llm.model
return payload
# ------------------------------------------------------------------
# routes
# ------------------------------------------------------------------
@app.get("/health")
def health() -> dict[str, Any]:
config = ctx.config
return {
"status": "ok" if config.is_ready() else "degraded",
"service": "minics-proxy",
"version": __version__,
"model": config.llm.model,
"ready": config.is_ready(),
"rerank": config.rerank_ready,
}
@app.get("/v1/models")
def list_models() -> ModelListResponse:
return ModelListResponse(
data=[
ModelObject(
id=ctx.config.llm.model or "minics",
owned_by="minics",
)
]
)
@app.post("/v1/chat/completions")
def chat_completions(request: ChatCompletionRequest):
started = time.time()
options = request.minics or MinicsOptions()
messages = [m.model_dump(exclude_none=True) for m in request.messages]
query = _last_user_text(messages)
model = request.model or ctx.config.llm.model or "minics"
# 1-2. retrieval happens first, before anything is sent upstream.
result, _citations = _retrieve(query, options)
metadata = _metadata(result, options, model)
# 3. build the grounded prompt.
forwarded = _grounded_messages(messages, result, options)
# 4. call the configured LLM endpoint.
try:
client = build_openai_client(ctx.config)
except Exception as exc: # noqa: BLE001
return JSONResponse(status_code=502, content={"error": {"message": str(exc)}})
try:
if request.stream:
return StreamingResponse(
_stream_response(client, request, forwarded, metadata, model),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
response = client.chat.completions.create(
**_upstream_payload(request, options), messages=forwarded
)
# NOTE: messages intentionally passed after the payload so the
# grounded version always wins.
except LLMError as exc:
return JSONResponse(status_code=502, content={"error": {"message": str(exc)}})
except Exception as exc: # noqa: BLE001 - surface upstream errors as OpenAI errors
return JSONResponse(
status_code=502,
content={"error": {"message": f"Upstream LLM request failed: {exc}"}},
)
body = response.model_dump(exclude_none=True)
metadata["latency_ms"] = int((time.time() - started) * 1000)
body["minics"] = metadata
return JSONResponse(content=body)
def _stream_response(client, request: ChatCompletionRequest, forwarded, metadata, model):
"""Yield SSE chunks; the first chunk carries the ``minics`` metadata."""
try:
payload = _upstream_payload(request, request.minics or MinicsOptions())
stream = client.chat.completions.create(**payload, messages=forwarded, stream=True)
first = True
for chunk in stream:
body = chunk.model_dump(exclude_none=True)
if first:
body["minics"] = metadata
first = False
yield f"data: {json.dumps(body, ensure_ascii=False)}\n\n"
yield "data: [DONE]\n\n"
except Exception as exc: # noqa: BLE001 - best effort mid-stream error
LOGGER.exception("stream failed")
error = {"error": {"message": f"Upstream stream failed: {exc}"}}
yield f"data: {json.dumps(error)}\n\n"
return app
def run_proxy(
*,
host: str = "127.0.0.1",
port: int = 8766,
home: str | None = None,
log_level: str = "info",
) -> None:
"""Run the proxy with uvicorn (blocking — used by ``minics proxy``)."""
import uvicorn
ctx = get_context(home)
app = create_proxy_app(ctx)
uvicorn.run(app, host=host, port=port, log_level=log_level)