File size: 2,774 Bytes
fc18e16
e8ed9ef
 
3c9b1a0
fc18e16
07f9509
e8ed9ef
fc18e16
07f9509
 
3c9b1a0
07f9509
 
 
 
 
 
 
 
 
3c9b1a0
e8ed9ef
 
 
 
 
 
 
 
07f9509
e8ed9ef
 
3c9b1a0
 
07f9509
 
 
 
3c9b1a0
07f9509
 
 
 
 
 
 
 
 
 
3c9b1a0
07f9509
3c9b1a0
07f9509
e8ed9ef
07f9509
 
 
 
 
 
 
 
 
 
 
e8ed9ef
07f9509
e8ed9ef
 
07f9509
 
 
fc18e16
07f9509
 
 
 
 
 
 
fc18e16
 
 
e8ed9ef
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
import gradio as gr
import os
import json
import requests

# Get Ragie API key
ragie_api_key = os.getenv('RAGIE_API_KEY')

def get_chunks(query):
    """Retrieve chunks with detailed API response checking"""
    try:
        # Print API key status (safely)
        if ragie_api_key:
            print("API key found")
        else:
            return "Error: No Ragie API key found in environment variables"
            
        print(f"\nSending query: {query}")
            
        # Make the API call
        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"  # Adjust this to match your scope
                }
            }
        )
        
        # Print full response details
        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}"""
            
        # Parse the successful response
        data = response.json()
        print(f"\nFound {len(data.get('scored_chunks', []))} chunks")
        
        # Format the chunks with API response info
        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

# Create a simple interface
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()