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

fix: improve completions to send a final chunk with usage details #2336

Merged
merged 4 commits into from
Aug 12, 2024

Conversation

drbh
Copy link
Collaborator

@drbh drbh commented Jul 30, 2024

This PR fixes an issue with the /v1/completions endpoint where the final messages was not treated as a CompletionFinal. This resulted in all of the streaming responses retuning empty usage information and no finish_reason. Now if the chunk is final we handle it accordingly.

Repro

from openai import OpenAI
import os

client = OpenAI(
    base_url="http://localhost:3000/v1",
    api_key=os.getenv("HF_TOKEN", "YOUR_API_KEY"),
)

i = 1337

chat_completion = client.completions.create(
    model="mistralai/Mistral-7B-Instruct-v0.1",
    prompt="What are three words that describe the Python programming language?",
    max_tokens=20,
    stream=True,
    seed=i,
)

# iterate over the stream of messages
for message in chat_completion:
    print(message)

response

Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text='\n')], created=1722373608, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text='Object')], created=1722373608, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text='-')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text='oriented')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text=',')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text=' Vers')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text='atile')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text=',')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text=' Easy')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text=' to')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text=' learn')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='', index=0, logprobs=None, text='.')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=None)
Completion(id='', choices=[CompletionChoice(finish_reason='eos_token', index=0, logprobs=None, text='</s>')], created=1722373609, model='mistralai/Mistral-7B-Instruct-v0.1', object='text_completion', system_fingerprint='2.2.1-dev0-native', usage=CompletionUsage(completion_tokens=13, prompt_tokens=12, total_tokens=25))

@drbh drbh force-pushed the support-completions-final-chunk branch 2 times, most recently from 8f347f7 to 3f83c46 Compare August 9, 2024 19:41
@drbh drbh force-pushed the support-completions-final-chunk branch from 3f83c46 to 515cd66 Compare August 9, 2024 19:46
Copy link
Collaborator

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

LGTM

@Narsil Narsil merged commit 30395b0 into main Aug 12, 2024
9 checks passed
@Narsil Narsil deleted the support-completions-final-chunk branch August 12, 2024 15:26
yuanwu2017 pushed a commit to yuanwu2017/tgi-gaudi that referenced this pull request Sep 26, 2024
…ggingface#2336)

* fix: improve completions to send a final chunk with usage details

* fix: include finish reason string

* fix: remove dev debug trait and unneeded mut

* fix: update openapi schema
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.

2 participants