|
import ollama
|
|
from typing import List, Optional, Dict
|
|
|
|
N_RESULTS = 20
|
|
def generateResponse(query_text: str, collection: Optional[Dict] = None) -> str:
|
|
"""Generates a response to a query based on the Chroma database collection.
|
|
|
|
Args:
|
|
query_text (str): The query to search for.
|
|
collection (Optional[Dict]): The Chroma collection object to use for querying.
|
|
|
|
Returns:
|
|
str: The response generated from the query.
|
|
"""
|
|
if collection is None:
|
|
raise ValueError("Collection is not provided")
|
|
|
|
|
|
query_results = collection.query(
|
|
query_texts=query_text,
|
|
n_results=N_RESULTS,
|
|
)
|
|
|
|
|
|
best_recommendation = query_results.get('documents', [])
|
|
|
|
|
|
prompt_template = f"""Use the following pieces of context to answer the question at the end. If you don't know the answer, say so.
|
|
|
|
This is the piece of context necessary: {best_recommendation}
|
|
|
|
Cross-reference all pieces of context to define variables and other unknown entities. Calculate mathematical values based on provided matching variables. Remember previous responses if asked a follow-up question.
|
|
|
|
Question: {query_text}
|
|
|
|
"""
|
|
response = ollama.generate(model="llama3", prompt=prompt_template)
|
|
final_response = response.get('response', 'No response generated')
|
|
return final_response
|
|
|
|
|
|
|
|
|
|
|