iamrobotbear commited on
Commit
d3f8557
1 Parent(s): 9b44f60

Update query.py

Browse files
Files changed (1) hide show
  1. query.py +13 -44
query.py CHANGED
@@ -1,12 +1,6 @@
1
  import requests
2
  import json
3
  import re
4
- from urllib.parse import quote
5
-
6
- def extract_between_tags(text, start_tag, end_tag):
7
- start_index = text.find(start_tag)
8
- end_index = text.find(end_tag, start_index)
9
- return text[start_index+len(start_tag):end_index]
10
 
11
  class VectaraQuery():
12
  def __init__(self, api_key: str, customer_id: str, corpus_id: str, prompt_name: str = None):
@@ -68,7 +62,7 @@ class VectaraQuery():
68
  response = requests.post(endpoint, data=json.dumps(body), verify=True, headers=self.get_headers(), stream=True)
69
  if response.status_code != 200:
70
  print(f"Query failed with code {response.status_code}, reason {response.reason}, text {response.text}")
71
- return "Sorry, something went wrong in my brain. Please try again later."
72
 
73
  chunks = []
74
  accumulated_text = "" # Initialize text accumulation
@@ -77,42 +71,17 @@ class VectaraQuery():
77
  if line: # filter out keep-alive new lines
78
  data = json.loads(line.decode('utf-8'))
79
  res = data['result']
80
- response_set = res['responseSet']
81
- if response_set is None:
82
- # grab next chunk and yield it as output
83
- summary = res.get('summary', None)
84
- if summary is None or len(summary)==0:
85
- continue
86
- else:
87
- chat = summary.get('chat', None)
88
- if chat and chat.get('status', None):
89
- st_code = chat['status']
90
- print(f"Chat query failed with code {st_code}")
91
- if st_code == 'RESOURCE_EXHAUSTED':
92
- self.conv_id = None
93
- return 'Sorry, Vectara chat turns exceeds plan limit.'
94
- return 'Sorry, something went wrong in my brain. Please try again later.'
95
- conv_id = chat.get('conversationId', None) if chat else None
96
- if conv_id:
97
- self.conv_id = conv_id
98
 
99
- chunk = summary['text']
100
- accumulated_text += chunk # Append current chunk to accumulation
101
- if len(accumulated_text) > pattern_max_length:
102
- accumulated_text = re.sub(r"\[\d+\]", "", accumulated_text)
103
- accumulated_text = re.sub(r"\s+\.", ".", accumulated_text)
104
- out_chunk = accumulated_text[:-pattern_max_length]
105
- chunks.append(out_chunk)
106
- yield out_chunk
107
- accumulated_text = accumulated_text[-pattern_max_length:]
108
-
109
- if summary['done']:
110
- break
111
 
112
- # yield the last piece
113
- if len(accumulated_text) > 0:
114
- accumulated_text = re.sub(r" \[\d+\]\.", ".", accumulated_text)
115
- chunks.append(accumulated_text)
116
- yield accumulated_text
117
-
118
- return ''.join(chunks)
 
1
  import requests
2
  import json
3
  import re
 
 
 
 
 
 
4
 
5
  class VectaraQuery():
6
  def __init__(self, api_key: str, customer_id: str, corpus_id: str, prompt_name: str = None):
 
62
  response = requests.post(endpoint, data=json.dumps(body), verify=True, headers=self.get_headers(), stream=True)
63
  if response.status_code != 200:
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
  chunks = []
68
  accumulated_text = "" # Initialize text accumulation
 
71
  if line: # filter out keep-alive new lines
72
  data = json.loads(line.decode('utf-8'))
73
  res = data['result']
74
+ response_set = res['responseSet']
75
+
76
+ if response_set:
77
+ for result in response_set:
78
+ text = result['text']
79
+ # Extract relevant information from the text
80
+ reason = re.search(r"Reason Why it Can't be Used: (.*?)\n", text).group(1)
81
+ alternative = re.search(r"Alternative: (.*?)\n", text).group(1)
82
+ notes = re.search(r"Notes: (.*?)\n", text).group(1)
 
 
 
 
 
 
 
 
 
83
 
84
+ response = f"Reason: {reason}\nAlternative: {alternative}\nNotes: {notes}"
85
+ return response
 
 
 
 
 
 
 
 
 
 
86
 
87
+ return "No relevant information found."