from langchain_community.document_loaders import UnstructuredPDFLoader
from langchain_community.embeddings import OllamaEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain.prompts import ChatPromptTemplate, PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.chat_models import ChatOllama
from langchain_core.runnables import RunnablePassthrough
from langchain.retrievers.multi_query import MultiQueryRetriever
# Librerias necesarias:
#pip3 install unstructured langchain
#pip3 install "unstructured[all-docs]"
#pip3 install chromadb
#pip3 install langchain-text-splitters
#pip3 install langchain_community
local_path = "esp32manual.pdf"
# Local PDF file uploads
if local_path:
loader = UnstructuredPDFLoader(file_path=local_path)
data = loader.load()
else:
print("Upload a PDF file")
# Preview first page
data[0].page_content
# Split and chunk
text_splitter = RecursiveCharacterTextSplitter(chunk_size=7500, chunk_overlap=100)
chunks = text_splitter.split_documents(data)
# Add to vector database
vector_db = Chroma.from_documents(
documents=chunks,
embedding=OllamaEmbeddings(model="phi3",show_progress=True),
collection_name="local-rag"
)
# LLM from Ollama
local_model = "phi3"
llm = ChatOllama(model=local_model)
QUERY_PROMPT = PromptTemplate(
input_variables=["question"],
template="""Eres un asistente de modelo de lenguaje de IA.
Su tarea principal es responder a mis preguntas.
Pregunta original: {question}""",
)
retriever = MultiQueryRetriever.from_llm(
vector_db.as_retriever(),
llm,
prompt=QUERY_PROMPT
)
# RAG prompt
template = """Las respuestas estan basadas para asistir al usuario en diferentes contxtos:
{context}
Pregunta: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Bucle para continuar preguntando hasta que el usuario escriba "adiós"
while True:
pregunta_usuario = input("Cesar: ")
if pregunta_usuario.lower() == "adios":
break
resultado = chain.invoke(pregunta_usuario)
print(resultado)
# Delete all collections in the db
vector_db.delete_collection()
print("Hasta pronto")
Deja una respuesta Cancelar la respuesta