avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2696

Abstract

s="hljs-string">"lxml"</span>, bs_kwargs={ <span class="hljs-string">"parse_only"</span>: SoupStrainer( name=(<span class="hljs-string">"article"</span>, <span class="hljs-string">"title"</span>, <span class="hljs-string">"html"</span>, <span class="hljs-string">"lang"</span>, <span class="hljs-string">"content"</span>) ), }, <span class="hljs-attribute">meta_function</span>=metadata_extractor, ).load()</pre></div><ol><li>Transform: The <code>RecursiveCharacterTextSplitter</code> is used to partition the content into equally-sized chunks:</li></ol><div id="57fb"><pre>transformed_docs = RecursiveCharacterTextSplitter( <span class="hljs-attribute">chunk_size</span>=4000, <span class="hljs-attribute">chunk_overlap</span>=200, ).split_documents(docs + api_ref)</pre></div><ol><li>Embed + Store: We use OpenAI’s embeddings to represent each chunk as a vector in the Weaviate vector store:</li></ol><div id="4856"><pre>client = weaviate.Client( <span class="hljs-attribute">url</span>=WEAVIATE_URL, <span class="hljs-attribute">auth_client_secret</span>=weaviate.AuthApiKey(api_key=WEAVIATE_API_KEY), ) embedding = OpenAIEmbeddings(<span class="hljs-attribute">chunk_size</span>=200) vectorstore = Weaviate( <span class="hljs-attribute">client</span>=client, <span class="hljs-attribute">index_name</span>=WEAVIATE_DOCS_INDEX_NAME, <span class="hljs-attribute">text_key</span>=<span class="hljs-string">"text"</span>, <span class="hljs-attribute">embedding</span>=embedding, <span class="hljs-attribute">by_text</span>=<span class="hljs-literal">False</span>, attributes=[<span class="hljs-string">"source"</span>, <span class="hljs-string">"title"</span>], )</pre></div><ol><li>Indexing + Record Management: We use the LangChain Indexing API and a <code>SQLRecordManager</code> to track writes to the vector store and handle deduplication and cleanup of documents.</li></ol><div id="1ef1"><pre>record_manager = SQLRecordManager( f<span class="hljs-string">"weaviate/{WEAVIATE_DOCS_INDEX_NAME}"</span>, <span class="hljs-attribute">db_url</span>=RECORD_MANAGER_DB_URL ) record_manager.create_schema() indexing_stats = index( transformed_docs, record_manager, vectorstore, <span class="hljs-attribute">cleanup</span>=<span class="hljs-string">"full"</span>, <span class="hljs-attribute">source_id_key</span>=<span class="hljs-string">"source"</span>, )</pre></div><h2 id="c812">Continuous Ingestion</h2><p id="083a">To keep the chatbot up-to-date with new LangChain releases and documentation, we add a scheduled Github Action to the repo that runs the ingestion pipeline daily.</p><h2 id

Options

="66c7">Question-Answering</h2><p id="7969">The question-answering chain involves using a prompt to rephrase the question, retrieving relevant documents, and then passing the context to a model to generate a response. Here’s an example of the chain:</p><div id="5c43"><pre>condense_question_chain = ( PromptTemplate.from_template(REPHRASE_TEMPLATE) <span class="hljs-string">| llm</span> <span class="hljs-string">| StrOutputParser()</span> ).with_config( run_name=<span class="hljs-string">"CondenseQuestion"</span>, ) retriever_chain = condense_question_chain <span class="hljs-string">| retriever</span></pre></div><div id="62df"><pre>_context = RunnableMap( { <span class="hljs-string">"context"</span>: retriever_chain | format_docs, <span class="hljs-string">"question"</span>: itemgetter(<span class="hljs-string">"question"</span>), <span class="hljs-string">"chat_history"</span>: itemgetter(<span class="hljs-string">"chat_history"</span>), } ).with_config(<span class="hljs-attribute">run_name</span>=<span class="hljs-string">"RetrieveDocs"</span>) prompt = ChatPromptTemplate.from_messages( [ (<span class="hljs-string">"system"</span>, RESPONSE_TEMPLATE), MessagesPlaceholder(<span class="hljs-attribute">variable_name</span>=<span class="hljs-string">"chat_history"</span>), (<span class="hljs-string">"human"</span>, <span class="hljs-string">"{question}"</span>), ] )

