68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
# from langchain_tools.lc_tools import LangChainTools
|
|
from langchain_community.llms import HuggingFaceEndpoint
|
|
|
|
|
|
# Instanciamos la clase LangChainTools que contiene herramientras LangChain
|
|
# langChainTools = LangChainTools()
|
|
|
|
# model_huggingface = "google/gemma-1.1-7b-it" # Es buena y funciona en espanol
|
|
# model_huggingface = (
|
|
# "google/gemma-1.1-2b-it" # Es buena y funciona en espanol funciona rapido
|
|
# )
|
|
# model_huggingface = 'tiiuae/falcon-7b-instruct'
|
|
model_huggingface = "mistralai/Mistral-7B-Instruct-v0.2"
|
|
huggingfacehub_api_token = "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"
|
|
|
|
# model_huggingface = "mistralai/Mixtral-8x22B-Instruct-v0.1" # 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,
|
|
) # Cargamos el modelo LLM desde LangChainllm llm = langChainTools.load_llm_open_source()
|
|
# respuesta = llm.invoke("Cual es el sentido de la vida?")
|
|
|
|
# print(respuesta)
|
|
|
|
import streamlit as st
|
|
from chats.streamlit_tools import import_file # ,clear_cache
|
|
from streamlit_extras.add_vertical_space import add_vertical_space
|
|
from langchain_tools.pdf_tools import PdfLangChain
|
|
from langchain_tools.lc_tools import LangChainTools
|
|
from chats.chat_tools import MessageManager
|
|
|
|
|
|
pdf_name = "1.TC_Malamud, Se está muriendo la democracia.pdf"
|
|
pdfLangChain = PdfLangChain(pdf_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()
|
|
|
|
# Cargamos el modelo de embeddings
|
|
embedding_model = langChainTools.load_embedding_hf()
|
|
|
|
# Creamos el vector store
|
|
docstorage = langChainTools.create_vector_strore(docs_split, pdf_name, embedding_model)
|
|
|
|
# Cargamos el modelo LLM desde LangChain
|
|
llm = langChainTools.load_llm_open_source()
|
|
|
|
# Creamos la cadena que integra Vectorstroe, el LLM para hacer consultas.Para este caso la cadena tene el parametro de memoria.
|
|
qa = langChainTools.define_retrieval_qa_memory(
|
|
llm, docstorage, pdf_name, embedding_model
|
|
)
|
|
# qa.question_generator.prompt.template = "Dado el siguiente diálogo y una pregunta de seguimiento, reformula la pregunta de seguimiento para que sea una pregunta independiente, en su idioma original.\n\nHistorial del chat:\n{chat_history}\nPregunta de seguimiento: {question}\nPregunta independiente:"
|
|
|
|
print(qa)
|