import os import getpass # Load environment variables load_dotenv() YOUR_LLM_ENDPOINT_URL = "https://z1nsc3eoo5nxnoos.us-east-1.aws.endpoints.huggingface.cloud" from langchain_huggingface import HuggingFaceEndpoint hf_llm = HuggingFaceEndpoint( endpoint_url=f"{YOUR_LLM_ENDPOINT_URL}", task="text-generation", max_new_tokens=512, top_k=10, top_p=0.95, typical_p=0.95, temperature=0.01, repetition_penalty=1.03, ) from langchain_core.prompts import PromptTemplate RAG_PROMPT_TEMPLATE = """\ <|start_header_id|>system<|end_header_id|> You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|> <|start_header_id|>user<|end_header_id|> User Query: {query} Context: {context}<|eot_id|> <|start_header_id|>assistant<|end_header_id|> """ rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE) from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings YOUR_EMBED_MODEL_URL = "https://jt4esmqgyp7m3fk8.us-east-1.aws.endpoints.huggingface.cloud" hf_embeddings = HuggingFaceEndpointEmbeddings( model=YOUR_EMBED_MODEL_URL, task="feature-extraction", ) !git clone https://github.com/dbredvick/paul-graham-to-kindle.git from langchain_community.document_loaders import TextLoader document_loader = TextLoader("./paul-graham-to-kindle/paul_graham_essays.txt") documents = document_loader.load() from langchain_text_splitters import RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30) split_documents = text_splitter.split_documents(documents) len(split_documents) from langchain_community.vectorstores import FAISS for i in range(0, len(split_documents), 32): if i == 0: vectorstore = FAISS.from_documents(split_documents[i:i+32], hf_embeddings) continue vectorstore.add_documents(split_documents[i:i+32]) hf_retriever = vectorstore.as_retriever() from operator import itemgetter from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnablePassthrough @cl.on_chat_start async def start_chat(): """ This function will be called at the start of every user session. We will build our LCEL RAG chain here, and store it in the user session. The user session is a dictionary that is unique to each user session, and is stored in the memory of the server. """ ### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT lcel_rag_chain = {"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}| rag_prompt | hf_llm cl.user_session.set("lcel_rag_chain", lcel_rag_chain) @cl.on_message async def main(message: cl.Message): """ This function will be called whenever a user sends a message to the bot. """ chainlit_question = message.content response = lcel_rag_chain.invoke({"question": chainlit_question}) chainlit_answer = response["response"].content msg = cl.Message(content=chainlit_answer) await msg.send()