fix(server): filter out attachment in pplx provider (#11986)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved handling of messages without attachments, ensuring a placeholder is shown when content is empty and attachments are omitted. - **New Features** - Added the ability to selectively include or exclude attachments in message processing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -82,7 +82,7 @@ export class PerplexityProvider
|
|||||||
try {
|
try {
|
||||||
metrics.ai.counter('chat_text_calls').add(1, { model });
|
metrics.ai.counter('chat_text_calls').add(1, { model });
|
||||||
|
|
||||||
const [system, msgs] = await chatToGPTMessage(messages);
|
const [system, msgs] = await chatToGPTMessage(messages, false);
|
||||||
|
|
||||||
const modelInstance = this.#instance(model);
|
const modelInstance = this.#instance(model);
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ export class PerplexityProvider
|
|||||||
try {
|
try {
|
||||||
metrics.ai.counter('chat_text_stream_calls').add(1, { model });
|
metrics.ai.counter('chat_text_stream_calls').add(1, { model });
|
||||||
|
|
||||||
const [system, msgs] = await chatToGPTMessage(messages);
|
const [system, msgs] = await chatToGPTMessage(messages, false);
|
||||||
|
|
||||||
const modelInstance = this.#instance(model);
|
const modelInstance = this.#instance(model);
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,9 @@ async function inferMimeType(url: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function chatToGPTMessage(
|
export async function chatToGPTMessage(
|
||||||
messages: PromptMessage[]
|
messages: PromptMessage[],
|
||||||
|
// TODO(@darkskygit): move this logic in interface refactoring
|
||||||
|
withAttachment: boolean = true
|
||||||
): Promise<[string | undefined, ChatMessage[], any]> {
|
): Promise<[string | undefined, ChatMessage[], any]> {
|
||||||
const system = messages[0]?.role === 'system' ? messages.shift() : undefined;
|
const system = messages[0]?.role === 'system' ? messages.shift() : undefined;
|
||||||
const schema = system?.params?.schema;
|
const schema = system?.params?.schema;
|
||||||
@@ -77,26 +79,31 @@ export async function chatToGPTMessage(
|
|||||||
contents.push({ type: 'text', text: content });
|
contents.push({ type: 'text', text: content });
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let attachment of attachments) {
|
if (withAttachment) {
|
||||||
let mimeType: string;
|
for (let attachment of attachments) {
|
||||||
if (typeof attachment === 'string') {
|
let mimeType: string;
|
||||||
mimeType =
|
if (typeof attachment === 'string') {
|
||||||
typeof mimetype === 'string'
|
mimeType =
|
||||||
? mimetype
|
typeof mimetype === 'string'
|
||||||
: await inferMimeType(attachment);
|
? mimetype
|
||||||
} else {
|
: await inferMimeType(attachment);
|
||||||
({ attachment, mimeType } = attachment);
|
|
||||||
}
|
|
||||||
if (SIMPLE_IMAGE_URL_REGEX.test(attachment)) {
|
|
||||||
if (mimeType.startsWith('image/')) {
|
|
||||||
contents.push({ type: 'image', image: attachment, mimeType });
|
|
||||||
} else {
|
} else {
|
||||||
const data = attachment.startsWith('data:')
|
({ attachment, mimeType } = attachment);
|
||||||
? await fetch(attachment).then(r => r.arrayBuffer())
|
}
|
||||||
: new URL(attachment);
|
if (SIMPLE_IMAGE_URL_REGEX.test(attachment)) {
|
||||||
contents.push({ type: 'file' as const, data, mimeType });
|
if (mimeType.startsWith('image/')) {
|
||||||
|
contents.push({ type: 'image', image: attachment, mimeType });
|
||||||
|
} else {
|
||||||
|
const data = attachment.startsWith('data:')
|
||||||
|
? await fetch(attachment).then(r => r.arrayBuffer())
|
||||||
|
: new URL(attachment);
|
||||||
|
contents.push({ type: 'file' as const, data, mimeType });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (!content.length) {
|
||||||
|
// temp fix for pplx
|
||||||
|
contents.push({ type: 'text', text: '[no content]' });
|
||||||
}
|
}
|
||||||
|
|
||||||
msgs.push({ role, content: contents } as ChatMessage);
|
msgs.push({ role, content: contents } as ChatMessage);
|
||||||
|
|||||||
Reference in New Issue
Block a user