fix anthropic openai-adapter
This commit is contained in:
parent
fce15b915e
commit
c03e65cb0a
5 changed files with 89 additions and 15 deletions
|
|
@ -61,19 +61,25 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
}
|
||||
return {
|
||||
...message,
|
||||
content: message.content.map((part) => {
|
||||
if (part.type === "text") {
|
||||
return part;
|
||||
}
|
||||
return {
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: "image/jpeg",
|
||||
data: part.image_url.url.split(",")[1],
|
||||
},
|
||||
};
|
||||
}),
|
||||
content: message.content
|
||||
.map((part) => {
|
||||
if (part.type === "text") {
|
||||
if ((part.text?.trim() ?? "") === "") {
|
||||
return null;
|
||||
}
|
||||
return part;
|
||||
}
|
||||
return {
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: "image/jpeg",
|
||||
// @ts-ignore
|
||||
data: part.image_url.url.split(",")[1],
|
||||
},
|
||||
};
|
||||
})
|
||||
.filter((x) => x !== null),
|
||||
};
|
||||
});
|
||||
return messages;
|
||||
|
|
@ -112,6 +118,7 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
message: {
|
||||
role: "assistant",
|
||||
content: completion.content[0].text,
|
||||
refusal: null,
|
||||
},
|
||||
index: 0,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
frequencyPenalty: body.frequency_penalty ?? undefined,
|
||||
presencePenalty: body.presence_penalty ?? undefined,
|
||||
stop:
|
||||
typeof body.stop === "string" ? [body.stop] : body.stop ?? undefined,
|
||||
typeof body.stop === "string" ? [body.stop] : (body.stop ?? undefined),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -101,6 +101,7 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
message: {
|
||||
role: "assistant",
|
||||
content: choice.message?.content ?? null,
|
||||
refusal: null,
|
||||
},
|
||||
})),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -93,6 +93,7 @@ export class CohereApi implements BaseLlmApi {
|
|||
message: {
|
||||
role: "assistant",
|
||||
content: data.text,
|
||||
refusal: null,
|
||||
},
|
||||
index: 0,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -89,7 +89,8 @@ export class GeminiApi implements BaseLlmApi {
|
|||
parts:
|
||||
typeof msg.content === "string"
|
||||
? [{ text: msg.content }]
|
||||
: msg.content.map(this._oaiPartToGeminiPart),
|
||||
: // @ts-ignore
|
||||
msg.content.map(this._oaiPartToGeminiPart),
|
||||
};
|
||||
})
|
||||
.filter((c) => c !== null);
|
||||
|
|
@ -130,6 +131,7 @@ export class GeminiApi implements BaseLlmApi {
|
|||
message: {
|
||||
role: "assistant",
|
||||
content: completion,
|
||||
refusal: null,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
|
|
|||
|
|
@ -120,6 +120,69 @@ function testConfig(config: LlmApiConfig, chatOnly: boolean = false) {
|
|||
expect(completion.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("should successfully stream multi-part chat with empty text", async () => {
|
||||
const stream = api.chatCompletionStream({
|
||||
model: config.model,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "Hello! Who are you?",
|
||||
},
|
||||
{
|
||||
// @ts-ignore
|
||||
type: "text",
|
||||
text: "",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
stream: true,
|
||||
});
|
||||
let completion = "";
|
||||
for await (const result of stream) {
|
||||
completion += result.choices[0].delta.content ?? "";
|
||||
|
||||
expect(result.choices.length).toBeGreaterThan(0);
|
||||
}
|
||||
expect(completion.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test.skip("should successfully stream multi-part chat with image", async () => {
|
||||
const stream = api.chatCompletionStream({
|
||||
model: config.model,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "Hello! Who are you?",
|
||||
},
|
||||
{
|
||||
// @ts-ignore
|
||||
type: "image_url",
|
||||
imageUrl: {
|
||||
url: "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Image_created_with_a_mobile_phone.png/1280px-Image_created_with_a_mobile_phone.png",
|
||||
detail: "low",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
stream: true,
|
||||
});
|
||||
let completion = "";
|
||||
for await (const result of stream) {
|
||||
completion += result.choices[0].delta.content ?? "";
|
||||
|
||||
expect(result.choices.length).toBeGreaterThan(0);
|
||||
}
|
||||
expect(completion.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("should successfully non-stream chat", async () => {
|
||||
const response = await api.chatCompletionNonStream({
|
||||
model: config.model,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue