Spaces:
Sleeping
Sleeping
Update query.py
Browse files
query.py
CHANGED
@@ -23,7 +23,11 @@ class VectaraQuery():
|
|
23 |
Format the response in the following way:\n\
|
24 |
Reason: <reason why the name cannot be used>\n\
|
25 |
Alternative: <alternative name>\n\
|
26 |
-
Notes: <additional notes
|
|
|
|
|
|
|
|
|
27 |
}},
|
28 |
{{
|
29 |
"role": "user",
|
@@ -44,7 +48,19 @@ class VectaraQuery():
|
|
44 |
'sentences_after': 2,
|
45 |
'start_tag': "%START_SNIPPET%",
|
46 |
'end_tag': "%END_SNIPPET%",
|
47 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
}
|
49 |
]
|
50 |
}
|
@@ -90,30 +106,60 @@ class VectaraQuery():
|
|
90 |
continue
|
91 |
text = result['text']
|
92 |
print(f"Processing text: {text}") # Debugging line
|
|
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
alternative = self.extract_text(text, "Alternative:", "Notes:")
|
97 |
-
notes = self.extract_text(text, "Notes:", "")
|
98 |
-
|
99 |
-
response = f"Reason: {reason}\nAlternative: {alternative}\nNotes: {notes}"
|
100 |
-
print(f"Generated response: {response}") # Debugging line
|
101 |
-
return response
|
102 |
|
103 |
return "No relevant information found."
|
104 |
|
105 |
-
def
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
|
|
23 |
Format the response in the following way:\n\
|
24 |
Reason: <reason why the name cannot be used>\n\
|
25 |
Alternative: <alternative name>\n\
|
26 |
+
Notes: <additional notes>\n\n\
|
27 |
+
Example:\n\
|
28 |
+
Reason: The name 'Vodka Sunrise' cannot be used because it is trademarked.\n\
|
29 |
+
Alternative: Use 'Morning Delight' instead.\n\
|
30 |
+
Notes: Ensure the drink contains vodka to match the alternative name."
|
31 |
}},
|
32 |
{{
|
33 |
"role": "user",
|
|
|
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 |
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 |
|