Tonic commited on
Commit
4854a72
1 Parent(s): e86fe78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -9,29 +9,46 @@ def query_vectara(question):
9
  user_message = question
10
 
11
  # Read authentication parameters from the .env file
12
- customer_id = config('CUSTOMER_ID')
13
- corpus_id = config('CORPUS_ID')
14
- api_key = config('API_KEY')
15
-
16
- # Define the query URL
17
- query_url = f"https://api.vectara.io:443/v1/query"
18
-
19
- headers = {
20
- "x-api-key": api_key, # Use the x-api-key header for authentication
 
 
 
 
 
 
 
 
 
21
  }
22
 
23
- query_body = {
24
- "query": user_message,
25
- "num_results": 10
 
26
  }
27
 
28
- query_response = requests.post(query_url, json=query_body, headers=headers)
29
-
30
- if query_response.status_code == 200:
31
- query_data = query_response.json()
 
 
 
 
 
 
 
32
  response_message = f"Response from Vectara API: {json.dumps(query_data, indent=2)}"
33
  else:
34
- response_message = f"Error: {query_response.status_code}"
35
 
36
  return response_message
37
 
 
9
  user_message = question
10
 
11
  # Read authentication parameters from the .env file
12
+ CUSTOMER_ID = config('CUSTOMER_ID')
13
+ CORPUS_ID = config('CORPUS_ID')
14
+ API_KEY = config('API_KEY')
15
+
16
+ # Define the data dictionary
17
+ data_dict = {
18
+ "query": [
19
+ {
20
+ "query": user_message,
21
+ "num_results": 10,
22
+ "corpus_key": [
23
+ {
24
+ "customer_id": CUSTOMER_ID,
25
+ "corpus_id": CORPUS_ID
26
+ }
27
+ ]
28
+ }
29
+ ]
30
  }
31
 
32
+ # Define the headers
33
+ api_key_header = {
34
+ "customer-id": CUSTOMER_ID,
35
+ "x-api-key": API_KEY
36
  }
37
 
38
+ # Make the API request
39
+ response = requests.post(
40
+ "https://api.vectara.io/v1/query",
41
+ data=json.dumps(data_dict),
42
+ verify=True,
43
+ headers=api_key_header,
44
+ headers={"Content-Type": "application/json"} # Set Content-Type header
45
+ )
46
+
47
+ if response.status_code == 200:
48
+ query_data = response.json()
49
  response_message = f"Response from Vectara API: {json.dumps(query_data, indent=2)}"
50
  else:
51
+ response_message = f"Error: {response.status_code}"
52
 
53
  return response_message
54