Skip to content

Commit

Permalink
handle strict openai compliance for streaming thinking model response…
Browse files Browse the repository at this point in the history
…s from google
  • Loading branch information
narengogi committed Jan 15, 2025
1 parent 3993321 commit 3a73f19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/providers/google-vertex-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
14 changes: 11 additions & 3 deletions src/providers/google/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 3a73f19

Please sign in to comment.