Skip to content

Commit 2f19cd5

Browse files
amaze-28hntrl
andauthored
feat: Add Perplexity support to universal chat model (langchain-ai#9028)
Co-authored-by: Hunter Lovell <40191806+hntrl@users.noreply.github.com>
1 parent 68e7642 commit 2f19cd5

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

.changeset/green-squids-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"langchain": patch
3+
---
4+
5+
feat: Add Perplexity support to universal chat model

langchain/src/chat_models/tests/universal.int.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,17 @@ describe("Works with all model providers", () => {
598598
expect(deepSeekResult).toBeDefined();
599599
expect(deepSeekResult.content.length).toBeGreaterThan(0);
600600
});
601+
602+
it("Can invoke perplexity", async () => {
603+
const perplexity = await initChatModel("sonar-pro", {
604+
modelProvider: "perplexity",
605+
temperature: 0,
606+
});
607+
608+
const perplexityResult = await perplexity.invoke("what's your name");
609+
expect(perplexityResult).toBeDefined();
610+
expect(perplexityResult.content.length).toBeGreaterThan(0);
611+
});
601612
});
602613

603614
test("Is compatible with agents", async () => {

langchain/src/chat_models/universal.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const _SUPPORTED_PROVIDERS = [
5151
"cerebras",
5252
"deepseek",
5353
"xai",
54+
"perplexity",
5455
] as const;
5556

5657
export type ChatModelProvider = (typeof _SUPPORTED_PROVIDERS)[number];
@@ -166,6 +167,19 @@ async function _initChatModelHelper(
166167
);
167168
return new ChatTogetherAI({ model, ...passedParams });
168169
}
170+
case "perplexity": {
171+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
172+
// @ts-ignore - Can not install as a proper dependency due to circular dependency
173+
const { ChatPerplexity } = await import(
174+
// We can not 'expect-error' because if you explicitly build `@langchain/community`
175+
// this import will be able to be resolved, thus there will be no error. However
176+
// this will never be the case in CI.
177+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
178+
// @ts-ignore - Can not install as a proper dependency due to circular dependency
179+
"@langchain/community/chat_models/perplexity"
180+
);
181+
return new ChatPerplexity({ model, ...passedParams });
182+
}
169183
default: {
170184
const supported = _SUPPORTED_PROVIDERS.join(", ");
171185
throw new Error(
@@ -220,6 +234,8 @@ export function _inferModelProvider(modelName: string): string | undefined {
220234
return "bedrock";
221235
} else if (modelName.startsWith("mistral")) {
222236
return "mistralai";
237+
} else if (modelName.startsWith("sonar") || modelName.startsWith("pplx")) {
238+
return "perplexity";
223239
} else {
224240
return undefined;
225241
}
@@ -638,6 +654,7 @@ export async function initChatModel<
638654
* - mistralai (@langchain/mistralai)
639655
* - groq (@langchain/groq)
640656
* - ollama (@langchain/ollama)
657+
* - perplexity (@langchain/community/chat_models/perplexity)
641658
* - cerebras (@langchain/cerebras)
642659
* - deepseek (@langchain/deepseek)
643660
* - xai (@langchain/xai)

0 commit comments

Comments
 (0)