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

add ChatCompletionStream Usage return #215

Merged
merged 1 commit into from
Apr 1, 2023

Conversation

nasa1024
Copy link
Contributor

add ChatCompletionStream Usage return

@codecov
Copy link

codecov bot commented Mar 31, 2023

Codecov Report

❗ No coverage uploaded for pull request base (master@8e3a046). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master     #215   +/-   ##
=========================================
  Coverage          ?   71.94%           
=========================================
  Files             ?       21           
  Lines             ?      581           
  Branches          ?        0           
=========================================
  Hits              ?      418           
  Misses            ?      124           
  Partials          ?       39           
Impacted Files Coverage Δ
chat_stream.go 88.00% <ø> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!

@sashabaranov sashabaranov merged commit b542086 into sashabaranov:master Apr 1, 2023
@collinvandyck
Copy link

Thank you this is what I was looking for!

@collinvandyck
Copy link

I'm not actually seeing any usage data come back in the streaming response. I added a roundtripper to log request/response data and it looks like this (formatting mine):

POST https://api.openai.com/v1/chat/completions
HTTP/2.0 200 OK
data: {
  "id": "chatcmpl-70smbYYzwcwpb19BkzLb3NFlvkIsP",
  "object": "chat.completion.chunk",
  "created": 1680444941,
  "model": "gpt-3.5-turbo-0301",
  "choices": [
    {
      "delta": {
        "role": "assistant"
      },
      "index": 0,
      "finish_reason": null
    }
  ]
}
data: {
  "id": "chatcmpl-70smbYYzwcwpb19BkzLb3NFlvkIsP",
  "object": "chat.completion.chunk",
  "created": 1680444941,
  "model": "gpt-3.5-turbo-0301",
  "choices": [
    {
      "delta": {
        "content": "How"
      },
      "index": 0,
      "finish_reason": null
    }
  ]
}

Is there something I need to do to signal to openai that usage data should be returned?

@sashabaranov
Copy link
Owner

@collinvandyck please upgrade to 1.5.8 go get -u github.com/sashabaranov/go-openai@v1.5.8

@collinvandyck
Copy link

Hey @sashabaranov ! I'm seeing this behavior on 1.5.8, go.mod:

github.com/sashabaranov/go-openai v1.5.8-0.20230401160622-b542086cbb22

After giving it a spin I saw that usage was always blank in the resp returned by Recv, so that's why I added the request/response logging to verify it wasn't actually coming back in the http response.

I'm kicking off the chat completion stream like this:

req := openai.ChatCompletionRequest{
    Model:    c.model,
    Messages: messages,
    Stream:   true,
}
resp, err := c.openai.CreateChatCompletionStream(ctx, req)
if err != nil {
    return nil, fmt.Errorf("stream: %w", err)
}

@collinvandyck
Copy link

@nasa1024
Copy link
Contributor Author

nasa1024 commented Apr 3, 2023

too embarrassed
@collinvandyck yes, /chat/completions when using stream=True will not return Usage, i will fix it

@collinvandyck
Copy link

It would be neat for go-openai to include a tokenizer to do this, but it seems like a pretty big lift. It looks like most of the folks that are doing this are calling out to a rust tokenizer since there's not an official go tokenizer. Not suggesting that you should do this, but it was surprising to me that there wasn't any official way to do this using openai's libraries in Go.

@nasa1024
Copy link
Contributor Author

nasa1024 commented Apr 3, 2023

It would be neat for go-openai to include a tokenizer to do this, but it seems like a pretty big lift. It looks like most of the folks that are doing this are calling out to a rust tokenizer since there's not an official go tokenizer. Not suggesting that you should do this, but it was surprising to me that there wasn't any official way to do this using openai's libraries in Go.

sir, see 223

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants