from fastapi import FastAPI from langchain.vectorstores.chroma import Chroma from langchain.prompts import ChatPromptTemplate from langchain_community.llms import LlamaCpp from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler from get_embedding_function import get_embedding_function from populate_database import populate_database CHROMA_PATH = "chroma" PROMPT_TEMPLATE = """ Answer the question based only on the following context: {context} --- Answer the question based on the above context: {question} """ populate_database() embedding_function = get_embedding_function() db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function) callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) model = LlamaCpp( model_path="mistral-7b-instruct-v0.2.Q4_K_M.gguf", temperature=0.1, max_tokens=2000, top_p=1, callback_manager=callback_manager, verbose=True, # Verbose is required to pass to the callback manager ) app = FastAPI() @app.get("/query") async def getAnswer(): query_text = "How many kids you have?" results = db.similarity_search_with_score(query_text, k=5) context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results]) prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE) prompt = prompt_template.format(context=context_text, question=query_text) response_text = model.invoke(prompt) sources = [doc.metadata.get("id", None) for doc, _score in results] formatted_response = f"Response: {response_text}\nSources: {sources}" return formatted_response