oc-chat_pdf/pruebas_open_source.py

129 lines
4.3 KiB
Python

from langchain_community.llms import HuggingFaceEndpoint
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAIEmbeddings
# model_huggingface = 'google/gemma-1.1-7b-it' # Es buena y funciona en espanol
# model_huggingface = 'tiiuae/falcon-7b-instruct'
# model_huggingface = 'mistralai/Mistral-7B-Instruct-v0.2'
# model_huggingface = "google/gemma-7b"
# model_huggingface = (
# "mistralai/Mixtral-8x7B-Instruct-v0.1" # Es muy buena y funciona en espanol
# )
huggingfacehub_api_token = "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"
model_huggingface = (
"google/gemma-1.1-2b-it" # Es buena y funciona en espanol funciona rapido
)
# Define the LLM
llm = HuggingFaceEndpoint(
repo_id=model_huggingface,
huggingfacehub_api_token=huggingfacehub_api_token,
temperature=0.5,
max_new_tokens=500,
)
# for chunk in llm.stream("Como se construye un ensayo?"):
# print(chunk, end="", flush=True)
# question = "Como se construye un ensayo?"
# output = llm.invoke(question)
# print(output)
# prompt = PromptTemplate(
# input_variables=["chat_history", "human_input"], template=template
# )
# memory = ConversationBufferMemory(memory_key="chat_history")
# llm = OpenAI()
# llm_chain = LLMChain(
# llm=llm,
# prompt=promptas st
from langchain_tools.pdf_tools import PdfLangChain
from langchain_tools.lc_tools import LangChainTools
from langchain_community.llms import HuggingFaceEndpoint
from langchain.chains.question_answering import load_qa_chain
from langchain.memory import ConversationBufferMemory
from langchain_core.prompts import PromptTemplate
# Inicializamos la clase PdfLangChain
pdfLangChain = PdfLangChain("1.TC_Malamud, Se está muriendo la democracia.pdf")
pdf_name = pdfLangChain.file_name
# Cargamos el documento PDF
docs: list = pdfLangChain.load_pdf()
# Dividimos los documentos en partes mas pequenas
docs_split: list = pdfLangChain.split_docs(docs)
# Instanciamos la clase LangChainTools que contiene herramientras LangChain
langChainTools = LangChainTools()
# Cargamos el modelo de embeddings
embedding_model = langChainTools.load_embedding_opnai()
# Creamos el vector store
docstorage = langChainTools.create_vector_strore(docs_split, pdf_name, embedding_model)
template = """Tu eres un chatbot y tienes una conversacion con un humano quien te \
hara preguntas y tu deberas responder con base al context y al chat_history. Si no \
conocer la respuesta, solo repsonde que no sabes como responder.
{context}
{chat_history}
Human: {question}
Chatbot:"""
prompt = PromptTemplate(
input_variables=["chat_history", "human_input", "context"], template=template
)
memory = ConversationBufferMemory(memory_key="chat_history", input_key="question")
chain = load_qa_chain(
llm,
chain_type="stuff",
memory=memory,
prompt=prompt,
verbose=True,
)
query = "cuales son los elementos clave del texto?"
# documents = docstorage.similarity_search(query)
# respuesta = chain.invoke(
# {"question": f"{prompt}", "input_documents": documents}, return_only_outputs=False
# )
# chain({"input_documents": documents, "human_input": query}, return_only_outputs=True)
# print(documents)
# print(respuesta["output_text"])
from langchain.chains import RetrievalQAWithSourcesChain, ConversationalRetrievalChain
chain = ConversationalRetrievalChain.from_llm(llm, docstorage.as_retriever())
print(dir(chain))
print(chain)
# template = f"Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:"
template = "Utiliza los siguientes fragmentos de contexto para responder la pregunta al final. Si no sabes la respuesta, simplemente di que no sabes, no intentes inventar una respuesta.\n\n{context}\n\nQuestion: {question}\nRespuesta útil:"
# Define el nuevo template que deseas utilizar
new_template = "Este es un nuevo template que reemplazará al anterior.\n\n{context}\n\nPregunta: {question}\nRespuesta útil:"
# Accede al prompt del LLMChain en combine_docs_chain y actualiza su template
chain.combine_docs_chain.llm_chain.prompt.template = template
print("\n\n")
# Imprime la instancia de clase para verificar que se haya actualizado el template
print(chain)