Spaces:
Runtime error
Runtime error
from fastapi import FastAPI | |
import argparse | |
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 | |
CHROMA_PATH = "chroma" | |
PROMPT_TEMPLATE = """ | |
Answer the question based only on the following context: | |
{context} | |
--- | |
Answer the question based on the above context: {question} | |
""" | |
embedding_function = get_embedding_function() | |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function) | |
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) | |
model = LlamaCpp( | |
model_path="/content/drive/MyDrive/mistral-7b-instruct-v0.2.Q4_K_M.gguf", | |
temperature=0.75, | |
max_tokens=2000, | |
top_p=1, | |
callback_manager=callback_manager, | |
verbose=True, # Verbose is required to pass to the callback manager | |
) | |
app = FastAPI() | |
async def getAnswer(): | |
query_text = "What's up?" | |
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 response_text |