response_synthesizer = (prompt | llm | StrOutputParser()).with_config( <span class="hljs-attribute">run_name</span>=<span class="hljs-string">"GenerateResponse"</span>, ) answer_chain = _context | response_synthesizer</pre></div><h2 id="6335">Evaluation</h2><p id="2c8d">LangSmith is used to benchmark each version of the chatbot. The evaluation process involves interacting with the app, viewing the trace for bad examples, assigning blame to the different components, and making changes to the bot’s prompt, retriever, and overall architecture.</p><div id="1815" class="link-block"> <a href="https://readmedium.com/langchain-what-is-weblangchain-61ec533dfc3d"> <div> <div> <h2>LANGCHAIN — What Is Weblangchain?</h2> <div><h3>The advance of technology is based on making it fit in so that you don’t really even notice it, so it’s part of…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nu7ZXSdSXeo6aCLEJYoZpg.jpeg)"></div> </div> </div> </a> </div></article></body>

LANGCHAIN — What Is The Purpose of Building Chat Langchain 2?

It’s not that we use technology, we live technology. — Godfrey Reggio

Building Chat LangChain 2 serves the purpose of demonstrating the power of LangChain in simplifying the creation of Large Language Model (LLM) applications. In this tutorial, we will walk through the process of building a chatbot called Chat LangChain that answers questions about LangChain by indexing and searching through the Python docs and API reference. We will cover how to use the LangChain Expression Language (LCEL) to define a RAG chain, evaluate an LLM application, deploy a LangChain application, and monitor its performance.

Architecture

Ingestion

To perform RAG, we need to create an index over some source of information about LangChain that can be queried at runtime. Ingestion refers to the process of loading, transforming, and indexing the relevant data sources. The ingestion pipeline includes the following steps:

  1. Load: We use SitemapLoader and RecursiveURLLoader to scrape the Python docs and API Reference. This approach is chosen for its ability to handle autogenerated content and its generalizability. Below is an example of loading the Python docs using SitemapLoader:
docs = SitemapLoader(
    "https://python.langchain.com/sitemap.xml",
    filter_urls=["https://python.langchain.com/"],
    parsing_function=langchain_docs_extractor,
    default_parser="lxml",
    bs_kwargs={
        "parse_only": SoupStrainer(
            name=("article", "title", "html", "lang", "content")
        ),
    },
    meta_function=metadata_extractor,
).load()
  1. Transform: The RecursiveCharacterTextSplitter is used to partition the content into equally-sized chunks:
transformed_docs = RecursiveCharacterTextSplitter(
    chunk_size=4000, 
    chunk_overlap=200,
).split_documents(docs + api_ref)
  1. Embed + Store: We use OpenAI’s embeddings to represent each chunk as a vector in the Weaviate vector store:
client = weaviate.Client(
    url=WEAVIATE_URL,
    auth_client_secret=weaviate.AuthApiKey(api_key=WEAVIATE_API_KEY),
)
embedding = OpenAIEmbeddings(chunk_size=200)
vectorstore = Weaviate(
    client=client,
    index_name=WEAVIATE_DOCS_INDEX_NAME,
    text_key="text",
    embedding=embedding,
    by_text=False,
    attributes=["source", "title"],
)
  1. Indexing + Record Management: We use the LangChain Indexing API and a SQLRecordManager to track writes to the vector store and handle deduplication and cleanup of documents.
record_manager = SQLRecordManager(
    f"weaviate/{WEAVIATE_DOCS_INDEX_NAME}", db_url=RECORD_MANAGER_DB_URL
)
record_manager.create_schema()
indexing_stats = index(
    transformed_docs,
    record_manager,
    vectorstore,
    cleanup="full",
    source_id_key="source",
)

Continuous Ingestion

To keep the chatbot up-to-date with new LangChain releases and documentation, we add a scheduled Github Action to the repo that runs the ingestion pipeline daily.

Question-Answering

The question-answering chain involves using a prompt to rephrase the question, retrieving relevant documents, and then passing the context to a model to generate a response. Here’s an example of the chain:

condense_question_chain = (
    PromptTemplate.from_template(REPHRASE_TEMPLATE)
    | llm
    | StrOutputParser()
).with_config(
    run_name="CondenseQuestion",
)
retriever_chain = condense_question_chain | retriever
_context = RunnableMap(
    {
        "context": retriever_chain | format_docs,
        "question": itemgetter("question"),
        "chat_history": itemgetter("chat_history"),
    }
).with_config(run_name="RetrieveDocs")
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", RESPONSE_TEMPLATE),
        MessagesPlaceholder(variable_name="chat_history"),
        ("human", "{question}"),
    ]
)

response_synthesizer = (prompt | llm | StrOutputParser()).with_config(
    run_name="GenerateResponse",
)
answer_chain = _context | response_synthesizer

Evaluation

LangSmith is used to benchmark each version of the chatbot. The evaluation process involves interacting with the app, viewing the trace for bad examples, assigning blame to the different components, and making changes to the bot’s prompt, retriever, and overall architecture.

Chat
Langchain
Purpose
ChatGPT
Building
Recommended from ReadMedium