-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_app_FastAPI.py
More file actions
224 lines (183 loc) · 7.77 KB
/
client_app_FastAPI.py
File metadata and controls
224 lines (183 loc) · 7.77 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
# client_app_FastAPI.py — CLI insights assistant using FASTAPI Client + RAG search
# - Friendly error handling (Auth/RateLimit/HTTP/Connection)
# - Optional LLM summarizer (Ollama/OpenAI) with citations
# - Flags: --server, --token, --verbose, --llm, --llm-provider, --ollama-url, --ollama-model, --openai-model
from __future__ import annotations
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv()) # loads ${workspaceFolder}/.env by default
import argparse
import os
import sys
import textwrap
from typing import Any, Dict, Optional
from FastAPI_client import (
FastAPIClient,
AuthError,
RateLimitError,
FastAPIHTTPError,
FastAPIConnectionError,
)
from summarizer import summarize # provider-agnostic: Ollama or OpenAI
# -------- helpers --------
def fmt_hit(hit: Dict[str, Any]) -> str:
src = hit.get("source", {}) or {}
badge = []
if src.get("doc_type"):
badge.append(src["doc_type"])
if src.get("page") is not None:
badge.append(f"p.{src['page']}")
if src.get("row") is not None:
badge.append(f"row {src['row']}")
where = " • ".join(badge) if badge else "source"
content = (hit.get("content") or "").replace("\n", " ")
preview = content[:200] + ("…" if len(content) > 200 else "")
score = hit.get("score", 0.0)
return f"- [{where}] {preview} (score={score:.3f})"
def print_error(e: Exception, verbose: bool = False) -> int:
"""Map exceptions to friendly guidance and return a non-zero exit code."""
def vprint(msg: str) -> None:
if verbose:
print(msg)
if isinstance(e, AuthError):
print("❌ Authorization error.")
print("→ Re-authenticate or request the missing scope/role.")
print(" - Pass a token with --token or set FASTAPI_TOKEN.")
print(" - Confirm you’re calling allowed tools/endpoints.")
if getattr(e, "payload", None):
vprint(f"[payload] {e.payload}")
return 2
if isinstance(e, RateLimitError):
wait = getattr(e, "retry_after", None) or "a few"
print("⏳ Rate limit hit (429).")
print(f"→ Wait {wait} seconds and try again.")
print(" - Reduce result size (e.g., lower k/per_page), or slow requests.")
if getattr(e, "payload", None):
vprint(f"[payload] {e.payload}")
return 3
if isinstance(e, FastAPIConnectionError):
print("🔌 Cannot reach the server.")
print("→ Is the server running? Try:")
print(" uvicorn FastAPI_server:app --port 8799 --reload")
print(" - Check --server URL, firewall/VPN, and network connectivity.")
vprint(str(e))
return 4
if isinstance(e, FastAPIHTTPError):
status = e.status
print(f"❌ HTTP error ({status}).")
if status == 400:
print("→ Bad request. Check parameters (paths, text_col, k).")
elif status == 404:
print("→ Not found. For ingest: verify file path. For search: try a broader query.")
elif status == 413:
print("→ Payload too large. Split the file or reduce fields/text.")
elif status == 422:
print("→ Validation error. Ensure required fields exist and are correctly typed.")
elif 500 <= status < 600:
print("→ Server issue. Try again shortly or check server logs.")
else:
print("→ Fix parameters and retry, or inspect server logs.")
if getattr(e, "payload", None):
vprint(f"[payload] {e.payload}")
return 5
# Fallback
print("❌ Unexpected error.")
if verbose:
import traceback
traceback.print_exc()
else:
print(str(e))
return 1
def make_host(args) -> FastAPIClient:
base_url = args.server or os.getenv("FASTAPI_SERVER_URL", "http://127.0.0.1:8799")
token = args.token or os.getenv("FASTAPI_TOKEN")
return FastAPIClient(base_url=base_url, token=token, max_attempts=3)
# -------- commands --------
def cmd_ingest_pdf(args) -> int:
host = make_host(args)
try:
res = host.ingest_pdf(args.path, doc_type=args.doc_type)
print(res)
return 0
except Exception as e:
return print_error(e, verbose=args.verbose)
def cmd_ingest_csv(args) -> int:
host = make_host(args)
try:
res = host.ingest_csv(args.path, text_col=args.text_col, doc_type=args.doc_type)
print(res)
return 0
except Exception as e:
return print_error(e, verbose=args.verbose)
def cmd_ask(args) -> int:
host = make_host(args)
try:
res = host.search_docs(args.query, k=args.k)
hits = res.get("hits", []) if isinstance(res, dict) else []
if not hits:
print("ℹ️ No matches. Try widening the query or ingesting more documents.")
return 0
print("Top matches:")
for h in hits:
print(fmt_hit(h))
# Build snippets + citations for summarizer
snippets = [h.get("content", "") for h in hits]
citations = [str(h.get("source", {}) or {}) for h in hits]
llm_answer = None
if args.llm:
llm_answer = summarize(
args.query,
snippets,
citations,
provider=args.llm_provider,
ollama_url=args.ollama_url,
ollama_model=args.ollama_model,
openai_model=args.openai_model,
)
if llm_answer:
print("\nAnswer (LLM, cited):\n", llm_answer)
else:
# Fallback: extractive answer from top snippets
joined = " ".join(snippets)[:1500]
answer = textwrap.shorten(joined, width=500, placeholder=" …")
print("\nAnswer (extractive):\n", answer)
print("\nCitations:")
for h in hits:
print(" -", h.get("source", {}))
return 0
except Exception as e:
return print_error(e, verbose=args.verbose)
# -------- CLI --------
def main() -> None:
p = argparse.ArgumentParser(description="Insights Assistant CLI — RAG + FASTAPI (friendly errors + optional LLM)")
p.add_argument("--server", help="FastAPI Server URL (default: env FASTAPI_SERVER_URL or http://127.0.0.1:8799)")
p.add_argument("--token", help="Bearer token for auth (optional, or set FASTAPI_TOKEN)")
p.add_argument("--verbose", action="store_true", help="Show detailed payloads/tracebacks on error")
sub = p.add_subparsers(dest="cmd")
p1 = sub.add_parser("ingest-pdf", help="Ingest a PDF")
p1.add_argument("path")
p1.add_argument("--doc-type", default="pdf")
p1.set_defaults(func=cmd_ingest_pdf)
p2 = sub.add_parser("ingest-csv", help="Ingest a CSV")
p2.add_argument("path")
p2.add_argument("--text-col", default=None, help="Optional column that contains text")
p2.add_argument("--doc-type", default="csv")
p2.set_defaults(func=cmd_ingest_csv)
p3 = sub.add_parser("ask", help="Ask a question against the RAG index")
p3.add_argument("query")
p3.add_argument("-k", type=int, default=5)
# LLM options
p3.add_argument("--llm", action="store_true", help="Use LLM to write a concise, cited summary")
p3.add_argument("--llm-provider", default=os.getenv("SUMMARIZER_PROVIDER", "ollama"),
choices=["ollama", "openai"], help="LLM backend (default: ollama)")
p3.add_argument("--ollama-url", default=os.getenv("OLLAMA_URL", "http://127.0.0.1:11434"))
p3.add_argument("--ollama-model", default=os.getenv("OLLAMA_MODEL", "llama3.1:8b"))
p3.add_argument("--openai-model", default=os.getenv("OPENAI_MODEL", "gpt-4o-mini"))
p3.set_defaults(func=cmd_ask)
args = p.parse_args()
if not getattr(args, "cmd", None):
p.print_help()
sys.exit(0)
rc = args.func(args)
sys.exit(rc)
if __name__ == "__main__":
main()