iamrobotbear commited on
Commit
1a22b02
1 Parent(s): 1f0edc8

Update query.py

Browse files
Files changed (1) hide show
  1. query.py +30 -51
query.py CHANGED
@@ -48,19 +48,7 @@ class VectaraQuery():
48
  'sentences_after': 2,
49
  'start_tag': "%START_SNIPPET%",
50
  'end_tag': "%END_SNIPPET%",
51
- },
52
- 'summary': [
53
- {
54
- 'responseLang': 'eng',
55
- 'maxSummarizedResults': 1,
56
- 'summarizerPromptName': self.prompt_name,
57
- 'promptText': prompt,
58
- 'chat': {
59
- 'store': True,
60
- 'conversationId': self.conv_id
61
- },
62
- }
63
- ]
64
  }
65
  ]
66
  }
@@ -106,60 +94,51 @@ class VectaraQuery():
106
  continue
107
  text = result['text']
108
  print(f"Processing text: {text}") # Debugging line
109
- accumulated_text += text
110
 
111
  if accumulated_text:
112
- return self.summarize_text(accumulated_text)
113
 
114
  return "No relevant information found."
115
 
116
- def summarize_text(self, text):
117
  endpoint = f"https://api.vectara.io/v1/stream-summary"
118
- prompt = f'''
119
- [
120
- {{
121
- "role": "system",
122
- "content": "You are an assistant that provides information about drink names based on a given corpus. \
123
- Format the response in the following way:\n\
124
- Reason: <reason why the name cannot be used>\n\
125
- Alternative: <alternative name>\n\
126
- Notes: <additional notes>\n\n\
127
- Example:\n\
128
- Reason: The name 'Vodka Sunrise' cannot be used because it is trademarked.\n\
129
- Alternative: Use 'Morning Delight' instead.\n\
130
- Notes: Ensure the drink contains vodka to match the alternative name."
131
- }},
132
- {{
133
- "role": "user",
134
- "content": "{text}"
135
- }}
136
- ]
137
- '''
138
  body = {
139
  'text': text,
140
  'summary': {
141
  'responseLang': 'eng',
142
  'maxSummarizedResults': 1,
143
  'summarizerPromptName': self.prompt_name,
144
- 'promptText': prompt,
145
- 'chat': {
146
- 'store': True,
147
- 'conversationId': self.conv_id
148
- },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  }
150
  }
151
- response = requests.post(endpoint, data=json.dumps(body), verify=True, headers=self.get_headers(), stream=True)
 
152
  if response.status_code != 200:
153
  print(f"Summary query failed with code {response.status_code}, reason {response.reason}, text {response.text}")
154
  return "Sorry, something went wrong. Please try again later."
155
 
156
- for line in response.iter_lines():
157
- if line: # filter out keep-alive new lines
158
- data = json.loads(line.decode('utf-8'))
159
- print(f"Received summary data chunk: {json.dumps(data, indent=2)}") # Debugging line
160
-
161
- if 'summary' in data:
162
- return data['summary']['text']
163
-
164
  return "No relevant information found."
165
-
 
48
  'sentences_after': 2,
49
  'start_tag': "%START_SNIPPET%",
50
  'end_tag': "%END_SNIPPET%",
51
+ }
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
53
  ]
54
  }
 
94
  continue
95
  text = result['text']
96
  print(f"Processing text: {text}") # Debugging line
97
+ accumulated_text += text + " "
98
 
99
  if accumulated_text:
100
+ return self.format_response_using_vectara(accumulated_text)
101
 
102
  return "No relevant information found."
103
 
104
+ def format_response_using_vectara(self, text):
105
  endpoint = f"https://api.vectara.io/v1/stream-summary"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  body = {
107
  'text': text,
108
  'summary': {
109
  'responseLang': 'eng',
110
  'maxSummarizedResults': 1,
111
  'summarizerPromptName': self.prompt_name,
112
+ 'promptText': f'''
113
+ [
114
+ {{
115
+ "role": "system",
116
+ "content": "You are an assistant that provides information about drink names based on a given corpus. \
117
+ Format the response in the following way:\n\
118
+ Reason: <reason why the name cannot be used>\n\
119
+ Alternative: <alternative name>\n\
120
+ Notes: <additional notes>\n\n\
121
+ Example:\n\
122
+ Reason: The name 'Vodka Sunrise' cannot be used because it is trademarked.\n\
123
+ Alternative: Use 'Morning Delight' instead.\n\
124
+ Notes: Ensure the drink contains vodka to match the alternative name."
125
+ }},
126
+ {{
127
+ "role": "user",
128
+ "content": "{text}"
129
+ }}
130
+ ]
131
+ '''
132
  }
133
  }
134
+ headers = self.get_headers()
135
+ response = requests.post(endpoint, data=json.dumps(body), headers=headers)
136
  if response.status_code != 200:
137
  print(f"Summary query failed with code {response.status_code}, reason {response.reason}, text {response.text}")
138
  return "Sorry, something went wrong. Please try again later."
139
 
140
+ data = response.json()
141
+ if 'summary' in data:
142
+ return data['summary']['text']
143
+
 
 
 
 
144
  return "No relevant information found."