Skip to content

Commit

Permalink
Refactor chatGpt.js for progress messages in createChatCompletion fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
savoygu committed Aug 15, 2023
1 parent 584aa2b commit 544e62a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { FetchOptions, FetchRequest } from 'ohmyfetch'
import { fetch } from 'ohmyfetch'
import ISO6391 from 'iso-639-1'
import { bgYellow, yellow } from 'kolorist'
import { isUndefined } from 'lodash-es'
import type { ValidConfig } from './config.js'
import conversations from './conversation.js'
import type { TranslatorOptions } from './translator.js'
Expand Down Expand Up @@ -75,8 +76,15 @@ export async function createChatCompletion(words: string[], options: ValidConfig
timeoutMs: options.timeout,
onProgress: options.stream
? (progress) => {
if (progress.delta)
process.stdout.write(progress.delta)
if (!progress.text)
process.stdout.write('\n ')
if (progress.delta) {
const indent = progress.delta.endsWith('\n')
process.stdout.write(progress.delta + (indent ? ' ' : ''))
}
else if (isUndefined(progress.delta)) {
process.stdout.write('\n')
}
}
: undefined,
})
Expand Down

0 comments on commit 544e62a

Please sign in to comment.