-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_client.py
executable file
·124 lines (100 loc) · 3.79 KB
/
example_client.py
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
#!/usr/bin/env python3
import argparse
import json
import requests
def query_rag_api(
query, top_k=5, filter_type=None, filter_path=None, api_url="http://localhost:8000"
):
"""Query the RAG API and return results."""
# Prepare the request
endpoint = f"{api_url}/query"
payload = {"query": query, "top_k": top_k}
# Add optional filters if provided
if filter_type:
payload["filter_type"] = filter_type
if filter_path:
payload["filter_path"] = filter_path
# Make the request
try:
response = requests.post(endpoint, json=payload)
response.raise_for_status() # Raise exception for HTTP errors
# Parse the JSON response
data = response.json()
return data
except requests.exceptions.RequestException as e:
print(f"Error querying API: {e}")
return None
def format_results(response_data):
"""Format the API response for display."""
if not response_data or "results" not in response_data:
return "No results found."
results = response_data["results"]
total = response_data["total"]
if not results:
return "No matching results found."
output = f"Found {total} relevant results:\n\n"
for i, result in enumerate(results, 1):
# Format the content (pretty-print JSON if possible)
try:
content_json = json.loads(result["content"])
formatted_content = json.dumps(content_json, indent=2)
except json.JSONDecodeError:
formatted_content = result["content"]
# Format the metadata
metadata = result["metadata"]
filepath = metadata.get("filepath", "unknown")
doc_type = metadata.get("type", "unknown")
# Add additional context based on the document type
if doc_type == "perspective":
component = metadata.get("component", "")
component_info = f"\nComponent: {component}" if component else ""
section = metadata.get("section", "")
section_info = f"\nSection: {section}" if section else ""
context = component_info + section_info
elif doc_type == "tag":
folder = metadata.get("folder", "")
folder_info = f"\nFolder: {folder}" if folder else ""
context = folder_info
else:
context = ""
# Format the entire result
output += f"Result {i} (Similarity: {result['similarity']:.4f}):\n"
output += f"Source: {filepath} (Type: {doc_type}){context}\n"
output += "Content:\n"
output += f"{formatted_content}\n\n"
output += "-" * 80 + "\n\n"
return output
def main():
# Set up argument parser
parser = argparse.ArgumentParser(description="Query the Ignition RAG API")
parser.add_argument("query", help="The natural language query to search for")
parser.add_argument(
"--top-k", type=int, default=3, help="Number of results to return (default: 3)"
)
parser.add_argument(
"--filter-type", choices=["perspective", "tag"], help="Filter by document type"
)
parser.add_argument("--filter-path", help="Filter by file path pattern")
parser.add_argument(
"--api-url",
default="http://localhost:8000",
help="API URL (default: http://localhost:8000)",
)
# Parse arguments
args = parser.parse_args()
# Query the API
results = query_rag_api(
query=args.query,
top_k=args.top_k,
filter_type=args.filter_type,
filter_path=args.filter_path,
api_url=args.api_url,
)
# Format and display results
if results:
print(format_results(results))
else:
print("Failed to get results from the API.")
print("Make sure the API server is running and accessible.")
if __name__ == "__main__":
main()