-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_docs.py
More file actions
executable file
·228 lines (186 loc) · 7.24 KB
/
query_docs.py
File metadata and controls
executable file
·228 lines (186 loc) · 7.24 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
#!/usr/bin/env python3
"""Query the ServiceNow documentation vector store"""
import click
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.syntax import Syntax
from rich import print as rprint
import json
from vector_store import VectorStoreManager
from config import settings
console = Console()
@click.command()
@click.argument('query', required=False)
@click.option(
'--interactive', '-i',
is_flag=True,
help='Interactive query mode'
)
@click.option(
'--max-results', '-k',
type=int,
default=5,
help='Maximum number of results to return'
)
@click.option(
'--threshold', '-t',
type=float,
default=0.7,
help='Minimum similarity score threshold'
)
@click.option(
'--show-metadata', '-m',
is_flag=True,
help='Show full metadata for results'
)
@click.option(
'--json-output', '-j',
is_flag=True,
help='Output results as JSON'
)
def query_documentation(query, interactive, max_results, threshold, show_metadata, json_output):
"""Query the ServiceNow documentation vector store"""
# Initialize vector store
try:
vector_store = VectorStoreManager()
info = vector_store.get_collection_info()
if not json_output:
console.print(f"[green]✓[/green] Connected to vector store")
console.print(f" Collection: {info['name']}")
console.print(f" Documents: {info['count']}")
console.print()
except Exception as e:
console.print(f"[red]✗[/red] Failed to connect to vector store: {e}")
console.print("[yellow]Run 'python index_docs.py' first to index the documentation[/yellow]")
return
# Interactive mode
if interactive:
console.print("[bold cyan]ServiceNow Documentation Query Tool[/bold cyan]")
console.print("Type 'quit' or 'exit' to stop, 'help' for commands\n")
while True:
try:
query_input = console.input("[bold green]Query>[/bold green] ")
if query_input.lower() in ['quit', 'exit', 'q']:
console.print("[yellow]Goodbye![/yellow]")
break
if query_input.lower() == 'help':
show_help()
continue
if query_input.lower().startswith('config'):
show_config()
continue
if query_input.strip():
perform_search(
vector_store,
query_input,
max_results,
threshold,
show_metadata,
json_output=False
)
except KeyboardInterrupt:
console.print("\n[yellow]Goodbye![/yellow]")
break
except Exception as e:
console.print(f"[red]Error: {e}[/red]")
# Single query mode
elif query:
perform_search(
vector_store,
query,
max_results,
threshold,
show_metadata,
json_output
)
else:
console.print("[yellow]Please provide a query or use --interactive mode[/yellow]")
console.print("Usage: python query_docs.py 'your query here'")
console.print(" or: python query_docs.py --interactive")
def perform_search(vector_store, query, max_results, threshold, show_metadata, json_output=False):
"""Perform a search and display results"""
# Search
results = vector_store.search_with_relevance(
query=query,
k=max_results,
distance_threshold=threshold
)
if json_output:
# JSON output mode
output = {
"query": query,
"results_count": len(results),
"results": results
}
print(json.dumps(output, indent=2))
return
# Display results
if not results:
console.print(f"[yellow]No results found for: '{query}'[/yellow]")
console.print(f"Try lowering the threshold (current: {threshold}) or using different keywords")
return
console.print(f"\n[bold cyan]Found {len(results)} results for: '{query}'[/bold cyan]\n")
for i, result in enumerate(results, 1):
# Create result panel
headers = result.get('headers', {})
if isinstance(headers, dict):
headers_str = ' > '.join([v for k, v in sorted(headers.items())]) if headers else "No headers"
else:
headers_str = str(headers) if headers else "No headers"
# Truncate content for display
content = result['content']
if len(content) > 500:
content = content[:500] + "..."
# Create panel content
panel_content = f"[bold]Score:[/bold] {result['score']:.3f}\n"
panel_content += f"[bold]Section:[/bold] {headers_str}\n\n"
panel_content += content
console.print(Panel(
panel_content,
title=f"[bold green]Result {i}[/bold green]",
border_style="green"
))
if show_metadata:
# Show metadata in a table
table = Table(title="Metadata", show_header=True)
table.add_column("Key", style="cyan")
table.add_column("Value", style="white")
for key, value in result['metadata'].items():
if key != 'headers': # Skip headers as we show them above
table.add_row(key, str(value))
console.print(table)
console.print()
def show_help():
"""Show help information"""
help_text = """
[bold cyan]Available Commands:[/bold cyan]
[green]<query>[/green] - Search for documentation
[green]help[/green] - Show this help message
[green]config[/green] - Show current configuration
[green]quit/exit[/green] - Exit the program
[bold cyan]Search Tips:[/bold cyan]
- Use specific technical terms for better results
- Include context like "configuration", "setup", "api", etc.
- Results are ranked by relevance score
[bold cyan]Examples:[/bold cyan]
- "Now Assist configuration"
- "AI agents setup"
- "Knowledge Graph API"
"""
console.print(Panel(help_text, title="Help", border_style="blue"))
def show_config():
"""Show current configuration"""
config_table = Table(title="Current Configuration", show_header=True)
config_table.add_column("Setting", style="cyan")
config_table.add_column("Value", style="white")
config_table.add_row("Embedding Model Type", settings.embedding_model_type)
config_table.add_row("Embedding Model", settings.embedding_model_name)
config_table.add_row("Collection Name", settings.collection_name)
config_table.add_row("Chunk Size", str(settings.chunk_size))
config_table.add_row("Chunk Overlap", str(settings.chunk_overlap))
config_table.add_row("Max Results", str(settings.max_results))
config_table.add_row("Similarity Threshold", str(settings.similarity_threshold))
console.print(config_table)
if __name__ == "__main__":
query_documentation()