Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,78 @@
|
|
| 1 |
-
|
| 2 |
chain = ConversationalRetrievalChain.from_llm(llm=llm,retriever=retriever)
|
| 3 |
print("LLM or Vector Database not initialized")
|
| 4 |
history_langchain_format = []
|
| 5 |
prompt = PromptTemplate(template=prompt_template,
|
| 6 |
input_variables=["chat_history", 'message'])
|
| 7 |
response = chain({"question": message, "chat_history": chat_history})
|
| 8 |
|
| 9 |
answer = response['answer']
|
| 10 |
|
| 11 |
chat_history.append((message, answer))
|
| 12 |
|
| 13 |
temp = []
|
| 14 |
for input_question, bot_answer in history:
|
| 15 |
temp.append(input_question)
|
| 16 |
temp.append(bot_answer)
|
| 17 |
history_langchain_format.append(temp)
|
| 18 |
temp.clear()
|
| 19 |
temp.append(message)
|
| 20 |
temp.append(answer)
|
| 21 |
history_langchain_format.append(temp)
|
| 22 |
|
| 23 |
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
chain = ConversationalRetrievalChain.from_llm(llm=llm,retriever=retriever)
|
| 2 |
print("LLM or Vector Database not initialized")
|
| 3 |
history_langchain_format = []
|
| 4 |
prompt = PromptTemplate(template=prompt_template,
|
| 5 |
input_variables=["chat_history", 'message'])
|
| 6 |
response = chain({"question": message, "chat_history": chat_history})
|
| 7 |
|
| 8 |
answer = response['answer']
|
| 9 |
|
| 10 |
chat_history.append((message, answer))
|
| 11 |
|
| 12 |
temp = []
|
| 13 |
for input_question, bot_answer in history:
|
| 14 |
temp.append(input_question)
|
| 15 |
temp.append(bot_answer)
|
| 16 |
history_langchain_format.append(temp)
|
| 17 |
temp.clear()
|
| 18 |
temp.append(message)
|
| 19 |
temp.append(answer)
|
| 20 |
history_langchain_format.append(temp)
|
| 21 |
|
| 22 |
return answer
|
| 23 |
+
|
| 24 |
+
from langchain import PromptTemplate
|
| 25 |
+
from langchain_community.llms import LlamaCpp
|
| 26 |
+
from langchain.chains import RetrievalQA
|
| 27 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 28 |
+
from langchain.prompts import SystemMessagePromptTemplate
|
| 29 |
+
from langchain_community.embeddings import SentenceTransformerEmbeddings
|
| 30 |
+
from fastapi import FastAPI, Request, Form, Response
|
| 31 |
+
from fastapi.responses import HTMLResponse
|
| 32 |
+
from fastapi.templating import Jinja2Templates
|
| 33 |
+
from fastapi.staticfiles import StaticFiles
|
| 34 |
+
from fastapi.encoders import jsonable_encoder
|
| 35 |
+
from qdrant_client import QdrantClient
|
| 36 |
+
from langchain_community.vectorstores import Qdrant
|
| 37 |
+
import os
|
| 38 |
+
import json
|
| 39 |
+
import gradio as gr
|
| 40 |
+
import sys
|
| 41 |
+
|
| 42 |
+
#sys.path.insert(0, <envs\myenv\lib\site-packages>).
|
| 43 |
+
|
| 44 |
+
local_llm = "BioMistral-7B.Q4_K_M.gguf"
|
| 45 |
+
llm = LlamaCpp(model_path=
|
| 46 |
+
local_llm,temperature=0.3,max_tokens=2048,top_p=1,n_ctx= 2048)
|
| 47 |
+
|
| 48 |
+
prompt_template = """Use the following pieces of information to answer the user's question.
|
| 49 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 50 |
+
|
| 51 |
+
Chat History: {chat_history}
|
| 52 |
+
Question: {question}
|
| 53 |
+
|
| 54 |
+
Only return the helpful answer. Answer must be detailed and well explained.
|
| 55 |
+
Helpful answer:
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
embeddings = SentenceTransformerEmbeddings(model_name="NeuML/pubmedbert-base-embeddings")
|
| 59 |
+
|
| 60 |
+
url = "http://localhost:6333"
|
| 61 |
+
client = QdrantClient(url=url, prefer_grpc=False)
|
| 62 |
+
db = Qdrant(client=client, embeddings=embeddings, collection_name="vector_db")
|
| 63 |
+
|
| 64 |
+
retriever = db.as_retriever(search_kwargs={"k":1})
|
| 65 |
+
|
| 66 |
+
chat_history = []
|
| 67 |
+
|
| 68 |
+
# Create the custom chain
|
| 69 |
+
if llm is not None and db is not None:
|
| 70 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm,retriever=retriever)
|
| 71 |
+
else:
|
| 72 |
+
print("LLM or Vector Database not initialized")
|
| 73 |
+
|
| 74 |
+
def predict(message, history):
|
| 75 |
+
history_langchain_format = []
|
| 76 |
+
|
| 77 |
+
prompt = PromptTemplate(template=prompt_template,
|
| 78 |
+
input_variables=["chat_history", 'message'])
|
| 79 |
+
|
| 80 |
+
response = chain({"question": message, "chat_history": chat_history})
|
| 81 |
+
|
| 82 |
+
answer = response['answer']
|
| 83 |
+
|
| 84 |
+
chat_history.append((message, answer))
|
| 85 |
+
|
| 86 |
+
temp = []
|
| 87 |
+
for input_question, bot_answer in history:
|
| 88 |
+
temp.append(input_question)
|
| 89 |
+
temp.append(bot_answer)
|
| 90 |
+
history_langchain_format.append(temp)
|
| 91 |
+
temp.clear()
|
| 92 |
+
temp.append(message)
|
| 93 |
+
temp.append(answer)
|
| 94 |
+
history_langchain_format.append(temp)
|
| 95 |
+
|
| 96 |
+
return answer
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
gr.ChatInterface(predict).launch(share=True,enable_queue=True)
|
| 100 |
+
|