Skip to content

Commit

Permalink
Structured object streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Cerlancism committed Sep 28, 2024
1 parent 4ca9bc0 commit e7bfdd3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/translatorStructuredArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export class TranslatorStructuredArray extends TranslatorStructuredBase
...this.options.createChatCompletionRequest,
stream: this.options.createChatCompletionRequest.stream,
max_tokens
}, { structure: structuredArray, name: "translation_array" })
}, {
structure: structuredArray,
name: "translation_array"
})

// console.log("[TranslatorStructuredArray]", output.choices[0].message.content)

Expand All @@ -62,16 +65,18 @@ export class TranslatorStructuredArray extends TranslatorStructuredBase
console.log("[TranslatorStructuredArray] Refusal Fallback", this.options.fallbackModel)
const requestOptions = { ...this.options.createChatCompletionRequest }
requestOptions.model = this.options.fallbackModel
const fallBackOutput = await this.services.openai.beta.chat.completions.parse({
const fallBackOutput = await this.streamParse({
messages,
...requestOptions,
stream: false,
response_format: zodResponseFormat(structuredArray, "translation_array"),
max_tokens
}, {
structure: structuredArray,
name: "translation_array"
})
translation = fallBackOutput.choices[0].message
}

if (translation.refusal)
{
return [translation.refusal]
Expand Down
11 changes: 7 additions & 4 deletions src/translatorStructuredObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ export class TranslatorStructuredObject extends TranslatorStructuredBase

await this.services.cooler?.cool()

const output = await this.services.openai.beta.chat.completions.parse({
const output = await this.streamParse({
messages,
...this.options.createChatCompletionRequest,
stream: false,
response_format: zodResponseFormat(translationBatch, "translation_object"),
stream: this.options.createChatCompletionRequest.stream,
max_tokens
}, {
structure: translationBatch,
name: "translation_object"
})

// console.log("[TranslatorStructuredObject]", output.choices[0].message.content)
Expand Down Expand Up @@ -157,7 +159,8 @@ export class TranslatorStructuredObject extends TranslatorStructuredBase
{
const output = {}

for (let index = 0; index < sourceLines.length; index++) {
for (let index = 0; index < sourceLines.length; index++)
{
const source = sourceLines[index];
const transform = transformLines[index]
output[source] = transform
Expand Down

0 comments on commit e7bfdd3

Please sign in to comment.