Skip to content

Commit

Permalink
Merge pull request #765 from narengogi/chore/perplexity/citations
Browse files Browse the repository at this point in the history
  • Loading branch information
VisargD authored Nov 26, 2024
2 parents 8dde9dc + 42cee7a commit d1e14b9
Showing 1 changed file with 31 additions and 4 deletions.
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

0 comments on commit d1e14b9

Please sign in to comment.