Skip to content

Commit

Permalink
Merge pull request #103 from Portkey-AI/fix/anthropic-messages-stream…
Browse files Browse the repository at this point in the history
…-parsing

fix anthropic messages stream event parsing
  • Loading branch information
VisargD authored Jan 9, 2024
2 parents 19894b2 + b5f0d42 commit 0057433
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/providers/anthropic/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ANTHROPIC } from "../../globals";
import { Params, Message } from "../../types/requestBody";
import { ChatCompletionResponse, ErrorResponse, ProviderConfig } from "../types";

Expand Down Expand Up @@ -118,7 +119,7 @@ export const AnthropicChatCompleteResponseTransform: (response: AnthropicChatCom
param: null,
code: null
},
provider: "anthropic"
provider: ANTHROPIC
} as ErrorResponse;
}

Expand All @@ -128,7 +129,7 @@ export const AnthropicChatCompleteResponseTransform: (response: AnthropicChatCom
object: "chat_completion",
created: Math.floor(Date.now() / 1000),
model: response.model,
provider: "anthropic",
provider: ANTHROPIC,
choices: [
{
message: {"role": "assistant", content: response.content[0].text},
Expand All @@ -147,12 +148,12 @@ export const AnthropicChatCompleteResponseTransform: (response: AnthropicChatCom
param: null,
code: null
},
provider: "anthropic"
provider: ANTHROPIC
} as ErrorResponse;
}


export const AnthropicChatCompleteStreamChunkTransform: (response: string) => string | undefined = (responseChunk) => {
export const AnthropicChatCompleteStreamChunkTransform: (response: string, fallbackId: string) => string | undefined = (responseChunk, fallbackId) => {
let chunk = responseChunk.trim();
if (
chunk.startsWith("event: ping") ||
Expand All @@ -167,18 +168,19 @@ export const AnthropicChatCompleteStreamChunkTransform: (response: string) => st
return "data: [DONE]\n\n"
}

chunk = chunk.replace(/^event: completion[\r\n]*/, "");
chunk = chunk.replace(/^event: content_block_delta[\r\n]*/, "");
chunk = chunk.replace(/^event: message_delta[\r\n]*/, "");
chunk = chunk.replace(/^data: /, "");
chunk = chunk.trim();


const parsedChunk: AnthropicChatCompleteStreamResponse = JSON.parse(chunk);
return `data: ${JSON.stringify({
id: "id",
id: fallbackId,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "",
provider: "anthropic",
provider: ANTHROPIC,
choices: [
{
delta: {
Expand Down

0 comments on commit 0057433

Please sign in to comment.