-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
178 lines (147 loc) · 6.02 KB
/
cli.py
File metadata and controls
178 lines (147 loc) · 6.02 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
"""Command-line interface for InsightDocs."""
import click
import requests
import json
from pathlib import Path
import os
API_BASE_URL = "http://localhost:8000/api/v1"
TOKEN_FILE = Path.home() / ".insightdocs_token"
def get_headers():
"""Get headers with authentication token if available."""
if TOKEN_FILE.exists():
token = TOKEN_FILE.read_text().strip()
return {"Authorization": f"Bearer {token}"}
return {}
@click.group()
def cli():
"""InsightDocs CLI - Manage documents and queries."""
pass
@cli.command()
@click.option('--email', prompt=True, help='User email')
@click.option('--password', prompt=True, hide_input=True, help='User password')
def login(email, password):
"""Login to get an authentication token."""
try:
response = requests.post(
f"{API_BASE_URL}/auth/login",
data={"username": email, "password": password} # OAuth2 expects form data
)
if response.status_code == 200:
token = response.json().get("access_token")
TOKEN_FILE.write_text(token)
click.echo("✓ Login successful! Token saved.")
else:
click.echo(f"✗ Login failed: {response.text}", err=True)
except Exception as e:
click.echo(f"✗ Error: {e}", err=True)
@cli.command()
@click.argument('file_path', type=click.Path(exists=True))
def upload(file_path):
"""Upload a document for processing."""
headers = get_headers()
if not headers:
click.echo("✗ Not logged in. Run 'python cli.py login' first.", err=True)
return
click.echo(f"Uploading {file_path}...")
with open(file_path, 'rb') as f:
files = {'file': (Path(file_path).name, f)}
response = requests.post(f"{API_BASE_URL}/documents/upload", files=files, headers=headers)
if response.status_code == 200:
data = response.json()
click.echo(f"✓ Document uploaded successfully!")
click.echo(f" Document ID: {data['document_id']}")
click.echo(f" Task ID: {data['task_id']}")
elif response.status_code == 401:
click.echo("✗ Unauthorized. Please login again.", err=True)
else:
click.echo(f"✗ Upload failed: {response.text}", err=True)
@cli.command()
@click.argument('query_text')
@click.option('--top-k', default=5, help='Number of results to retrieve')
def query(query_text, top_k):
"""Query documents using natural language."""
headers = get_headers()
if not headers:
click.echo("✗ Not logged in. Run 'python cli.py login' first.", err=True)
return
click.echo(f"Processing query: {query_text}")
payload = {
"query": query_text,
"top_k": top_k
}
response = requests.post(f"{API_BASE_URL}/query/", json=payload, headers=headers)
if response.status_code == 200:
data = response.json()
click.echo("\n" + "="*60)
click.echo("ANSWER:")
click.echo("="*60)
click.echo(data['answer'])
click.echo("\n" + "="*60)
click.echo(f"SOURCES ({len(data['sources'])} found):")
click.echo("="*60)
for i, source in enumerate(data['sources'], 1):
click.echo(f"\n[{i}] (distance: {source.get('distance', 0):.4f})")
content = source.get('text', '') or source.get('content_preview', '')
click.echo(content[:200] + "..." if len(content) > 200 else content)
elif response.status_code == 401:
click.echo("✗ Unauthorized. Please login again.", err=True)
else:
click.echo(f"✗ Query failed: {response.text}", err=True)
@cli.command()
def list_documents():
"""List all documents."""
headers = get_headers()
if not headers:
click.echo("✗ Not logged in. Run 'python cli.py login' first.", err=True)
return
response = requests.get(f"{API_BASE_URL}/documents/", headers=headers)
if response.status_code == 200:
data = response.json()
click.echo(f"\nFound {data['total']} document(s):\n")
for doc in data['documents']:
click.echo(f"ID: {doc['id']}")
click.echo(f" Filename: {doc['filename']}")
click.echo(f" Status: {doc['status']}")
click.echo(f" Created: {doc['created_at']}")
click.echo()
elif response.status_code == 401:
click.echo("✗ Unauthorized. Please login again.", err=True)
else:
click.echo(f"✗ Failed to list documents: {response.text}", err=True)
@cli.command()
@click.argument('task_id')
def status(task_id):
"""Check status of a task."""
headers = get_headers()
# Note: Status check might be public depending on implementation, but safer to include auth
# If unauth is allowed for task status, we can relax this. Assuming auth required.
response = requests.get(f"{API_BASE_URL}/tasks/{task_id}", headers=headers)
if response.status_code == 200:
data = response.json()
click.echo(f"\nTask Status: {data['status']}")
click.echo(f"Progress: {data['progress']:.1f}%")
if data.get('error'):
click.echo(f"Error: {data['error']}")
if data.get('result'):
click.echo(f"\nResult:")
click.echo(json.dumps(data['result'], indent=2))
elif response.status_code == 401:
click.echo("✗ Unauthorized. Please login again.", err=True)
else:
click.echo(f"✗ Failed to get status: {response.text}", err=True)
@cli.command()
def health():
"""Check system health."""
response = requests.get(f"{API_BASE_URL}/health")
if response.status_code == 200:
data = response.json()
click.echo(f"\nSystem Status: {data['status']}")
click.echo(f"Version: {data['version']}")
click.echo("\nComponents:")
for component, status in data['components'].items():
icon = "✓" if status in ["healthy", "operational"] else "✗"
click.echo(f" {icon} {component}: {status}")
else:
click.echo(f"✗ Health check failed: {response.text}", err=True)
if __name__ == '__main__':
cli()