ChatPerplexity
本指南将帮助你开始使用 Perplexity
聊天模型。如需了解 ChatPerplexity
所有功能和配置的详细文档,请访问 API
参考文档。
概览
集成详情
| 类别 | 包 | 本地支持 | 可序列化 | Python 支持 | 包下载量 | 最新版本 |
|---|---|---|---|---|---|---|
ChatPerplexity | @langchain/community | ❌ | beta | ✅ | ![]() | ![]() |
模型特性
有关如何使用特定特性的指南,请参见下表标题中的链接。
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | 逐 token 流式传输 | token 使用量 | 对数概率 |
|---|---|---|---|---|---|---|---|---|
| ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ |
请注意,在撰写本文时,Perplexity 仅在某些使用层级上支持结构化输出。
准备工作
要访问 Perplexity 模型,你需要创建一个 Perplexity 账户,获取 API
密钥,并安装 @langchain/community 集成包。
凭据
前往 https://perplexity.ai 注册 Perplexity 并生成一个 API
密钥。完成此操作后,请设置 PERPLEXITY_API_KEY 环境变量:
export PERPLEXITY_API_KEY="your-api-key"
如果你想自动追踪模型调用,也可以通过取消注释以下内容来设置你的 LangSmith API 密钥:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
安装
LangChain Perplexity 的集成位于 @langchain/community 包中:
:::提示 请参阅安装集成包的一般说明部分。 :::
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
实例化
现在我们可以实例化我们的模型对象并生成聊天补全:
import { ChatPerplexity } from "@langchain/community/chat_models/perplexity";
const llm = new ChatPerplexity({
model: "sonar",
temperature: 0,
maxTokens: undefined,
timeout: undefined,
maxRetries: 2,
// other params...
});
调用
const aiMsg = await llm.invoke([
{
role: "system",
content:
"You are a helpful assistant that translates English to French. Translate the user sentence.",
},
{
role: "user",
content: "I love programming.",
},
]);
aiMsg;
AIMessage {
"id": "run-71853938-aa30-4861-9019-f12323c09f9a",
"content": "J'adore la programmation.",
"additional_kwargs": {
"citations": [
"https://careersatagoda.com/blog/why-we-love-programming/",
"https://henrikwarne.com/2012/06/02/why-i-love-coding/",
"https://forum.freecodecamp.org/t/i-love-programming-but/497502",
"https://ilovecoding.org",
"https://thecodinglove.com"
]
},
"response_metadata": {
"tokenUsage": {
"promptTokens": 20,
"completionTokens": 9,
"totalTokens": 29
}
},
"tool_calls": [],
"invalid_tool_calls": []
}
console.log(aiMsg.content);
J'adore la programmation.
链式调用
我们可以像这样将模型与提示模板链式调用:
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
],
["human", "{input}"],
]);
const chain = prompt.pipe(llm);
await chain.invoke({
input_language: "English",
output_language: "German",
input: "I love programming.",
});
AIMessage {
"id": "run-a44dc452-4a71-423d-a4ee-50a2d7c90abd",
"content": "**English to German Translation:**\n\n\"I love programming\" translates to **\"Ich liebe das Programmieren.\"**\n\nIf you'd like to express your passion for programming in more detail, here are some additional translations:\n\n- **\"Programming is incredibly rewarding and fulfilling.\"** translates to **\"Das Programmieren ist unglaublich lohnend und erfüllend.\"**\n- **\"I enjoy solving problems through coding.\"** translates to **\"Ich genieße es, Probleme durch Codieren zu lösen.\"**\n- **\"I find the process of creating something from nothing very satisfying.\"** translates to **\"Ich finde den Prozess, etwas aus dem Nichts zu schaffen, sehr befriedigend.\"**",
"additional_kwargs": {
"citations": [
"https://careersatagoda.com/blog/why-we-love-programming/",
"https://henrikwarne.com/2012/06/02/why-i-love-coding/",
"https://dev.to/dvddpl/coding-is-boring-why-do-you-love-coding-cl0",
"https://forum.freecodecamp.org/t/i-love-programming-but/497502",
"https://ilovecoding.org"
]
},
"response_metadata": {
"tokenUsage": {
"promptTokens": 15,
"completionTokens": 149,
"totalTokens": 164
}
},
"tool_calls": [],
"invalid_tool_calls": []
}
API 参考文档
如需了解 ChatPerplexity 所有功能和配置的详细文档,请访问 API 参考页面: https://api.js.langchain.com/classes/\_langchain_community.chat_models_perplexity.ChatPerplexity.html
Related
- Chat model conceptual guide
- Chat model how-to guides

