-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
333 lines (267 loc) · 10.3 KB
/
api.py
File metadata and controls
333 lines (267 loc) · 10.3 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python3
"""
Vimprove API server.
Provides query endpoint for retrieving relevant docs and generating responses.
Usage:
export OPENROUTER_API_KEY=your_key
uv run api.py [--port 8000] [--host 0.0.0.0]
"""
import os
from pathlib import Path
from typing import Any
import httpx
import uvicorn
import argparse
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from dotenv import load_dotenv
from src.retriever import VimproveRetriever
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for startup and shutdown events."""
# Startup
global retriever, openrouter_key
cache_dir = Path(os.environ.get("VIMPROVE_CACHE_DIR", "./vimprove-cache")).resolve()
openrouter_key = os.environ.get("OPENROUTER_API_KEY")
if not openrouter_key:
print("Warning: OPENROUTER_API_KEY not set. Query endpoint will fail.")
print(f"Loading retriever from {cache_dir}...")
retriever = VimproveRetriever(cache_dir)
print(f"✓ Retriever ready ({retriever.collection.count()} chunks)")
yield # App runs here
app = FastAPI(
title="Vimprove API",
description="RAG-powered Neovim configuration assistant",
version="0.1.0",
lifespan=lifespan,
)
# Global retriever (loaded on startup)
retriever: VimproveRetriever | None = None
openrouter_key: str | None = None
class QueryRequest(BaseModel):
query: str = Field(..., description="Natural language query about Neovim")
context: str | None = Field(None, description="Optional config snippet for context")
n_results: int = Field(10, ge=1, le=50, description="Number of chunks to retrieve")
source_filter: str | None = Field(
None, description="Filter by source (e.g., 'telescope.nvim')"
)
model: str = Field(
"anthropic/claude-4.5-sonnet", description="OpenRouter model to use"
)
max_tokens: int = Field(
2000, ge=100, le=4000, description="Max tokens for response"
)
class QueryResponse(BaseModel):
query: str
response: str
sources: list[dict[str, Any]]
model_used: str
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {
"status": "healthy",
"retriever_loaded": retriever is not None,
"chunks_count": retriever.collection.count() if retriever else 0,
}
@app.post("/query", response_model=QueryResponse)
async def query(request: QueryRequest):
"""Query the documentation and generate a response. Returns explanation + config snippet + relevant doc sections."""
if not retriever:
raise HTTPException(status_code=503, detail="Retriever not initialized")
if not openrouter_key:
raise HTTPException(status_code=500, detail="OPENROUTER_API_KEY not set")
# Retrieve relevant chunks
results = retriever.search(
query=request.query,
n_results=request.n_results,
source_filter=request.source_filter,
)
if not results:
raise HTTPException(status_code=404, detail="No relevant documentation found")
# Build prompt
prompt = build_prompt(request.query, results, request.context)
# Call OpenRouter
response_text = await call_openrouter(
prompt=prompt, model=request.model, max_tokens=request.max_tokens
)
# Format sources for response
sources = [
{
"source": r["metadata"]["source"],
"type": r["metadata"]["type"],
"heading": r["metadata"].get("heading"), # vimdoc
"tags": r["metadata"].get("tags"), # vimdoc
"headings": r["metadata"].get("headings"), # markdown
"text": r["text"][:200] + "..." if len(r["text"]) > 200 else r["text"],
"distance": r["distance"],
}
for r in results[:5] # Top 5 sources only
]
return QueryResponse(
query=request.query,
response=response_text,
sources=sources,
model_used=request.model,
)
@app.post("/query/stream")
async def query_stream(request: QueryRequest):
"""
Streaming version of query endpoint.
Returns Server-Sent Events with response chunks.
"""
if not retriever:
raise HTTPException(status_code=503, detail="Retriever not initialized")
if not openrouter_key:
raise HTTPException(status_code=500, detail="OPENROUTER_API_KEY not set")
# Retrieve relevant chunks
results = retriever.search(
query=request.query,
n_results=request.n_results,
source_filter=request.source_filter,
)
if not results:
raise HTTPException(status_code=404, detail="No relevant documentation found")
# Build prompt
prompt = build_prompt(request.query, results, request.context)
# Stream response
async def generate():
async for chunk in stream_openrouter(
prompt=prompt, model=request.model, max_tokens=request.max_tokens
):
yield f"data: {chunk}\n\n"
return StreamingResponse(generate(), media_type="text/event-stream")
def build_prompt(query: str, results: list[dict[str, Any]], context: str | None) -> str:
"""Build prompt for LLM with retrieved docs and query."""
# Format retrieved chunks
docs_text = "\n\n---\n\n".join(
[
f"**Source:** {r['metadata']['source']}\n"
+ f"**Type:** {r['metadata']['type']}\n"
+ (
f"**Heading:** {r['metadata']['heading']}\n"
if r["metadata"].get("heading")
else ""
)
+ (
f"**Tags:** {r['metadata']['tags']}\n"
if r["metadata"].get("tags")
else ""
)
+ (
f"**Section:** {r['metadata']['headings']}\n"
if r["metadata"].get("headings")
else ""
)
+ "\n"
+ r["text"]
for r in results
]
)
context_section = ""
if context:
context_section = f"""## Current Config
```lua
{context}
```
"""
prompt = f"""You are an expert Neovim configuration assistant. Answer the user's question using the provided documentation.
## Query
{query}
{context_section}
## Documentation
{docs_text}
---
Provide a response with:
Explanation (2-3 sentences): What the user needs to do.
Configuration (complete Lua snippet): Ready-to-use code.
Sources (1-2 key references): Where to learn more.
Keep it concise. Use idiomatic Lua (e.g., vim.keymap.set over vim.api.nvim_set_keymap).
If the docs don't fully answer the question, say so."""
return prompt
async def call_openrouter(prompt: str, model: str, max_tokens: int) -> str:
"""Call OpenRouter API for completion."""
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {openrouter_key}",
"HTTP-Referer": "https://www.github.com/rlarson20/Vimprove",
"X-Title": "Vimprove",
"Content-Type": "application/json",
},
json={
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": max_tokens,
},
)
if response.status_code != 200:
raise HTTPException(
status_code=response.status_code,
detail=f"OpenRouter API error: {response.text}",
)
data = response.json()
return data["choices"][0]["message"]["content"]
async def stream_openrouter(prompt: str, model: str, max_tokens: int):
"""Stream response from OpenRouter API."""
async with httpx.AsyncClient(timeout=60.0) as client:
async with client.stream(
"POST",
"https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {openrouter_key}",
"HTTP-Referer": "https://www.github.com/rlarson20/Vimprove",
"X-Title": "Vimprove",
"Content-Type": "application/json",
},
json={
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": max_tokens,
"stream": True,
},
) as response:
if response.status_code != 200:
error_text = await response.aread()
raise HTTPException(
status_code=response.status_code,
detail=f"OpenRouter API error: {error_text.decode()}",
)
async for line in response.aiter_lines():
if line.startswith("data: "):
data = line[6:]
if data == "[DONE]":
break
# Handle empty or malformed data
if not data.strip():
continue
try:
import json
chunk = json.loads(data)
# Safely navigate the nested structure
if (
chunk.get("choices")
and len(chunk["choices"]) > 0
and chunk["choices"][0].get("delta")
and chunk["choices"][0]["delta"].get("content")
):
yield chunk["choices"][0]["delta"]["content"]
except json.JSONDecodeError:
# Handle malformed JSON specifically
continue
except (KeyError, IndexError, TypeError):
# Handle missing keys, empty arrays, or wrong types
continue
def main():
parser = argparse.ArgumentParser(description="Vimprove API server")
parser.add_argument("--host", default="127.0.0.1", help="Host to bind to")
parser.add_argument("--port", type=int, default=8000, help="Port to bind to")
parser.add_argument("--reload", action="store_true", help="Enable auto-reload")
args = parser.parse_args()
load_dotenv()
uvicorn.run("api:app", host=args.host, port=args.port, reload=args.reload)
if __name__ == "__main__":
main()