Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#477 Estimate tokens usage with streaming completions #507

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/chatgpt-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,17 @@ export class ChatGPTAPI {
result.id = response.id
}

if (response?.choices?.length) {
if (response.choices?.length) {
const delta = response.choices[0].delta
result.delta = delta.content
if (delta?.content) result.text += delta.content
result.detail = response

if (delta.role) {
result.role = delta.role
}

result.detail = response
transitive-bullshit marked this conversation as resolved.
Show resolved Hide resolved

onProgress?.(result)
}
} catch (err) {
Expand Down Expand Up @@ -286,7 +287,16 @@ export class ChatGPTAPI {
}
}
}
).then((message) => {
).then(async (message) => {
if (message.detail && !message.detail.usage) {
const promptTokens = numTokens
const completionTokens = await this._getTokenCount(message.text)
message.detail.usage = {
prompt_tokens: promptTokens,
completion_tokens: completionTokens,
total_tokens: promptTokens + completionTokens
}
}
return this._upsertMessage(message).then(() => message)
})

Expand Down
14 changes: 13 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,25 @@ export type SendMessageBrowserOptions = {
abortSignal?: AbortSignal
}

interface CreateChatCompletionStreamResponse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like to export all interfaces for ease of access by libs

extends openai.CreateChatCompletionDeltaResponse {
usage: CreateCompletionStreamResponseUsage
}

interface CreateCompletionStreamResponseUsage
extends openai.CreateCompletionResponseUsage {
estimated: true
}

export interface ChatMessage {
id: string
text: string
role: Role
name?: string
delta?: string
detail?: any
detail?:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually should not be nullable. In the current logic it can never be undefined in final message object. But I could not find a good way how to type it correctly with current mutation-like codestyle

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine to leave it nullable for now. appreciate the note

| openai.CreateChatCompletionResponse
| CreateChatCompletionStreamResponse

// relevant for both ChatGPTAPI and ChatGPTUnofficialProxyAPI
parentMessageId?: string
Expand Down