Spaces:
Sleeping
Sleeping
Update query.py
Browse files
query.py
CHANGED
@@ -56,7 +56,6 @@ class VectaraQuery():
|
|
56 |
}
|
57 |
|
58 |
def submit_query(self, query_str: str):
|
59 |
-
|
60 |
endpoint = f"https://api.vectara.io/v1/stream-query"
|
61 |
body = self.get_body(query_str)
|
62 |
response = requests.post(endpoint, data=json.dumps(body), verify=True, headers=self.get_headers(), stream=True)
|
@@ -64,14 +63,12 @@ class VectaraQuery():
|
|
64 |
print(f"Query failed with code {response.status_code}, reason {response.reason}, text {response.text}")
|
65 |
return "Sorry, something went wrong. Please try again later."
|
66 |
|
67 |
-
|
68 |
-
accumulated_text = "" # Initialize text accumulation
|
69 |
-
pattern_max_length = 50 # Example heuristic
|
70 |
for line in response.iter_lines():
|
71 |
if line: # filter out keep-alive new lines
|
72 |
data = json.loads(line.decode('utf-8'))
|
73 |
print(f"Received data chunk: {json.dumps(data, indent=2)}") # Debugging line
|
74 |
-
|
75 |
if 'result' not in data:
|
76 |
continue
|
77 |
|
@@ -87,14 +84,14 @@ class VectaraQuery():
|
|
87 |
continue
|
88 |
text = result['text']
|
89 |
print(f"Processing text: {text}") # Debugging line
|
90 |
-
|
91 |
-
reason_match = re.search(r"Reason Why it Can't be Used: (.*?)\n", text)
|
92 |
-
alternative_match = re.search(r"Alternative: (.*?)\n", text)
|
93 |
-
notes_match = re.search(r"Notes: (.*?)\n", text)
|
94 |
|
95 |
-
reason = reason_match.group(1) if reason_match else "Not available"
|
96 |
-
alternative = alternative_match.group(1) if alternative_match else "Not available"
|
97 |
-
notes = notes_match.group(1) if notes_match else "Not available"
|
98 |
|
99 |
response = f"Reason: {reason}\nAlternative: {alternative}\nNotes: {notes}"
|
100 |
return response
|
|
|
56 |
}
|
57 |
|
58 |
def submit_query(self, query_str: str):
|
|
|
59 |
endpoint = f"https://api.vectara.io/v1/stream-query"
|
60 |
body = self.get_body(query_str)
|
61 |
response = requests.post(endpoint, data=json.dumps(body), verify=True, headers=self.get_headers(), stream=True)
|
|
|
63 |
print(f"Query failed with code {response.status_code}, reason {response.reason}, text {response.text}")
|
64 |
return "Sorry, something went wrong. Please try again later."
|
65 |
|
66 |
+
accumulated_text = ""
|
|
|
|
|
67 |
for line in response.iter_lines():
|
68 |
if line: # filter out keep-alive new lines
|
69 |
data = json.loads(line.decode('utf-8'))
|
70 |
print(f"Received data chunk: {json.dumps(data, indent=2)}") # Debugging line
|
71 |
+
|
72 |
if 'result' not in data:
|
73 |
continue
|
74 |
|
|
|
84 |
continue
|
85 |
text = result['text']
|
86 |
print(f"Processing text: {text}") # Debugging line
|
87 |
+
|
88 |
+
reason_match = re.search(r"Reason Why it Can't be Used: (.*?)\n", text, re.DOTALL)
|
89 |
+
alternative_match = re.search(r"Alternative: (.*?)\n", text, re.DOTALL)
|
90 |
+
notes_match = re.search(r"Notes: (.*?)\n", text, re.DOTALL)
|
91 |
|
92 |
+
reason = reason_match.group(1).strip() if reason_match else "Not available"
|
93 |
+
alternative = alternative_match.group(1).strip() if alternative_match else "Not available"
|
94 |
+
notes = notes_match.group(1).strip() if notes_match else "Not available"
|
95 |
|
96 |
response = f"Reason: {reason}\nAlternative: {alternative}\nNotes: {notes}"
|
97 |
return response
|