langchain-contextual: update provider documentation and add reranker documentation (#30415)

Hi @ccurme!

Thanks so much for helping with getting the Contextual documentation
merged last time. We added the reranker to our provider's documentation!
Please let me know if there's any issues with it! Would love to also
work with your team on an announcement for this! 🙏

Thank you for contributing to LangChain!

- [x] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [x] **PR message**: ***Delete this entire checklist*** and replace
with
- **Description:** updates contextual provider documentation to include
information about our reranker, also includes documentation for
contextual's reranker in the retrievers section
    - **Twitter handle:** https://x.com/ContextualAI/highlights


docs have been added


- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.

---------

Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
Jonathan Feng 2025-03-22 15:09:09 -07:00 committed by GitHub
parent d867afff1c
commit 3848a1371d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 297 additions and 6 deletions

View File

@ -6,22 +6,54 @@
"source": [
"# Contextual AI\n",
"\n",
"Contextual AI is a platform that offers state-of-the-art Retrieval-Augmented Generation (RAG) technology for enterprise applications. Our platformant models helps innovative teams build production-ready AI applications that can process millions of pages of documents with exceptional accuracy.\n",
"Contextual AI provides state-of-the-art RAG components designed specifically for accurate and reliable enterprise AI applications. Our LangChain integration exposes standalone API endpoints for our specialized models:\n",
"\n",
"- Grounded Language Model (GLM): The world's most grounded language model, engineered to minimize hallucinations by prioritizing faithfulness to retrieved knowledge. GLM delivers exceptional factual accuracy with inline attributions, making it ideal for enterprise RAG and agentic applications where reliability is critical.\n",
"\n",
"- Instruction-Following Reranker: The first reranker that follows custom instructions to intelligently prioritize documents based on specific criteria like recency, source, or document type. Outperforming competitors on industry benchmarks, our reranker resolves conflicting information challenges in enterprise knowledge bases.\n",
"\n",
"Founded by the inventors of RAG technology, Contextual AI's specialized components help innovative teams accelerate the development of production-ready RAG agents that deliver responses with exceptional accuracy.\n",
"\n",
"## Grounded Language Model (GLM)\n",
"\n",
"The Grounded Language Model (GLM) is specifically engineered to minimize hallucinations in RAG and agentic applications. The GLM achieves:\n",
"The Grounded Language Model (GLM) is engineered specifically to minimize hallucinations in enterprise RAG and agentic applications. The GLM delivers:\n",
"\n",
"- State-of-the-art performance on the FACTS benchmark\n",
"- Responses strictly grounded in provided knowledge sources\n",
"- Strong performance with 88% factual accuracy on the FACTS benchmark ([See benchmark results](https://venturebeat.com/ai/contextual-ais-new-ai-model-crushes-gpt-4o-in-accuracy-heres-why-it-matters/))\n",
"- Responses strictly grounded in provided knowledge sources with inline attributions ([Read product details](https://contextual.ai/blog/introducing-grounded-language-model/))\n",
"- Precise source citations integrated directly within generated responses\n",
"- Prioritization of retrieved context over parametric knowledge ([View technical overview](https://contextual.ai/blog/platform-benchmarks-2025/))\n",
"- Clear acknowledgment of uncertainty when information is unavailable\n",
"\n",
"GLM serves as a drop-in replacement for general-purpose LLMs in RAG pipelines, dramatically improving reliability for mission-critical enterprise applications.\n",
"\n",
"## Instruction-Following Reranker\n",
"\n",
"The world's first Instruction-Following Reranker revolutionizes document ranking with unprecedented control and accuracy. Key capabilities include:\n",
"\n",
"- Natural language instructions to prioritize documents based on recency, source, metadata, and more ([See how it works](https://contextual.ai/blog/introducing-instruction-following-reranker/))\n",
"- Superior performance on the BEIR benchmark with a score of 61.2, outperforming competitors by significant margins ([View benchmark data](https://contextual.ai/blog/platform-benchmarks-2025/))\n",
"- Intelligent resolution of conflicting information from multiple knowledge sources\n",
"- Seamless integration as a drop-in replacement for existing rerankers\n",
"- Dynamic control over document ranking through natural language commands\n",
"\n",
"The reranker excels at handling enterprise knowledge bases with potentially contradictory information, allowing you to specify exactly which sources should take precedence in various scenarios.\n",
"\n",
"## Using Contextual AI with LangChain\n",
"\n",
"See details [here](/docs/integrations/chat/contextual).\n",
"\n",
"This integration allows you to easily incorporate Contextual AI's GLM into your LangChain workflows. Whether you're building applications for regulated industries or security-conscious environments, Contextual AI provides the grounded and reliable responses your use cases demand.\n",
"This integration allows you to easily incorporate Contextual AI's GLM and Instruction-Following Reranker into your LangChain workflows. The GLM ensures your applications deliver strictly grounded responses, while the reranker significantly improves retrieval quality by intelligently prioritizing the most relevant documents.\n",
"\n",
"Get started with a free trial today and experience the most grounded language model for enterprise AI applications."
"Whether you're building applications for regulated industries or security-conscious environments, Contextual AI provides the accuracy, control, and reliability your enterprise use cases demand.\n",
"\n",
"Get started with a free trial today and experience the most grounded language model and instruction-following reranker for enterprise AI applications."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Grounded Language Model"
]
},
{
@ -43,6 +75,7 @@
}
],
"source": [
"# Integrating the Grounded Language Model\n",
"import getpass\n",
"import os\n",
"\n",
@ -81,6 +114,74 @@
"\n",
"print(ai_msg.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Instruction-Following Reranker"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"from langchain_contextual import ContextualRerank\n",
"\n",
"if not os.getenv(\"CONTEXTUAL_AI_API_KEY\"):\n",
" os.environ[\"CONTEXTUAL_AI_API_KEY\"] = getpass.getpass(\n",
" \"Enter your Contextual API key: \"\n",
" )\n",
"\n",
"\n",
"api_key = \"\"\n",
"model = \"ctxl-rerank-en-v1-instruct\"\n",
"\n",
"compressor = ContextualRerank(\n",
" model=model,\n",
" api_key=api_key,\n",
")\n",
"\n",
"from langchain_core.documents import Document\n",
"\n",
"query = \"What is the current enterprise pricing for the RTX 5090 GPU for bulk orders?\"\n",
"instruction = \"Prioritize internal sales documents over market analysis reports. More recent documents should be weighted higher. Enterprise portal content supersedes distributor communications.\"\n",
"\n",
"document_contents = [\n",
" \"Following detailed cost analysis and market research, we have implemented the following changes: AI training clusters will see a 15% uplift in raw compute performance, enterprise support packages are being restructured, and bulk procurement programs (100+ units) for the RTX 5090 Enterprise series will operate on a $2,899 baseline.\",\n",
" \"Enterprise pricing for the RTX 5090 GPU bulk orders (100+ units) is currently set at $3,100-$3,300 per unit. This pricing for RTX 5090 enterprise bulk orders has been confirmed across all major distribution channels.\",\n",
" \"RTX 5090 Enterprise GPU requires 450W TDP and 20% cooling overhead.\",\n",
"]\n",
"\n",
"metadata = [\n",
" {\n",
" \"Date\": \"January 15, 2025\",\n",
" \"Source\": \"NVIDIA Enterprise Sales Portal\",\n",
" \"Classification\": \"Internal Use Only\",\n",
" },\n",
" {\"Date\": \"11/30/2023\", \"Source\": \"TechAnalytics Research Group\"},\n",
" {\n",
" \"Date\": \"January 25, 2025\",\n",
" \"Source\": \"NVIDIA Enterprise Sales Portal\",\n",
" \"Classification\": \"Internal Use Only\",\n",
" },\n",
"]\n",
"\n",
"documents = [\n",
" Document(page_content=content, metadata=metadata[i])\n",
" for i, content in enumerate(document_contents)\n",
"]\n",
"reranked_documents = compressor.compress_documents(\n",
" query=query,\n",
" instruction=instruction,\n",
" documents=documents,\n",
")"
]
}
],
"metadata": {

View File

@ -0,0 +1,190 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Contextual AI Reranker\n",
"\n",
"Contextual AI's Instruction-Following Reranker is the world's first reranker designed to follow custom instructions about how to prioritize documents based on specific criteria like recency, source, and metadata. With superior performance on the BEIR benchmark (scoring 61.2 and outperforming competitors by significant margins), it delivers unprecedented control and accuracy for enterprise RAG applications.\n",
"\n",
"## Key Capabilities\n",
"\n",
"- **Instruction Following**: Dynamically control document ranking through natural language commands\n",
"- **Conflict Resolution**: Intelligently handle contradictory information from multiple knowledge sources\n",
"- **Superior Accuracy**: Achieve state-of-the-art performance on industry benchmarks\n",
"- **Seamless Integration**: Drop-in replacement for existing rerankers in your RAG pipeline\n",
"\n",
"The reranker excels at resolving real-world challenges in enterprise knowledge bases, such as prioritizing recent documents over outdated ones or favoring internal documentation over external sources.\n",
"\n",
"To learn more about our instruction-following reranker and see examples of it in action, visit our [product overview](https://contextual.ai/blog/introducing-instruction-following-reranker/).\n",
"\n",
"For comprehensive documentation on Contextual AI's products, please visit our [developer portal](https://docs.contextual.ai/).\n",
"\n",
"This integration requires the `contextual-client` Python SDK. Learn more about it [here](https://github.com/ContextualAI/contextual-client-python).\n",
"\n",
"## Overview\n",
"\n",
"This integration invokes Contextual AI's Grounded Language Model.\n",
"\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| [ContextualRerank](https://python.langchain.com/api_reference/en/latest/chat_models/langchain_contextual.rerank.ContextualRerank.html) | [langchain-contextual](https://python.langchain.com/api_reference/en/latest/contextual_api_reference.html) | ❌ | beta | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-contextual?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-contextual?style=flat-square&label=%20) |\n",
"\n",
"## Setup\n",
"\n",
"To access Contextual's reranker models you'll need to create a/an Contextual AI account, get an API key, and install the `langchain-contextual` integration package.\n",
"\n",
"### Credentials\n",
"\n",
"Head to [app.contextual.ai](https://app.contextual.ai) to sign up to Contextual and generate an API key. Once you've done this set the CONTEXTUAL_AI_API_KEY environment variable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"if not os.getenv(\"CONTEXTUAL_AI_API_KEY\"):\n",
" os.environ[\"CONTEXTUAL_AI_API_KEY\"] = getpass.getpass(\n",
" \"Enter your Contextual API key: \"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation\n",
"\n",
"The LangChain Contextual integration lives in the `langchain-contextual` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-contextual"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"The Contextual Reranker arguments are:\n",
"\n",
"| Parameter | Type | Description |\n",
"| --- | --- | --- |\n",
"| documents | list[Document] | A sequence of documents to rerank. Any metadata contained in the documents will also be used for reranking. |\n",
"| query | str | The query to use for reranking. |\n",
"| model | str | The version of the reranker to use. Currently, we just have \"ctxl-rerank-en-v1-instruct\". |\n",
"| top_n | Optional[int] | The number of results to return. If None returns all results. Defaults to self.top_n. |\n",
"| instruction | Optional[str] | The instruction to be used for the reranker. |\n",
"| callbacks | Optional[Callbacks] | Callbacks to run during the compression process. |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_contextual import ContextualRerank\n",
"\n",
"api_key = \"\"\n",
"model = \"ctxl-rerank-en-v1-instruct\"\n",
"\n",
"compressor = ContextualRerank(\n",
" model=model,\n",
" api_key=api_key,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Usage\n",
"\n",
"First, we will set up the global variables and examples we'll use, and instantiate our reranker client."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.documents import Document\n",
"\n",
"query = \"What is the current enterprise pricing for the RTX 5090 GPU for bulk orders?\"\n",
"instruction = \"Prioritize internal sales documents over market analysis reports. More recent documents should be weighted higher. Enterprise portal content supersedes distributor communications.\"\n",
"\n",
"document_contents = [\n",
" \"Following detailed cost analysis and market research, we have implemented the following changes: AI training clusters will see a 15% uplift in raw compute performance, enterprise support packages are being restructured, and bulk procurement programs (100+ units) for the RTX 5090 Enterprise series will operate on a $2,899 baseline.\",\n",
" \"Enterprise pricing for the RTX 5090 GPU bulk orders (100+ units) is currently set at $3,100-$3,300 per unit. This pricing for RTX 5090 enterprise bulk orders has been confirmed across all major distribution channels.\",\n",
" \"RTX 5090 Enterprise GPU requires 450W TDP and 20% cooling overhead.\",\n",
"]\n",
"\n",
"metadata = [\n",
" {\n",
" \"Date\": \"January 15, 2025\",\n",
" \"Source\": \"NVIDIA Enterprise Sales Portal\",\n",
" \"Classification\": \"Internal Use Only\",\n",
" },\n",
" {\"Date\": \"11/30/2023\", \"Source\": \"TechAnalytics Research Group\"},\n",
" {\n",
" \"Date\": \"January 25, 2025\",\n",
" \"Source\": \"NVIDIA Enterprise Sales Portal\",\n",
" \"Classification\": \"Internal Use Only\",\n",
" },\n",
"]\n",
"\n",
"documents = [\n",
" Document(page_content=content, metadata=metadata[i])\n",
" for i, content in enumerate(document_contents)\n",
"]\n",
"reranked_documents = compressor.compress_documents(\n",
" query=query,\n",
" instruction=instruction,\n",
" documents=documents,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use within a chain\n",
"\n",
"Examples coming soon."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all ChatContextual features and configurations head to the Github page: https://github.com/ContextualAI//langchain-contextual"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}