Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import requests
|
|
7 |
ragie_api_key = os.getenv('RAGIE_API_KEY')
|
8 |
|
9 |
def get_chunks(query):
|
10 |
-
"""Retrieve chunks
|
11 |
try:
|
12 |
# Print API key status (safely)
|
13 |
if ragie_api_key:
|
@@ -17,67 +17,50 @@ def get_chunks(query):
|
|
17 |
|
18 |
print(f"\nSending query: {query}")
|
19 |
|
20 |
-
# Make the API call
|
21 |
response = requests.post(
|
22 |
"https://api.ragie.ai/retrievals",
|
23 |
headers={
|
24 |
"Content-Type": "application/json",
|
25 |
-
"Authorization":
|
26 |
},
|
27 |
json={
|
28 |
"query": query,
|
29 |
"filter": {
|
30 |
-
"scope": "tutorial"
|
31 |
}
|
32 |
}
|
33 |
)
|
34 |
|
35 |
-
# Print
|
36 |
print(f"\nAPI Response Status: {response.status_code}")
|
37 |
-
print(f"Response Headers: {dict(response.headers)}")
|
38 |
|
39 |
if response.status_code != 200:
|
40 |
-
|
41 |
-
try:
|
42 |
-
error_detail = response.json()
|
43 |
-
print(f"Error Response Body: {error_detail}")
|
44 |
-
except:
|
45 |
-
error_detail = response.text
|
46 |
-
print(f"Error Response Text: {error_detail}")
|
47 |
-
return f"""API Call Failed:
|
48 |
-
Status Code: {response.status_code}
|
49 |
-
Error Details: {error_detail}"""
|
50 |
|
51 |
-
# Parse the
|
52 |
data = response.json()
|
53 |
-
print(f"\
|
54 |
|
55 |
-
# Format the chunks
|
56 |
-
result =
|
57 |
-
Status Code: {response.status_code}
|
58 |
-
Number of Chunks: {len(data.get('scored_chunks', []))}
|
59 |
-
|
60 |
-
Retrieved Chunks:
|
61 |
-
"""
|
62 |
for i, chunk in enumerate(data.get("scored_chunks", []), 1):
|
63 |
-
result += f"
|
64 |
result += f"{chunk['text']}\n"
|
65 |
-
result += "-" * 50
|
66 |
|
67 |
-
return result if data.get("scored_chunks") else "
|
68 |
|
69 |
except Exception as e:
|
70 |
-
|
71 |
-
print(error_msg)
|
72 |
-
return error_msg
|
73 |
|
74 |
-
# Create
|
75 |
demo = gr.Interface(
|
76 |
fn=get_chunks,
|
77 |
inputs=gr.Textbox(label="Enter your query"),
|
78 |
-
outputs=gr.Textbox(label="
|
79 |
-
title="Ragie Chunk Retriever
|
80 |
-
description="Enter a query to see
|
81 |
)
|
82 |
|
83 |
if __name__ == "__main__":
|
|
|
7 |
ragie_api_key = os.getenv('RAGIE_API_KEY')
|
8 |
|
9 |
def get_chunks(query):
|
10 |
+
"""Retrieve chunks using exact format from docs"""
|
11 |
try:
|
12 |
# Print API key status (safely)
|
13 |
if ragie_api_key:
|
|
|
17 |
|
18 |
print(f"\nSending query: {query}")
|
19 |
|
20 |
+
# Make the API call exactly as shown in docs
|
21 |
response = requests.post(
|
22 |
"https://api.ragie.ai/retrievals",
|
23 |
headers={
|
24 |
"Content-Type": "application/json",
|
25 |
+
"Authorization": "Bearer " + ragie_api_key
|
26 |
},
|
27 |
json={
|
28 |
"query": query,
|
29 |
"filter": {
|
30 |
+
"scope": "tutorial"
|
31 |
}
|
32 |
}
|
33 |
)
|
34 |
|
35 |
+
# Print response status
|
36 |
print(f"\nAPI Response Status: {response.status_code}")
|
|
|
37 |
|
38 |
if response.status_code != 200:
|
39 |
+
return f"API Call Failed: Status Code {response.status_code}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
# Parse the response
|
42 |
data = response.json()
|
43 |
+
print(f"\nAPI Response: {json.dumps(data, indent=2)}")
|
44 |
|
45 |
+
# Format the chunks
|
46 |
+
result = "Retrieved Chunks:\n\n"
|
|
|
|
|
|
|
|
|
|
|
47 |
for i, chunk in enumerate(data.get("scored_chunks", []), 1):
|
48 |
+
result += f"Chunk {i}:\n"
|
49 |
result += f"{chunk['text']}\n"
|
50 |
+
result += "-" * 50 + "\n"
|
51 |
|
52 |
+
return result if data.get("scored_chunks") else "No chunks found for this query."
|
53 |
|
54 |
except Exception as e:
|
55 |
+
return f"Error: {str(e)}"
|
|
|
|
|
56 |
|
57 |
+
# Create interface
|
58 |
demo = gr.Interface(
|
59 |
fn=get_chunks,
|
60 |
inputs=gr.Textbox(label="Enter your query"),
|
61 |
+
outputs=gr.Textbox(label="Retrieved Chunks", lines=20),
|
62 |
+
title="Ragie Chunk Retriever",
|
63 |
+
description="Enter a query to see what chunks Ragie retrieves."
|
64 |
)
|
65 |
|
66 |
if __name__ == "__main__":
|