Spaces:
Runtime error
Runtime error
File size: 1,641 Bytes
395714a a36e309 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import google.genai as genai
import chromadb
import os
from dotenv import load_dotenv
load_dotenv()
def query(question,history):
API_KEY = os.environ.get("GEMINI_API_KEY")
client_db = chromadb.PersistentClient(path="data\\vector_db")
collection = client_db.get_or_create_collection("notes")
client_ai = genai.Client(api_key=API_KEY)
#question = "নোটগুলো থেকে আমাকে কিছু গুরুত্বপূর্ণ অনুশীলন দিন।" #test by asking question
embedding_model ="gemini-embedding-001"
question_embedding = client_ai.models.embed_content(
model=embedding_model,
contents=question) #it returns a vector
embedded_question = question_embedding.embeddings[0].values
query_results = collection.query(
query_embeddings=[embedded_question],
n_results=10
)
print(query_results)
retrieved_texts =f"\n\n".join(query_results['documents'][0]) #retrieves the documents from the query results
final_response = client_ai.models.generate_content(
model="gemini-3.1-flash-lite-preview",
contents=f"""
Answer the question based on the notes below.
your main work is to find question in the notes and send to the user if asked.
also solve them if asked.
Format any math equations in LaTeX.
Notes:{retrieved_texts}
History of previous questions and answers:
{history}
User Question: {question}
"""
)
return final_response.text
print("Final Answer:",final_response.text) |