Skip to content
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

Allow citations through strictOpenAiCompliance flag for perplexity #765

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/providers/perplexity-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const PerplexityAIChatCompleteConfig: ProviderConfig = {
min: 0,
max: 1,
},
search_domain_filter: {
param: 'search_domain_filter',
required: false,
},
top_k: {
param: 'top_k',
min: 0,
Expand Down Expand Up @@ -83,6 +87,7 @@ export interface PerplexityAIChatCompleteResponse {
model: string;
object: string;
created: number;
citations: string[];
choices: PerplexityAIChatChoice[];
usage: {
prompt_tokens: number;
Expand All @@ -104,6 +109,7 @@ export interface PerplexityAIChatCompletionStreamChunk {
model: string;
object: string;
created: number;
citations?: string[];
usage: {
prompt_tokens: number;
completion_tokens: number;
Expand All @@ -114,8 +120,15 @@ export interface PerplexityAIChatCompletionStreamChunk {

export const PerplexityAIChatCompleteResponseTransform: (
response: PerplexityAIChatCompleteResponse | PerplexityAIErrorResponse,
responseStatus: number
) => ChatCompletionResponse | ErrorResponse = (response) => {
responseStatus: number,
responseHeaders: Headers,
strictOpenAiCompliance: boolean
) => ChatCompletionResponse | ErrorResponse = (
response,
_responseStatus,
_responseHeaders,
strictOpenAiCompliance
) => {
if ('error' in response) {
return generateErrorResponse(
{
Expand All @@ -135,6 +148,9 @@ export const PerplexityAIChatCompleteResponseTransform: (
created: response.created,
model: response.model,
provider: PERPLEXITY_AI,
...(!strictOpenAiCompliance && {
citations: response.citations,
}),
choices: [
{
message: {
Expand All @@ -158,8 +174,16 @@ export const PerplexityAIChatCompleteResponseTransform: (
};

export const PerplexityAIChatCompleteStreamChunkTransform: (
response: string
) => string = (responseChunk) => {
response: string,
fallbackId: string,
streamState: any,
strictOpenAiCompliance: boolean
) => string = (
responseChunk,
fallbackId,
_streamState,
strictOpenAiCompliance
) => {
let chunk = responseChunk.trim();
chunk = chunk.replace(/^data: /, '');
chunk = chunk.trim();
Expand All @@ -172,6 +196,9 @@ export const PerplexityAIChatCompleteStreamChunkTransform: (
created: Math.floor(Date.now() / 1000),
model: parsedChunk.model,
provider: PERPLEXITY_AI,
...(!strictOpenAiCompliance && {
citations: parsedChunk.citations,
}),
choices: [
{
delta: {
Expand Down
Loading