### Install LangSmith Python SDK Source: https://github.com/langchain-ai/chat-langchain/blob/master/LANGSMITH.md Before running evaluations or interacting with LangSmith programmatically, you need to install the LangSmith Python SDK. Use pip to add the 'langsmith' package to your development environment. ```shell pip install langsmith ``` -------------------------------- ### Instantiate Weaviate Vector Store in Python Source: https://github.com/langchain-ai/chat-langchain/blob/master/MODIFY.md This code snippet demonstrates how to instantiate a Weaviate client and vector store within the `ingest_docs` function in `backend/ingest.py` for Chat LangChain. It shows how to configure the client with a URL and API key, and then initialize the `Weaviate` vector store with the client, index name, text key, embedding, and attributes. This setup is crucial for storing document embeddings for ingestion and retrieval operations, and similar steps apply to `backend/chain.py`. ```python client = weaviate.Client( url=WEAVIATE_URL, auth_client_secret=weaviate.AuthApiKey(api_key=WEAVIATE_API_KEY), ) vectorstore = Weaviate( client=client, index_name=WEAVIATE_DOCS_INDEX_NAME, text_key="text", embedding=embedding, by_text=False, attributes=["source", "title"], ) ``` -------------------------------- ### Build Chat LangChain Frontend Locally Source: https://github.com/langchain-ai/chat-langchain/blob/master/DEPLOYMENT.md Commands to navigate into the frontend directory and build the application locally. This step ensures the frontend is ready before deployment to Vercel. ```shell cd frontend yarn yarn build ``` -------------------------------- ### GitHub Actions: Configure Environment Secrets for Indexing Source: https://github.com/langchain-ai/chat-langchain/blob/master/DEPLOYMENT.md Environment variables required for the 'Indexing' environment in GitHub Actions. These secrets are crucial for recurring data ingestion tasks and should match those in Vercel. ```APIDOC OPENAI_API_KEY= RECORD_MANAGER_DB_URL= WEAVIATE_API_KEY= WEAVIATE_INDEX_NAME=langchain WEAVIATE_URL= ``` -------------------------------- ### Vercel: Connect Frontend to LangGraph Cloud Backend API Source: https://github.com/langchain-ai/chat-langchain/blob/master/DEPLOYMENT.md Environment variables to be added in Vercel for connecting the frontend to the LangGraph Cloud backend API. This includes the base API URL, the public-facing API URL, and the LangSmith API key for monitoring. ```APIDOC API_BASE_URL: Matches your LangGraph Cloud deployment API URL NEXT_PUBLIC_API_URL: API URL that LangGraph Cloud deployment is proxied to, e.g. "https://chat.langchain.com/api" LANGCHAIN_API_KEY: LangSmith API key ``` -------------------------------- ### Python Prompt Template for Rephrasing Follow-up Questions Source: https://github.com/langchain-ai/chat-langchain/blob/master/CONCEPTS.md This Python string defines a prompt template used to rephrase a follow-up question into a standalone question. It takes the chat history and the current follow-up question as input, enabling the language model to generate a semantically clearer query for better vector store searches. ```python REPHRASE_TEMPLATE = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone Question:""" ``` -------------------------------- ### Configure LangSmith Tracing Environment Variables Source: https://github.com/langchain-ai/chat-langchain/blob/master/LANGSMITH.md To enable LangSmith tracing for your LLM application, set the LANGCHAIN_TRACING_V2 environment variable to true and provide your LangChain API key as LANGCHAIN_API_KEY. These variables are the simplest way to activate observability. ```shell export LANGCHAIN_TRACING_V2=true export LANGCHAIN_API_KEY=... ``` -------------------------------- ### LangChain Indexing API for RAG Applications Source: https://github.com/langchain-ai/chat-langchain/blob/master/CONCEPTS.md Outlines the importance of indexing in production RAG applications to prevent duplicate documents and improve performance. Describes the two main steps: Ingestion and Hashing, and how the LangChain indexing API facilitates robust document management. ```APIDOC LangChain Indexing API: Purpose: Robust document indexing for production RAG applications. Benefits: - Prevents Duplicate Results: Ensures unique documents in vector store. - Improves Performance: Only new documents require embedding generation. Process Steps: 1. Ingestion: Pulls documents to be added. 2. Hashing: Creates unique hashes for each document (with metadata) to identify new documents. 3. Insertion: Inserts new, hashed documents into the vector store (after Record Manager check). ``` -------------------------------- ### Instantiate MongoDocumentManager for Record Management in Python Source: https://github.com/langchain-ai/chat-langchain/blob/master/MODIFY.md This code snippet illustrates how to replace the default `SQLRecordManager` with a `MongoDocumentManager` for document ingestion in Chat LangChain. It demonstrates importing `MongoDocumentManager` and then instantiating it with a namespace, MongoDB URL, database name, and collection name. The `create_schema()` method is called to initialize the necessary schema for record management. ```python from langchain_community.indexes import MongoDocumentManager record_manager = MongoDocumentManager( namespace="kittens", mongodb_url="mongodb://langchain:langchain@localhost:6022/", db_name="test_db", collection_name="test_collection", ) record_manager.create_schema() ``` -------------------------------- ### Understanding Vector Stores in Chat LangChain Source: https://github.com/langchain-ai/chat-langchain/blob/master/CONCEPTS.md Explains the fundamental role of vector stores as specialized databases for high-dimensional vectors, generated by text embedding models. Details the process of vector generation, similarity searches, and context retrieval to enhance language model capabilities. ```APIDOC VectorStore: Description: Specialized database for high-dimensional vectors from text embedding models. Role in Chat LangChain: - Vector Generation: Converts textual content into unique vectors (fingerprints). - Similarity Searches: Finds documents relevant to a user query by comparing vector distances. - Context Retrieval: Provides retrieved documents to the language model for informed responses. ``` -------------------------------- ### Add New LLM Alternatives and Update Response Synthesizer Source: https://github.com/langchain-ai/chat-langchain/blob/master/MODIFY.md This snippet shows how to extend the existing `configurable_alternatives` method of the `llm` variable by adding a new LLM class declaration. It also demonstrates the necessary step of updating the `response_synthesizer` variable to include the newly added LLM alternative, ensuring it's properly integrated into the response generation pipeline. ```Python .configurable_alternatives( local_ollama=ChatCohere( model="llama2", temperature=0, ), ) ``` ```Python response_synthesizer = ( default_response_synthesizer.configurable_alternatives( ConfigurableField("llm"), default_key="openai_gpt_3_5_turbo", anthropic_claude_3_haiku=default_response_synthesizer, ... local_ollama=default_response_synthesizer, ) | StrOutputParser() ).with_config(run_name="GenerateResponse") ``` -------------------------------- ### LangChain Record Manager API Overview Source: https://github.com/langchain-ai/chat-langchain/blob/master/CONCEPTS.md Provides an interface for managing records of upserted documents before ingestion into a vector store. Details key concepts like Namespace, Keys, Group IDs, and Timestamps, enabling efficient tracking, insertion, update, deletion, and querying of records. ```APIDOC LangChain Record Manager API: Interface: Manages records of upserted documents before vector store ingestion. Capabilities: Efficiently insert, update, delete, and query records. Core Concepts: - Namespace: Logical separation of records. - Keys: Unique identifier for each record. - Group IDs: Optional grouping for batch operations. - Timestamps: Tracks last update time (updated_at) for time-range queries. Function: Tracks which documents need to be added/updated in the vector store, preventing duplication. ``` -------------------------------- ### LangChain Smith Evaluation run_on_dataset Function Source: https://github.com/langchain-ai/chat-langchain/blob/master/LANGSMITH.md The `run_on_dataset` function from `langchain.smith.evaluation.runner_utils` is used to evaluate your LLM application against a predefined dataset. It allows you to configure custom evaluation criteria, such as semantic similarity or using an LLM as a judge, and then view the results in the LangSmith dashboard. ```APIDOC Function: langchain.smith.evaluation.runner_utils.run_on_dataset Purpose: Evaluates an LLM application against a given dataset using specified criteria. Parameters: dataset: (Type not specified) The dataset to be used for evaluation. evaluation_criteria: (Type not specified) Custom criteria for evaluation (e.g., semantic similarity, LLM as a judge). Returns: (Type not specified) Evaluation results, viewable in the LangSmith dashboard. ``` -------------------------------- ### Customize LangChain Runnable Run Name with .with_config Source: https://github.com/langchain-ai/chat-langchain/blob/master/LANGSMITH.md For more detailed tracing in LangSmith, you can add a custom run name to any LangChain Runnable using the .with_config method. This helps in organizing and identifying specific operations within your traces, especially for extending default tracing behavior. ```python .with_config( run_name="CondenseQuestion", ) ``` -------------------------------- ### Replace All LLM Providers with a Single LangChain LLM Source: https://github.com/langchain-ai/chat-langchain/blob/master/MODIFY.md This snippet demonstrates how to replace the entire `llm` variable declaration in `./backend/chain.py` with a single LangChain LLM class. It simplifies the LLM configuration by removing multiple configurable alternatives and using a single chosen LLM, while maintaining the `llm` variable name to avoid breaking other parts of the endpoint. ```Python llm = ChatOpenAI( model="gpt-4o-mini-2024-07-18", streaming=True, temperature=0, ).configurable_alternatives( # This gives this field an id # When configuring the end runnable, we can then use this id to configure this field ConfigurableField(id="llm"), default_key="openai_gpt_3_5_turbo", anthropic_claude_3_haiku=ChatAnthropic( model="claude-3-haiku-20240307", max_tokens=16384, temperature=0, anthropic_api_key=os.environ.get("ANTHROPIC_API_KEY", "not_provided"), ), ... ) ``` ```Python llm = ChatYourLLM( model="model-name", streaming=True, temperature=0, ).configurable_alternatives( # This gives this field an id ConfigurableField(id="llm") ) ``` -------------------------------- ### Change Embeddings Model in Chat LangChain Source: https://github.com/langchain-ai/chat-langchain/blob/master/MODIFY.md This snippet illustrates how to modify the `get_embeddings_model` function in `./backend/ingest.py` to use a different embeddings model. It demonstrates replacing the default `OpenAIEmbeddings` with `MistralAIEmbeddings`, requiring an import and API key configuration, thereby allowing customization of the vector store's embedding process. ```Python def get_embeddings_model() -> Embeddings: return OpenAIEmbeddings(model="text-embedding-3-small", chunk_size=200) ``` ```Python from langchain_mistralai import MistralAIEmbeddings def get_embeddings_model() -> Embeddings: return MistralAIEmbeddings(mistral_api_key="your-api-key") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.