Feat: Complete chat and finder with data
This commit is contained in:
parent
125a7137d6
commit
9b3a48401f
78
app.py
78
app.py
|
@ -12,7 +12,8 @@ from langchain.document_loaders.recursive_url_loader import RecursiveUrlLoader
|
|||
from langchain.document_loaders import UnstructuredURLLoader
|
||||
from langchain.document_loaders.csv_loader import CSVLoader
|
||||
from langchain import LLMChain
|
||||
from langchain.llms import GPT4All
|
||||
#from langchain.llms import GPT4All
|
||||
from gpt4all import GPT4All
|
||||
from langchain.embeddings import GPT4AllEmbeddings
|
||||
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
||||
from langchain.callbacks.base import BaseCallbackManager
|
||||
|
@ -26,9 +27,9 @@ import time
|
|||
import re
|
||||
|
||||
def loadModels():
|
||||
#model = GPT4All("orca-mini-3b.ggmlv3.q4_0.bin")
|
||||
callback_manager = BaseCallbackManager([StreamingStdOutCallbackHandler()])
|
||||
llm = GPT4All(model="orca-mini-3b.ggmlv3.q4_0.bin",temp=0.1,streaming=True,callback_manager=callback_manager,)# verbose=True,repeat_last_n=0
|
||||
llm = GPT4All("orca-mini-3b.ggmlv3.q4_0.bin")
|
||||
|
||||
#llm = GPT4All(model="orca-mini-3b.ggmlv3.q4_0.bin",temp=0.1,streaming=True,callbacks=[StreamingStdOutCallbackHandler()])# verbose=True,repeat_last_n=0
|
||||
embeddings = GPT4AllEmbeddings()
|
||||
return llm, embeddings
|
||||
|
||||
|
@ -38,11 +39,14 @@ st2=time.time()
|
|||
|
||||
Archives=[["https://www.gob.mx/sectur","Web"],
|
||||
["https://centrohistorico.pueblacapital.gob.mx/nuestro-centro-historico/nuestro-blog/item/33-capilla-de-la-virgen-del-rosario","Web"],
|
||||
["https://capilladelrosariopuebla.com.mx/","Web"],
|
||||
#["https://capilladelrosariopuebla.com.mx/","Web"],
|
||||
#["https://www.tripadvisor.es/Tourism-g150768-Mexico-Vacations.html","Web"],
|
||||
["https://www.mexicodestinos.com/blog/destinos-mexico/","Web"],
|
||||
["https://visitmexico.com/","Web"],
|
||||
["https://www.turismomexico.es/","Web"],
|
||||
["https://es.wikipedia.org/wiki/Capilla_del_Rosario_(Puebla)","Web"],
|
||||
["https://www.mexicodesconocido.com.mx/capilla-del-rosario-puebla.html","Web"]
|
||||
|
||||
|
||||
]
|
||||
|
||||
|
@ -71,9 +75,9 @@ A=makeDb(Archives)
|
|||
|
||||
def makeFinder():
|
||||
text_splitter = RecursiveCharacterTextSplitter(
|
||||
chunk_size = 1000,
|
||||
chunk_size = 250,
|
||||
length_function=len,
|
||||
chunk_overlap=200
|
||||
chunk_overlap=50
|
||||
)
|
||||
documents = text_splitter.split_documents(A)
|
||||
try:
|
||||
|
@ -106,6 +110,23 @@ Question:
|
|||
|
||||
Respuesta:"""
|
||||
|
||||
# prompt_template = f"""
|
||||
# ### System:
|
||||
# Reponde la pregunta basado en el Contexto dado.
|
||||
# Si la pregunta no puede ser contestada usando la informacion dada
|
||||
# responder con "No poseo conocimiento sobre ese tema". Responder siempre en español.
|
||||
|
||||
# ### User:
|
||||
|
||||
# Contexto:
|
||||
# {{relevant_context}}
|
||||
|
||||
# Pregunta:
|
||||
# {{user_query}}
|
||||
|
||||
# ### Response:
|
||||
|
||||
# Respuesta:"""
|
||||
|
||||
prompt_template = PromptTemplate(
|
||||
input_variables=["relevant_context","user_query"], template=prompt_template
|
||||
|
@ -116,11 +137,11 @@ prompt_template = PromptTemplate(
|
|||
# relevant_context="Otaisa es el lugar mas lindo de mexico pero esta muy cerca de nethai que no es tan lindo pero la comida es muy buena"
|
||||
# )
|
||||
# print(prompt)
|
||||
llm_chain = LLMChain(llm=llm,prompt=prompt_template)
|
||||
#llm_chain = LLMChain(llm=llm,prompt=prompt_template)
|
||||
|
||||
|
||||
def FinderDb(query,dbs,filtred=False):
|
||||
Sal = dbs.similarity_search_with_score(query,2)
|
||||
Sal = dbs.similarity_search_with_score(query,9)
|
||||
page_content=[]
|
||||
d=[]
|
||||
if filtred:
|
||||
|
@ -128,7 +149,6 @@ def FinderDb(query,dbs,filtred=False):
|
|||
else:
|
||||
lim=9000000
|
||||
for output in Sal:
|
||||
print(output)
|
||||
if output[1]<lim:
|
||||
page_content.append(output[0].page_content)
|
||||
d.append(output[1])
|
||||
|
@ -138,21 +158,37 @@ def FinderDb(query,dbs,filtred=False):
|
|||
return "/n".join(page_content),d
|
||||
|
||||
|
||||
def QARequest(Pregunta,filtred=False):
|
||||
query = Pregunta
|
||||
def QARequest(message, history):
|
||||
text="""Hola este chatbot esta en un periodo de prueba, si encuentras alguna falla Notificala y muchas gracias por usarlo.
|
||||
Respecto a tu pregunta la respuesta es: """
|
||||
query = message
|
||||
context,d=FinderDb(query,db,filtred=True)
|
||||
#A=llm_chain.run(user_query=query,relevant_context=context)
|
||||
return context,d#A
|
||||
prompt=prompt_template.format(
|
||||
user_query=query,
|
||||
relevant_context=context
|
||||
)
|
||||
print("#############",prompt,"#############",)
|
||||
text2=""
|
||||
for i in text:
|
||||
time.sleep(0.7)
|
||||
text2=text2+i
|
||||
yield text2
|
||||
for i in llm.generate(prompt, max_tokens=200, streaming=True,temp=0.01, top_k=40, top_p=0.4):
|
||||
time.sleep(0.7)
|
||||
text2=text2+i
|
||||
yield text2
|
||||
|
||||
|
||||
#question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"
|
||||
with gr.Blocks() as demo:
|
||||
Pregunta = gr.Textbox(label="Pregunta")
|
||||
Respuesta = gr.Textbox(label="Respuesta")
|
||||
metrica=gr.Textbox(label="metrica")
|
||||
Enviar_btn = gr.Button("Responder")
|
||||
Enviar_btn.click(fn=QARequest, inputs=[Pregunta], outputs=[Respuesta,metrica], api_name="Angela")
|
||||
# with gr.Blocks() as demo:
|
||||
# Pregunta = gr.Textbox(label="Pregunta")
|
||||
# Respuesta = gr.Textbox(label="Respuesta")
|
||||
# metrica=gr.Textbox(label="metrica")
|
||||
# Enviar_btn = gr.Button("Responder")
|
||||
# Enviar_btn.click(fn=QARequest, inputs=[Pregunta], outputs=[Respuesta], api_name="Angela")
|
||||
gr.ChatInterface(QARequest).queue().launch()
|
||||
|
||||
demo.launch() #
|
||||
|
||||
#demo.queue().launch() #
|
||||
#print(A,time.time()-st,time.time()-st2)
|
||||
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
import time
|
||||
import gradio as gr
|
||||
from langchain.llms import GPT4All
|
||||
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain.chains import LLMChain
|
||||
from gpt4all import GPT4All
|
||||
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
||||
# Callbacks support token-wise streaming
|
||||
model = GPT4All("orca-mini-3b.ggmlv3.q4_0.bin")
|
||||
|
||||
template = """Question: {question}
|
||||
|
||||
Answer: Let's think step by step."""
|
||||
|
||||
prompt = PromptTemplate(template=template, input_variables=["question"])
|
||||
|
||||
prompt_template = """
|
||||
### System:
|
||||
Answer the question based on the input below.
|
||||
If the question cannot be answered using the information
|
||||
provided answer with "I don't know". Ever answer in spanish.
|
||||
|
||||
### Input:
|
||||
%s
|
||||
|
||||
### User:
|
||||
Question:
|
||||
%s
|
||||
|
||||
|
||||
|
||||
|
||||
### Response:
|
||||
Respuesta:"""
|
||||
|
||||
|
||||
|
||||
|
||||
def FinderDb(query,dbs,filtred=False):
|
||||
Sal = dbs.similarity_search_with_score(query,9)
|
||||
page_content=[]
|
||||
d=[]
|
||||
if filtred:
|
||||
lim=1.5
|
||||
else:
|
||||
lim=9000000
|
||||
for output in Sal:
|
||||
if output[1]<lim:
|
||||
page_content.append(output[0].page_content)
|
||||
d.append(output[1])
|
||||
if len(page_content)<1:
|
||||
return None,None
|
||||
|
||||
return "/n".join(page_content),d
|
||||
|
||||
def slow_echo(message, history):
|
||||
text=""
|
||||
context,d=FinderDb(query,db,filtred=True)
|
||||
query=prompt_template%("Otaisa es la cuidad mas linda de parita",message)
|
||||
print(query)
|
||||
for i in model.generate(query, max_tokens=200, streaming=True,temp=0.1, top_k=40, top_p=0.4):
|
||||
time.sleep(0.03)
|
||||
text=text+i
|
||||
yield text
|
||||
|
||||
gr.ChatInterface(slow_echo).queue().launch()
|
||||
|
Loading…
Reference in New Issue