ChatContextual
这将帮助您开始使用 Contextual AI 的 Grounded Language Model 聊天模型。
要了解更多关于 Contextual AI 的信息,请访问我们的文档。
此集成需要 contextual-client Python SDK。在此处了解更多信息。
概述
此集成调用 Contextual AI 的 Grounded Language Model。
集成详情
| 类 | 包 | 本地 | 可序列化 | JS 支持 | 包下载 | 包最新版本 |
|---|---|---|---|---|---|---|
| ChatContextual | langchain-contextual | ❌ | beta | ❌ |
模型功能
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | Token 级别流式传输 | 原生异步 | 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 | Optional[float] | 采样温度,影响响应的随机性。请注意,较高的温度值可能会降低基准性。 | 0 |
| top_p | Optional[float] | 核心采样参数,是温度的替代选项,同样影响响应的随机性。请注意,较高的 top_p 值可能会降低基准性。 | 0.9 |
| max_new_tokens | Optional[int] | 模型在响应中可以生成的最大 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
)
调用
ChatContextual.invoke 方法在调用时接受额外的 kwargs。
这些额外的输入包括:
| 参数 | 类型 | 描述 |
|---|---|---|
| knowledge | list[str] | 必需:一个字符串列表,包含模型在生成响应时可使用的知识来源。 |
| system_prompt | Optional[str] | 可选:模型在生成响应时应遵循的指令。请注意,我们不保证模型会严格遵守这些指令。 |
| avoid_commentary | Optional[bool] | 可选(默认为 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)
链式调用
我们可以将语境化模型与输出解析器进行链式调用。
from langchain_core.output_parsers import StrOutputParser
chain = llm | StrOutputParser
chain.invoke(
messages, knowledge=knowledge, systemp_prompt=system_prompt, avoid_commentary=True
)
API Reference:StrOutputParser
API 参考
如需了解 ChatContextual 的所有功能和配置的详细文档,请访问 Github 页面:https://github.com/ContextualAI//langchain-contextual
Related
- Chat model conceptual guide
- Chat model how-to guides