LangChainHub

repository·master·Indexed 25 days ago

https://github.com/hwchase17/langchain-hub

A central repository for sharing and discovering high-quality LangChain primitives, including prompts, chains, and agents. It provides pre-configured artifacts such as LLMMathChain, LLMBashChain, and LLMCheckerChain, as well as specialized prompts for API responses, conversation memory, and Program-Aided Language models (PAL). Users can load these artifacts using the `lc://` URI scheme via `load_chain` or `load_prompt` and export their own chains in JSON or YAML format.

Tokens
7.1K
Snippets
26
Records
45
Agent score
86%

What's inside LangChainHub

  1. Use LLMCheckerChain to improve factual answer accuracy

    master

    The LLMCheckerChain is a specialized chain designed to improve the quality of answers to factual questions through a multi-step verification process.

    When you run this chain, it performs the following workflow:

    1. Drafting: Generates an initial draft answer to the question.
    2. Assumption Extraction: Asks the model to list the assumptions underlying the draft statement.
    3. Verification: Asks the model to determine if each assertion is true or false, providing explanations for false assertions.
    4. Revision: Prompts the model to revise the original answer based on the findings from the verification step.
  2. Use API URL Prompts to construct API queries

    master

    API URL Prompts are specialized prompts designed to construct an API URL to answer a specific question. These prompts require two specific inputs:

    1. api_docs: The documentation for the target API.
    2. question: The specific question being asked of the API.

    You can use these prompts within an APIChain by loading them via the lc:// URI scheme.

    from langchain.prompts import load_prompt
    from langchain.chains import APIChain
    
    llm = ...
    api_docs = ...
    prompt = load_prompt('lc://prompts/api/api_url/<file-name>')
    chain = APIChain.from_llm_and_api_docs(llm, api_docs, api_url_prompt=prompt)
  3. Load prompts from LangChainHub

    master

    You can load any prompt from the LangChainHub by using the lc:// prefix followed by the path relative to the langchain-hub repository.

    For example, if a prompt is located at langchain-hub/prompts/qa/stuff/basic/prompt.yaml, you should use the path lc://prompts/qa/stuff/basic/prompt.yaml with the load_prompt function.

    from langchain.prompts import load_prompt
    
    prompt = load_prompt('lc://prompts/qa/stuff/basic/prompt.yaml')
  4. Use QA with Sources Refine Prompts

    master

    These prompts are designed for question-answering tasks where the chain uses the refine method to iteratively improve an answer using multiple pieces of context.

    Expected Inputs

    The prompt expects the following input variables:

    1. question: The original question to be answered.
    2. existing_answer: The answer generated from previous documents.
    3. context_str: The new piece of context used to refine the existing answer.
    from langchain.prompts import load_prompt
    from langchain.chains.qa_with_sources import load_qa_with_sources_chain
    
    llm = ...
    prompt = load_prompt('lc://prompts/qa_with_sources/refine/<file-name>')
    chain = load_qa_with_sources_chain(llm, chain_type="refine", refine_prompt=prompt)
  5. Use the LLM Math Chain prompt

    master

    The llm_math prompt is designed to answer mathematical questions by optionally generating iPython syntax. This allows the model to execute code to solve complex math problems more accurately.

    Expected Input:

    • question: The user's mathematical question string.
    from langchain.prompts import load_prompt
    from langchain.chains import LLMMathChain
    
    llm = ... # Initialize your LLM
    prompt = load_prompt('lc://prompts/llm_math/<file-name>')
    chain = LLMMathChain(llm=llm, prompt=prompt)
  6. Use the QA with Sources Map Reduce Reduce Prompt

    master

    This prompt is designed for the 'reduce' step of a Map Reduce QA chain. It takes the individual summaries generated during the 'map' phase and reduces them into a final answer that includes relevant sources.

    Expected Inputs:

    • summaries: The summaries generated during the map step.
    • question: The original question being answered.
    from langchain.prompts import load_prompt
    from langchain.chains.qa_with_sources import load_qa_with_sources_chain
    
    llm = ...
    # Replace <file-name> with the specific prompt file name
    prompt = load_prompt('lc://prompts/qa_with_sources/map_reduce/reduce/<file-name>')
    chain = load_qa_with_sources_chain(llm, chain_type="map_reduce", combine_prompt=prompt)
  7. Upload prompts to LangChainHub

    master

    To upload a prompt to the LangChainHub, you must provide two files:

    1. The prompt file: Supported formats are json, yaml, and python.
    2. An associated README file: This provides a description and usage patterns. If you are uploading to an existing directory, a README should already exist.

    Organization Rules:

    • Prompts must be organized by use case within the directory structure.
    • When adding to an existing folder, ensure the prompt serves the same use case and has the same inputs as existing prompts in that folder.