From 3a73f19862cd03728748797c090e722dee4bb3cc Mon Sep 17 00:00:00 2001 From: Narendranath Gogineni Date: Wed, 15 Jan 2025 19:05:39 +0530 Subject: [PATCH] handle strict openai compliance for streaming thinking model responses from google --- src/providers/google-vertex-ai/chatComplete.ts | 14 +++++++++++--- src/providers/google/chatComplete.ts | 14 +++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/providers/google-vertex-ai/chatComplete.ts b/src/providers/google-vertex-ai/chatComplete.ts index 0557cc79e..70010cb34 100644 --- a/src/providers/google-vertex-ai/chatComplete.ts +++ b/src/providers/google-vertex-ai/chatComplete.ts @@ -798,15 +798,23 @@ export const GoogleChatCompleteStreamChunkTransform: ( if (generation.content?.parts[0]?.text) { if (generation.content.parts[0].thought) containsChainOfThoughtMessage = true; - let content: string = generation.content.parts[0]?.text; + + let content: string = + strictOpenAiCompliance && containsChainOfThoughtMessage + ? '' + : generation.content.parts[0]?.text; if (generation.content.parts[1]?.text) { - content += '\r\n\r\n' + generation.content.parts[1]?.text; + if (strictOpenAiCompliance) + content = generation.content.parts[1].text; + else content += '\r\n\r\n' + generation.content.parts[1]?.text; containsChainOfThoughtMessage = false; } else if ( containsChainOfThoughtMessage && !generation.content.parts[0]?.thought ) { - content = '\r\n\r\n' + content; + if (strictOpenAiCompliance) + content = generation.content.parts[0].text; + else content = '\r\n\r\n' + content; containsChainOfThoughtMessage = false; } message = { diff --git a/src/providers/google/chatComplete.ts b/src/providers/google/chatComplete.ts index 7b02776dc..8a84a9e12 100644 --- a/src/providers/google/chatComplete.ts +++ b/src/providers/google/chatComplete.ts @@ -581,15 +581,23 @@ export const GoogleChatCompleteStreamChunkTransform: ( if (generation.content?.parts[0]?.text) { if (generation.content.parts[0].thought) containsChainOfThoughtMessage = true; - let content: string = generation.content.parts[0]?.text; + + let content: string = + strictOpenAiCompliance && containsChainOfThoughtMessage + ? '' + : generation.content.parts[0]?.text; if (generation.content.parts[1]?.text) { - content += '\r\n\r\n' + generation.content.parts[1]?.text; + if (strictOpenAiCompliance) + content = generation.content.parts[1].text; + else content += '\r\n\r\n' + generation.content.parts[1]?.text; containsChainOfThoughtMessage = false; } else if ( containsChainOfThoughtMessage && !generation.content.parts[0]?.thought ) { - content = '\r\n\r\n' + content; + if (strictOpenAiCompliance) + content = generation.content.parts[0].text; + else content = '\r\n\r\n' + content; containsChainOfThoughtMessage = false; } message = {