Skip to content
12 changes: 11 additions & 1 deletion nodejs/perplexity/sample-agent/src/perplexityClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ interface ChatCompletionResponse {
[key: string]: unknown;
}

const PERPLEXITY_API_HOST = "api.perplexity.ai";
const PERPLEXITY_INTEGRATION = "agent365-samples";

/**
* Client for interacting with the Perplexity AI SDK.
*/
Expand All @@ -41,7 +44,14 @@ export class PerplexityClient {
private systemPrompt: string;

constructor(apiKey: string, model: string, systemPrompt: string) {
this.client = new Perplexity({ apiKey });
const client = new Perplexity({ apiKey });
// Identify this sample to Perplexity only when targeting the public API host.
this.client =
new URL(client.baseURL).hostname === PERPLEXITY_API_HOST
? client.withOptions({
defaultHeaders: { "X-Pplx-Integration": PERPLEXITY_INTEGRATION },
})
: client;
this.model = model;
this.systemPrompt = systemPrompt;
}
Expand Down