-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
86 lines (68 loc) · 2.62 KB
/
example_usage.py
File metadata and controls
86 lines (68 loc) · 2.62 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
#!/usr/bin/env python3
"""
Example usage of the Vector Knowledge Base API
This script demonstrates how to:
1. Upload a PDF document
2. Query the knowledge base
3. Process the results
Before running: Set VKB_API_KEY environment variable
Usage: python example_usage.py
"""
import os
import requests
import json
# Configuration
API_BASE_URL = "http://localhost:8080/api" # Change to your deployed URL
API_KEY = os.environ.get("VKB_API_KEY")
if not API_KEY:
print("Please set VKB_API_KEY environment variable")
exit(1)
def upload_document(file_path):
"""Upload a PDF document to the knowledge base"""
headers = {"X-API-KEY": API_KEY}
with open(file_path, 'rb') as f:
files = {'file': (os.path.basename(file_path), f, 'application/pdf')}
response = requests.post(f"{API_BASE_URL}/upload", files=files, headers=headers)
if response.status_code == 200:
result = response.json()
print(f"✅ Upload successful! Document ID: {result['document_id']}")
return result['document_id']
else:
print(f"❌ Upload failed: {response.text}")
return None
def query_documents(query):
"""Query the knowledge base"""
headers = {"X-API-KEY": API_KEY, "Content-Type": "application/json"}
data = {"query": query}
response = requests.post(f"{API_BASE_URL}/query", json=data, headers=headers)
if response.status_code == 200:
results = response.json()
print(f"✅ Found {len(results['results'])} results for: '{query}'")
for i, result in enumerate(results['results'], 1):
print(f"\n--- Result {i} ---")
print(f"Source: {result['title']}")
print(f"Score: {result['score']:.3f}")
print(f"Content: {result['content'][:200]}...")
return results
else:
print(f"❌ Query failed: {response.text}")
return None
if __name__ == "__main__":
print("Vector Knowledge Base API Example")
print("=" * 40)
# Example 1: Upload a document (if you have a PDF file)
# doc_id = upload_document("your_document.pdf")
# Example 2: Query the knowledge base
query_examples = [
"What is machine learning?",
"Tell me about artificial intelligence",
"How does neural network work?"
]
for query in query_examples:
print(f"\n🔍 Searching: {query}")
results = query_documents(query)
if results and results['results']:
print("✅ Search completed successfully")
else:
print("ℹ️ No results found - try uploading some documents first")
print("-" * 50)