|
|
import gradio as gr |
|
|
import os |
|
|
import json |
|
|
import requests |
|
|
|
|
|
|
|
|
ragie_api_key = os.getenv('RAGIE_API_KEY') |
|
|
|
|
|
def get_chunks(query): |
|
|
"""Retrieve chunks with detailed API response checking""" |
|
|
try: |
|
|
|
|
|
if ragie_api_key: |
|
|
print("API key found") |
|
|
else: |
|
|
return "Error: No Ragie API key found in environment variables" |
|
|
|
|
|
print(f"\nSending query: {query}") |
|
|
|
|
|
|
|
|
response = requests.post( |
|
|
"https://api.ragie.ai/retrievals", |
|
|
headers={ |
|
|
"Content-Type": "application/json", |
|
|
"Authorization": f"Bearer {ragie_api_key}" |
|
|
}, |
|
|
json={ |
|
|
"query": query, |
|
|
"filter": { |
|
|
"scope": "tutorial" |
|
|
} |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
print(f"\nAPI Response Status: {response.status_code}") |
|
|
print(f"Response Headers: {dict(response.headers)}") |
|
|
|
|
|
if response.status_code != 200: |
|
|
error_detail = "" |
|
|
try: |
|
|
error_detail = response.json() |
|
|
print(f"Error Response Body: {error_detail}") |
|
|
except: |
|
|
error_detail = response.text |
|
|
print(f"Error Response Text: {error_detail}") |
|
|
return f"""API Call Failed: |
|
|
Status Code: {response.status_code} |
|
|
Error Details: {error_detail}""" |
|
|
|
|
|
|
|
|
data = response.json() |
|
|
print(f"\nFound {len(data.get('scored_chunks', []))} chunks") |
|
|
|
|
|
|
|
|
result = f"""API Call Successful: |
|
|
Status Code: {response.status_code} |
|
|
Number of Chunks: {len(data.get('scored_chunks', []))} |
|
|
|
|
|
Retrieved Chunks: |
|
|
""" |
|
|
for i, chunk in enumerate(data.get("scored_chunks", []), 1): |
|
|
result += f"\nChunk {i} (Score: {chunk['score']}):\n" |
|
|
result += f"{chunk['text']}\n" |
|
|
result += "-" * 50 |
|
|
|
|
|
return result if data.get("scored_chunks") else "API call successful but no chunks were found for this query." |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"Error during API call: {str(e)}" |
|
|
print(error_msg) |
|
|
return error_msg |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=get_chunks, |
|
|
inputs=gr.Textbox(label="Enter your query"), |
|
|
outputs=gr.Textbox(label="API Response and Retrieved Chunks", lines=20), |
|
|
title="Ragie Chunk Retriever with API Debugging", |
|
|
description="Enter a query to see full API response details and retrieved chunks." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |