Update app.py
Browse files
app.py
CHANGED
@@ -148,43 +148,62 @@ def create_vector_db(final_items):
|
|
148 |
from llama_cpp import Llama
|
149 |
|
150 |
llm = Llama.from_pretrained(
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
154 |
for item in final_items:
|
155 |
-
|
156 |
-
|
157 |
-
1. Provide a detailed summary using a limited number of words
|
158 |
-
2. Maintain all original values and include any mathematical expressions or values in full.
|
159 |
-
3. Ensure that all variable names and their values are clearly presented.
|
160 |
-
4. Write the summary in paragraph format, putting an emphasis on clarity and completeness.
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
output = llm(
|
166 |
-
prompt,
|
167 |
-
temperature = 0.1,
|
168 |
-
top_p = 0.9,
|
169 |
-
top_k = 20,
|
170 |
-
stream=False,
|
171 |
-
)
|
172 |
-
|
173 |
-
final_result = output["choices"][0]["text"]
|
174 |
-
documents.append(final_result)
|
175 |
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
db.add(
|
178 |
-
documents=
|
179 |
-
ids=
|
180 |
)
|
181 |
|
182 |
return db
|
183 |
|
|
|
184 |
def generate_response(db, query_text, previous_context):
|
185 |
query_results = db.query(
|
186 |
query_texts=query_text,
|
187 |
-
n_results=
|
188 |
)
|
189 |
|
190 |
if not query_results.get('documents'):
|
|
|
148 |
from llama_cpp import Llama
|
149 |
|
150 |
llm = Llama.from_pretrained(
|
151 |
+
repo_id="xzlinuxmodels/ollama3.1",
|
152 |
+
filename="unsloth.BF16.gguf",
|
153 |
+
)
|
154 |
+
|
155 |
+
# Placeholder for final results to add to the database
|
156 |
+
documents_to_add = []
|
157 |
+
ids_to_add = []
|
158 |
+
|
159 |
for item in final_items:
|
160 |
+
# Generate the ID from the first 20 characters of the item
|
161 |
+
item_id = f"id_{item[:45].replace(' ', '_')}" # Use first 20 characters, replace spaces for a valid ID
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
# Check if the ID already exists in the database
|
164 |
+
existing_documents = db.get(ids=[item_id])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
if not existing_documents: # If the ID does not exist
|
167 |
+
# Generate the LLM prompt and output
|
168 |
+
prompt = f"""
|
169 |
+
Summarize the following segment of Antimony in a clear and concise manner:
|
170 |
+
1. Provide a detailed summary using a limited number of words
|
171 |
+
2. Maintain all original values and include any mathematical expressions or values in full.
|
172 |
+
3. Ensure that all variable names and their values are clearly presented.
|
173 |
+
4. Write the summary in paragraph format, putting an emphasis on clarity and completeness.
|
174 |
+
|
175 |
+
Here is the antimony segment to summarize: {item}
|
176 |
+
"""
|
177 |
+
|
178 |
+
output = llm(
|
179 |
+
prompt,
|
180 |
+
temperature=0.1,
|
181 |
+
top_p=0.9,
|
182 |
+
top_k=20,
|
183 |
+
stream=False,
|
184 |
+
)
|
185 |
+
|
186 |
+
# Extract the generated summary text
|
187 |
+
final_result = output["choices"][0]["text"]
|
188 |
+
|
189 |
+
# Add the result to documents and its corresponding ID to the lists
|
190 |
+
documents_to_add.append(final_result)
|
191 |
+
ids_to_add.append(item_id)
|
192 |
+
|
193 |
+
# Add the new documents to the vector database, if there are any
|
194 |
+
if documents_to_add:
|
195 |
db.add(
|
196 |
+
documents=documents_to_add,
|
197 |
+
ids=ids_to_add
|
198 |
)
|
199 |
|
200 |
return db
|
201 |
|
202 |
+
|
203 |
def generate_response(db, query_text, previous_context):
|
204 |
query_results = db.query(
|
205 |
query_texts=query_text,
|
206 |
+
n_results=5,
|
207 |
)
|
208 |
|
209 |
if not query_results.get('documents'):
|