Skip to main content
Open In ColabOpen on GitHub

Contextual AI

上下文智能提供最先进的RAG组件,专为准确可靠的企事业AI应用而设计。我们的LangChain集成提供了针对专用模型的独立API端点:

  • 接地语言模型(GLM):全球最接地的语言模型,通过优先考虑对检索知识的忠实性来最大限度减少幻觉。GLM 提供卓越的事实准确性,并带有内联引用,非常适合企业级 RAG 和代理应用,这些场景中可靠性至关重要。

  • 指令遵循重排序器:首个能够根据自定义指令智能优先排序文档的重排序器,可基于特定标准(如时效性、来源或文档类型)进行优化。在行业基准测试中表现优于竞争对手,我们的重排序器解决了企业知识库中冲突信息的难题。

由RAG技术的发明者创立,Contextual AI的专业组件帮助创新团队加速开发可投入生产的RAG代理,从而实现高精度的回答。

接地语言模型(GLM)

接地语言模型(GLM)专为在企业RAG和代理型应用中最大限度减少幻觉而设计。GLM提供:

  • 在FACTS基准测试中实现88%的事实准确性,表现强劲 (查看基准测试结果)
  • 响应严格基于提供的知识来源,并附带内联引用 (阅读产品详情)
  • 生成响应中直接集成的精确来源引用
  • 检索到的上下文优先于参数知识 (查看技术概览)
  • 当信息不可用时,明确承认不确定性

GLM 可作为 RAG 流程中通用大型语言模型的即插即用替代方案,显著提升关键企业应用的可靠性。

指令遵循重排序器

世界上首个指令遵循重排序器(Instruction-Following Reranker)彻底革新了文档排序方式,实现了前所未有的控制力和准确性。主要功能包括:

  • 基于最新性、来源、元数据等条件对文档进行优先级排序的自然语言指令 (查看工作原理)
  • 在BEIR基准测试中表现优异,得分为61.2,显著优于竞争对手(查看基准数据)
  • 来自多个知识源的冲突信息的智能解决
  • 无缝集成,作为现有重排序器的即插即用替代方案
  • 通过自然语言命令动态控制文档排序

重排序器在处理可能包含矛盾信息的企业知识库方面表现出色,使您能够指定在不同场景下哪些来源应优先考虑。

使用 LangChain 的上下文感知 AI

查看详细信息 这里

此集成可让您轻松将 Contextual AI 的 GLM 和指令遵循重排序器引入您的 LangChain 工作流。GLM 确保您的应用程序提供严格基于上下文的响应,而重排序器则通过智能优先排序最相关的文档,显著提升检索质量。

无论您是在为受监管行业或注重安全的环境开发应用程序,Contextual AI 都能提供企业级应用场景所需的准确性、控制力和可靠性。

今天开始免费试用,体验最适合企业人工智能应用的最可靠的语言模型和指令遵循重排序器。

接地语言模型

# Integrating the Grounded Language Model
import getpass
import os

from langchain_contextual import ChatContextual

# Set credentials
if not os.getenv("CONTEXTUAL_AI_API_KEY"):
os.environ["CONTEXTUAL_AI_API_KEY"] = getpass.getpass(
"Enter your Contextual API key: "
)

# initialize Contextual llm
llm = ChatContextual(
model="v1",
api_key="",
)
# include a system prompt (optional)
system_prompt = "You are a helpful assistant that uses all of the provided knowledge to answer the user's query to the best of your ability."

# provide your own knowledge from your knowledge-base here in an array of string
knowledge = [
"There are 2 types of dogs in the world: good dogs and best dogs.",
"There are 2 types of cats in the world: good cats and best cats.",
]

# create your message
messages = [
("human", "What type of cats are there in the world and what are the types?"),
]

# invoke the GLM by providing the knowledge strings, optional system prompt
# if you want to turn off the GLM's commentary, pass True to the `avoid_commentary` argument
ai_msg = llm.invoke(
messages, knowledge=knowledge, system_prompt=system_prompt, avoid_commentary=True
)

print(ai_msg.content)
According to the information available, there are two types of cats in the world:

1. Good cats
2. Best cats

指令遵循重排序器

import getpass
import os

from langchain_contextual import ContextualRerank

if not os.getenv("CONTEXTUAL_AI_API_KEY"):
os.environ["CONTEXTUAL_AI_API_KEY"] = getpass.getpass(
"Enter your Contextual API key: "
)


api_key = ""
model = "ctxl-rerank-en-v1-instruct"

compressor = ContextualRerank(
model=model,
api_key=api_key,
)

from langchain_core.documents import Document

query = "What is the current enterprise pricing for the RTX 5090 GPU for bulk orders?"
instruction = "Prioritize internal sales documents over market analysis reports. More recent documents should be weighted higher. Enterprise portal content supersedes distributor communications."

document_contents = [
"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.",
"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.",
"RTX 5090 Enterprise GPU requires 450W TDP and 20% cooling overhead.",
]

metadata = [
{
"Date": "January 15, 2025",
"Source": "NVIDIA Enterprise Sales Portal",
"Classification": "Internal Use Only",
},
{"Date": "11/30/2023", "Source": "TechAnalytics Research Group"},
{
"Date": "January 25, 2025",
"Source": "NVIDIA Enterprise Sales Portal",
"Classification": "Internal Use Only",
},
]

documents = [
Document(page_content=content, metadata=metadata[i])
for i, content in enumerate(document_contents)
]
reranked_documents = compressor.compress_documents(
query=query,
instruction=instruction,
documents=documents,
)
API 参考:文档