跳到主要内容
这将帮助您开始使用 Contextual AI 的基础语言模型 聊天模型 要了解有关 Contextual AI 的更多信息,请访问我们的文档 此集成需要 contextual-client Python SDK。在此处了解更多信息:这里

概览

此集成调用 Contextual AI 的基础语言模型。

集成详情

类别本地可序列化JS 支持下载量版本
ChatContextuallangchain-contextual测试版PyPI - DownloadsPyPI - Version

模型功能

工具调用结构化输出JSON 模式图像输入音频输入视频输入令牌级流式传输原生异步Token 用量Logprobs

设置

要访问 Contextual 模型,您需要创建一个 Contextual AI 帐户,获取一个 API 密钥,并安装 langchain-contextual 集成包。

凭据

前往 app.contextual.ai 注册 Contextual 并生成一个 API 密钥。完成此操作后,设置 CONTEXTUAL_AI_API_KEY 环境变量。
import getpass
import os

if not os.getenv("CONTEXTUAL_AI_API_KEY"):
    os.environ["CONTEXTUAL_AI_API_KEY"] = getpass.getpass(
        "Enter your Contextual API key: "
    )
如果您想获取模型调用的自动化跟踪,您还可以通过取消注释下方来设置您的 LangSmith API 密钥
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

安装

LangChain Contextual 集成位于 langchain-contextual 包中
pip install -qU langchain-contextual

实例化

现在我们可以实例化我们的模型对象并生成聊天补全。 聊天客户端可以使用以下附加设置进行实例化:
参数类型描述默认
temperature可选[浮点数]采样温度,影响响应中的随机性。请注意,较高的温度值可能会降低基础性。0
top_p可选[浮点数]一种用于核采样的参数,是温度的替代方案,也影响响应的随机性。请注意,较高的 top_p 值可能会降低基础性。0.9
max_new_tokens可选[整数]模型在响应中可以生成的最大 token 数。最小值为 1,最大值为 2048。1024
from langchain_contextual import ChatContextual

llm = ChatContextual(
    model="v1",  # defaults to `v1`
    api_key="",
    temperature=0,  # defaults to 0
    top_p=0.9,  # defaults to 0.9
    max_new_tokens=1024,  # defaults to 1024
)

调用

Contextual 基础语言模型在调用 ChatContextual.invoke 方法时接受额外的 kwargs 这些额外的输入包括:
参数类型描述
知识列表[字符串]必需:基础语言模型在生成响应时可以使用的知识源字符串列表。
system_prompt可选[字符串]可选:模型在生成响应时应遵循的指令。请注意,我们不保证模型会完全遵循这些指令。
avoid_commentary可选[布尔值]可选(默认为 False):一个标志,指示模型是否应避免在响应中提供额外的评论。评论本质上是对话性的,不包含可验证的声明;因此,评论不严格基于可用上下文。但是,评论可能提供有用的上下文,从而提高响应的帮助性。
# 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)

链接

我们可以将 Contextual 模型与输出解析器链接起来。
from langchain_core.output_parsers import StrOutputParser

chain = llm | StrOutputParser

chain.invoke(
    messages, knowledge=knowledge, systemp_prompt=system_prompt, avoid_commentary=True
)

API 参考

有关所有 ChatContextual 功能和配置的详细文档,请访问 Github 页面:github.com/ContextualAI//langchain-contextual
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.