Spaces:
Running
Running
Update webchat.py
Browse files- webchat.py +37 -1
webchat.py
CHANGED
|
@@ -143,7 +143,7 @@ def create_embedding(url, collection_name):
|
|
| 143 |
return collection
|
| 144 |
|
| 145 |
|
| 146 |
-
def
|
| 147 |
# Create embeddings for the text file
|
| 148 |
collection = create_embedding(url, collection_name)
|
| 149 |
|
|
@@ -161,6 +161,42 @@ def create_prompt(url, question, collection_name):
|
|
| 161 |
|
| 162 |
return prompt
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
def main():
|
| 166 |
|
|
|
|
| 143 |
return collection
|
| 144 |
|
| 145 |
|
| 146 |
+
def create_prompt_old(url, question, collection_name):
|
| 147 |
# Create embeddings for the text file
|
| 148 |
collection = create_embedding(url, collection_name)
|
| 149 |
|
|
|
|
| 161 |
|
| 162 |
return prompt
|
| 163 |
|
| 164 |
+
def create_prompt(url, question, collection_name):
|
| 165 |
+
try:
|
| 166 |
+
# Create embeddings for the text file
|
| 167 |
+
collection = create_embedding(url, collection_name)
|
| 168 |
+
except Exception as e:
|
| 169 |
+
return f"Error creating embeddings: {e}"
|
| 170 |
+
|
| 171 |
+
try:
|
| 172 |
+
# Query relevant information
|
| 173 |
+
relevant_chunks = collection.query(
|
| 174 |
+
query_texts=[question],
|
| 175 |
+
n_results=5,
|
| 176 |
+
)
|
| 177 |
+
context = "\n\n\n".join(relevant_chunks["documents"][0])
|
| 178 |
+
except Exception as e:
|
| 179 |
+
return f"Error querying the collection: {e}"
|
| 180 |
+
|
| 181 |
+
# Create the prompt
|
| 182 |
+
prompt = (
|
| 183 |
+
"<|begin_of_text|>\n"
|
| 184 |
+
"<|start_header_id|>system<|end_header_id|>\n"
|
| 185 |
+
"You are a helpful AI assistant.\n"
|
| 186 |
+
"<|eot_id|>\n"
|
| 187 |
+
"<|start_header_id|>user<|end_header_id|>\n"
|
| 188 |
+
f"### Context:\n{context}\n\n"
|
| 189 |
+
f"### Instruction:\n"
|
| 190 |
+
f"Please answer the following question based on the above context. Your answer should be concise and directly address the question. "
|
| 191 |
+
f"If the question is unanswerable based on the given context, respond with 'unanswerable'.\n\n"
|
| 192 |
+
f"### Question:\n{question}\n"
|
| 193 |
+
"<|eot_id|>\n"
|
| 194 |
+
"<|start_header_id|>assistant<|end_header_id|>\n"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
return prompt
|
| 198 |
+
|
| 199 |
+
|
| 200 |
|
| 201 |
def main():
|
| 202 |
|