Skip to content

Commit

Permalink
img2
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Feb 13, 2025
1 parent 827cda2 commit f8f36e0
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
79 changes: 74 additions & 5 deletions packages/perplexity/src/perplexity-language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ describe('PerplexityLanguageModel', () => {
contents,
usage = { prompt_tokens: 10, completion_tokens: 20 },
citations = [],
images,
}: {
contents: string[];
usage?: {
Expand All @@ -317,6 +318,7 @@ describe('PerplexityLanguageModel', () => {
num_search_queries?: number;
};
citations?: string[];
images?: z.infer<typeof perplexityImageSchema>[];
}) {
const baseChunk = (
content: string,
Expand All @@ -327,6 +329,7 @@ describe('PerplexityLanguageModel', () => {
id: 'stream-id',
created: 1680003600,
model: modelId,
images,
citations,
choices: [
{
Expand Down Expand Up @@ -358,7 +361,7 @@ describe('PerplexityLanguageModel', () => {
};
}

it('should stream text deltas correctly', async () => {
it('should stream text deltas', async () => {
prepareStreamResponse({ contents: ['Hello', ', ', 'World!'] });

const { stream } = await perplexityLM.doStream({
Expand Down Expand Up @@ -394,6 +397,7 @@ describe('PerplexityLanguageModel', () => {
usage: { promptTokens: 10, completionTokens: 20 },
providerMetadata: {
perplexity: {
images: null,
usage: {
citationTokens: null,
numSearchQueries: null,
Expand All @@ -404,7 +408,7 @@ describe('PerplexityLanguageModel', () => {
]);
});

it('should stream sources correctly', async () => {
it('should stream sources', async () => {
prepareStreamResponse({
contents: ['Hello', ', ', 'World!'],
citations: ['http://example.com/123', 'https://example.com/456'],
Expand Down Expand Up @@ -459,6 +463,7 @@ describe('PerplexityLanguageModel', () => {
usage: { promptTokens: 10, completionTokens: 20 },
providerMetadata: {
perplexity: {
images: null,
usage: {
citationTokens: null,
numSearchQueries: null,
Expand Down Expand Up @@ -486,7 +491,70 @@ describe('PerplexityLanguageModel', () => {
});
});

it('should send usage in streaming mode', async () => {
it('should send usage', async () => {
prepareStreamResponse({
contents: ['Hello', ', ', 'World!'],
images: [
{
image_url: 'https://example.com/image.jpg',
origin_url: 'https://example.com/image.jpg',
height: 100,
width: 100,
},
],
});
const { stream } = await perplexityLM.doStream({
inputFormat: 'prompt',
mode: { type: 'regular' },
prompt: TEST_PROMPT,
});

const result = await convertReadableStreamToArray(stream);

expect(result).toEqual([
{
id: 'stream-id',
modelId: 'perplexity-001',
timestamp: new Date('2023-03-28T11:40:00.000Z'),
type: 'response-metadata',
},
{
type: 'text-delta',
textDelta: 'Hello',
},
{
type: 'text-delta',
textDelta: ', ',
},
{
type: 'text-delta',
textDelta: 'World!',
},
{
type: 'finish',
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 20 },
providerMetadata: {
perplexity: {
images: [
{
imageUrl: 'https://example.com/image.jpg',
originUrl: 'https://example.com/image.jpg',
height: 100,
width: 100,
},
],
usage: {
citationTokens: null,
numSearchQueries: null,
},
},
},
},
]);
});

it('should send images', async () => {
prepareStreamResponse({
contents: ['Hello', ', ', 'World!'],
usage: {
Expand All @@ -505,7 +573,7 @@ describe('PerplexityLanguageModel', () => {

const result = await convertReadableStreamToArray(stream);

expect(result).toEqual([
expect(result).toStrictEqual([
{
id: 'stream-id',
modelId: 'perplexity-001',
Expand All @@ -530,6 +598,7 @@ describe('PerplexityLanguageModel', () => {
usage: { promptTokens: 11, completionTokens: 21 },
providerMetadata: {
perplexity: {
images: null,
usage: {
citationTokens: 30,
numSearchQueries: 40,
Expand All @@ -540,7 +609,7 @@ describe('PerplexityLanguageModel', () => {
]);
});

it('should pass headers in streaming mode', async () => {
it('should pass headers', async () => {
prepareStreamResponse({ contents: [] });
const lmWithCustomHeaders = new PerplexityLanguageModel(modelId, {
baseURL: 'https://api.perplexity.ai',
Expand Down
16 changes: 16 additions & 0 deletions packages/perplexity/src/perplexity-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,20 @@ export class PerplexityLanguageModel implements LanguageModelV1 {
citationTokens: number | null;
numSearchQueries: number | null;
};
images: Array<{
imageUrl: string;
originUrl: string;
height: number;
width: number;
}> | null;
};
} = {
perplexity: {
usage: {
citationTokens: null,
numSearchQueries: null,
},
images: null,
},
};
let isFirstChunk = true;
Expand Down Expand Up @@ -297,6 +304,15 @@ export class PerplexityLanguageModel implements LanguageModelV1 {
};
}

if (value.images != null) {
providerMetadata.perplexity.images = value.images.map(image => ({
imageUrl: image.image_url,
originUrl: image.origin_url,
height: image.height,
width: image.width,
}));
}

const choice = value.choices[0];
if (choice?.finish_reason != null) {
finishReason = mapPerplexityFinishReason(choice.finish_reason);
Expand Down

0 comments on commit f8f36e0

Please sign in to comment.