-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add Perplexity as a BYOK provider #5094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jliounis
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
jliounis:add-perplexity-byok-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+280
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| import { IConfigurationService } from '../../../platform/configuration/common/configurationService'; | ||
| import { ILogService } from '../../../platform/log/common/logService'; | ||
| import { IFetcherService } from '../../../platform/networking/common/fetcherService'; | ||
| import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService'; | ||
| import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation'; | ||
| import { packageJson } from '../../../platform/env/common/packagejson'; | ||
| import { BYOKKnownModels, BYOKModelCapabilities, byokKnownModelsToAPIInfo } from '../common/byokProvider'; | ||
| import { AbstractOpenAICompatibleLMProvider, LanguageModelChatConfiguration, OpenAICompatibleLanguageModelChatInformation } from './abstractLanguageModelChatProvider'; | ||
| import { IBYOKStorageService } from './byokStorageService'; | ||
|
|
||
| // Perplexity Agent API: https://docs.perplexity.ai/docs/agent-api | ||
| // OpenAI-Responses-compatible base URL: https://api.perplexity.ai/v1 | ||
| // Exposes third-party models (OpenAI, Anthropic, Google, etc.) plus presets | ||
| // (e.g. pro-search) under one API key. Does not provide a stable `/models` | ||
| // discovery endpoint, so we ship a curated list and override `getAllModels`. | ||
| const PERPLEXITY_INTEGRATION_HEADER = 'X-Pplx-Integration'; | ||
|
|
||
| const PERPLEXITY_KNOWN_MODELS: BYOKKnownModels = { | ||
| 'openai/gpt-5.4': { | ||
| name: 'GPT-5.4 (via Perplexity Agent API)', | ||
| toolCalling: true, | ||
| vision: true, | ||
| maxInputTokens: 200000, | ||
| maxOutputTokens: 16000, | ||
| }, | ||
| 'openai/gpt-5.2': { | ||
| name: 'GPT-5.2 (via Perplexity Agent API)', | ||
| toolCalling: true, | ||
| vision: true, | ||
| maxInputTokens: 200000, | ||
| maxOutputTokens: 16000, | ||
| }, | ||
| 'anthropic/claude-sonnet-4-6': { | ||
| name: 'Claude Sonnet 4.6 (via Perplexity Agent API)', | ||
| toolCalling: true, | ||
| vision: true, | ||
| maxInputTokens: 200000, | ||
| maxOutputTokens: 16000, | ||
| }, | ||
| 'anthropic/claude-opus-4-7': { | ||
| name: 'Claude Opus 4.7 (via Perplexity Agent API)', | ||
| toolCalling: true, | ||
| vision: true, | ||
| maxInputTokens: 200000, | ||
| maxOutputTokens: 32000, | ||
| thinking: true, | ||
| }, | ||
| 'google/gemini-3-1-pro': { | ||
| name: 'Gemini 3.1 Pro (via Perplexity Agent API)', | ||
| toolCalling: true, | ||
| vision: true, | ||
| maxInputTokens: 1000000, | ||
| maxOutputTokens: 16000, | ||
| }, | ||
| }; | ||
|
|
||
| export class PerplexityLMProvider extends AbstractOpenAICompatibleLMProvider { | ||
|
|
||
| public static readonly providerName = 'Perplexity'; | ||
|
|
||
| constructor( | ||
| knownModels: BYOKKnownModels | undefined, | ||
| byokStorageService: IBYOKStorageService, | ||
| @IFetcherService fetcherService: IFetcherService, | ||
| @ILogService logService: ILogService, | ||
| @IInstantiationService instantiationService: IInstantiationService, | ||
| @IConfigurationService configurationService: IConfigurationService, | ||
| @IExperimentationService expService: IExperimentationService | ||
| ) { | ||
| super( | ||
| PerplexityLMProvider.providerName.toLowerCase(), | ||
| PerplexityLMProvider.providerName, | ||
| PerplexityLMProvider.mergeKnownModels(knownModels), | ||
| byokStorageService, | ||
| fetcherService, | ||
| logService, | ||
| instantiationService, | ||
| configurationService, | ||
| expService | ||
| ); | ||
| } | ||
|
|
||
| private static mergeKnownModels(remote: BYOKKnownModels | undefined): BYOKKnownModels { | ||
| const integrationHeader = { | ||
| [PERPLEXITY_INTEGRATION_HEADER]: `vscode-copilot/${packageJson.version}`, | ||
| }; | ||
| const merged: BYOKKnownModels = {}; | ||
| for (const [id, caps] of Object.entries(PERPLEXITY_KNOWN_MODELS)) { | ||
| merged[id] = { ...caps, requestHeaders: { ...(caps.requestHeaders ?? {}), ...integrationHeader } }; | ||
| } | ||
| if (remote) { | ||
| for (const [id, caps] of Object.entries(remote)) { | ||
| merged[id] = { ...caps, requestHeaders: { ...(caps.requestHeaders ?? {}), ...integrationHeader } }; | ||
| } | ||
| } | ||
| return merged; | ||
| } | ||
|
|
||
| protected getModelsBaseUrl(): string | undefined { | ||
| // Agent API base URL. The Agent API is OpenAI-Responses-compatible at | ||
| // /v1/responses (alias) and /v1/agent (primary). It exposes third-party | ||
| // models from OpenAI, Anthropic, Google, etc., plus presets like pro-search. | ||
| return 'https://api.perplexity.ai/v1'; | ||
| } | ||
|
|
||
| protected override async getAllModels(silent: boolean, apiKey: string | undefined, configuration: LanguageModelChatConfiguration | undefined): Promise<OpenAICompatibleLanguageModelChatInformation<LanguageModelChatConfiguration>[]> { | ||
| const baseUrl = this.getModelsBaseUrl(); | ||
| const merged = this._knownModels ?? {}; | ||
| return byokKnownModelsToAPIInfo(this._name, merged).map(model => ({ | ||
| ...model, | ||
| url: baseUrl ?? 'https://api.perplexity.ai/v1', | ||
| })); | ||
| } | ||
|
|
||
| protected override resolveModelCapabilities(modelData: unknown): BYOKModelCapabilities | undefined { | ||
| const data = modelData as { id?: string; name?: string }; | ||
| if (!data?.id) { | ||
| return undefined; | ||
| } | ||
| // Sensible defaults for any model returned by /models that we don't already know about. | ||
| return { | ||
| name: data.name ?? data.id, | ||
| toolCalling: true, | ||
| vision: false, | ||
| maxInputTokens: 128000, | ||
| maxOutputTokens: 8000, | ||
| requestHeaders: { | ||
| [PERPLEXITY_INTEGRATION_HEADER]: `vscode-copilot/${packageJson.version}`, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
src/extension/byok/vscode-node/test/perplexityProvider.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { PerplexityLMProvider } from '../perplexityProvider'; | ||
|
|
||
| function createProvider(knownModels?: Record<string, any>) { | ||
| const fetch = vi.fn(async (url: string) => { | ||
| throw new Error(`Unexpected fetch in test: ${url}`); | ||
| }); | ||
|
|
||
| const logService = { | ||
| _serviceBrand: undefined, | ||
| trace: vi.fn(), | ||
| debug: vi.fn(), | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| show: vi.fn(), | ||
| createSubLogger: vi.fn(), | ||
| withExtraTarget: vi.fn(), | ||
| }; | ||
| logService.createSubLogger.mockReturnValue(logService); | ||
| logService.withExtraTarget.mockReturnValue(logService); | ||
|
|
||
| const storage = { | ||
| getAPIKey: vi.fn().mockResolvedValue(undefined), | ||
| storeAPIKey: vi.fn().mockResolvedValue(undefined), | ||
| deleteAPIKey: vi.fn().mockResolvedValue(undefined), | ||
| getStoredModelConfigs: vi.fn().mockResolvedValue({}), | ||
| saveModelConfig: vi.fn().mockResolvedValue(undefined), | ||
| removeModelConfig: vi.fn().mockResolvedValue(undefined), | ||
| }; | ||
|
|
||
| const provider = new PerplexityLMProvider( | ||
| knownModels as any, | ||
| storage as any, | ||
| { fetch } as any, | ||
| logService as any, | ||
| { createInstance: vi.fn().mockReturnValue({}) } as any, | ||
| { | ||
| isConfigured: vi.fn().mockReturnValue(false), | ||
| getConfig: vi.fn(), | ||
| setConfig: vi.fn(), | ||
| } as any, | ||
| {} as any | ||
| ); | ||
|
|
||
| return { provider, fetch, storage, logService }; | ||
| } | ||
|
|
||
| describe('PerplexityLMProvider', () => { | ||
| it('getAllModels returns the curated Agent API model list', async () => { | ||
| const { provider, fetch } = createProvider(); | ||
|
|
||
| const models = await (provider as any).getAllModels(true, 'test-api-key', undefined); | ||
|
|
||
| expect(models.length).toBe(5); | ||
|
|
||
| const expectedIds = [ | ||
| 'openai/gpt-5.4', | ||
| 'openai/gpt-5.2', | ||
| 'anthropic/claude-sonnet-4-6', | ||
| 'anthropic/claude-opus-4-7', | ||
| 'google/gemini-3-1-pro', | ||
| ]; | ||
| const ids = models.map((m: any) => m.id); | ||
| expect(ids.sort()).toEqual(expectedIds.sort()); | ||
|
|
||
| // No sonar references at all in the curated list. | ||
| expect(models.some((m: any) => m.id.includes('sonar'))).toBe(false); | ||
| expect(models.some((m: any) => (m.name ?? '').toLowerCase().includes('sonar'))).toBe(false); | ||
|
|
||
| // Each model points at the Agent API base URL. | ||
| for (const model of models) { | ||
| expect(model.url).toBe('https://api.perplexity.ai/v1'); | ||
| } | ||
|
|
||
| // /models endpoint must never be queried — getAllModels is overridden. | ||
| expect(fetch).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('every model includes X-Pplx-Integration header in merged capabilities', async () => { | ||
| const { provider } = createProvider(); | ||
|
|
||
| const knownModels = (provider as any)._knownModels as Record<string, { requestHeaders?: Record<string, string> }>; | ||
| expect(knownModels).toBeDefined(); | ||
| const ids = Object.keys(knownModels); | ||
| expect(ids.length).toBe(5); | ||
|
|
||
| for (const id of ids) { | ||
| const headers = knownModels[id].requestHeaders; | ||
| expect(headers).toBeDefined(); | ||
| expect(headers!['X-Pplx-Integration']).toMatch(/^vscode-copilot\//); | ||
| } | ||
| }); | ||
|
|
||
| it('integration header is not overridden by per-model requestHeaders', () => { | ||
| const { provider } = createProvider({ | ||
| 'openai/gpt-5.4': { | ||
| name: 'GPT-5.4 (custom)', | ||
| toolCalling: true, | ||
| vision: true, | ||
| maxInputTokens: 200000, | ||
| maxOutputTokens: 16000, | ||
| requestHeaders: { | ||
| 'X-Pplx-Integration': 'malicious-override', | ||
| 'X-Custom-Header': 'kept', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const knownModels = (provider as any)._knownModels as Record<string, { requestHeaders?: Record<string, string> }>; | ||
| const headers = knownModels['openai/gpt-5.4'].requestHeaders!; | ||
|
|
||
| // The provider's integration header always wins. | ||
| expect(headers['X-Pplx-Integration']).toMatch(/^vscode-copilot\//); | ||
| expect(headers['X-Pplx-Integration']).not.toBe('malicious-override'); | ||
|
|
||
| // Other custom headers should be preserved. | ||
| expect(headers['X-Custom-Header']).toBe('kept'); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new BYOK provider is introduced but there are no accompanying unit tests under
src/extension/byok/vscode-node/test/(other providers like Ollama/Gemini/Azure have coverage). Adding a Perplexity provider spec would help lock in: the curated model list shown in the picker and thatX-Pplx-Integrationis present in each model'srequestHeaders.