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 logprobs support with OpenAI API chat completions #3423

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 19 additions & 1 deletion docs/openai_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ completion = openai.chat.completions.create(
print(completion.choices[0].message.content)
```

### Logprobs

Logprobs are supported with the OpenAI API:

```python

# create a chat completion
completion = openai.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "Hello! What is your name?"}],
log_probs=True
)
# print the completion
print(completion.choices[0].logprobs)
```

### Streaming

Streaming is also supported. See [test_openai_api.py](../tests/test_openai_api.py). If your api server is behind a proxy you'll need to turn off buffering, you can do so in Nginx by setting `proxy_buffering off;` in the location block for the proxy.

### cURL
Expand Down Expand Up @@ -146,7 +164,7 @@ export FASTCHAT_WORKER_API_EMBEDDING_BATCH_SIZE=1
## Todos
Some features to be implemented:

- [ ] Support more parameters like `logprobs`, `logit_bias`, `user`, `presence_penalty` and `frequency_penalty`
- [ ] Support more parameters like `logit_bias`, `user`, `presence_penalty` and `frequency_penalty`
- [ ] Model details (permissions, owner and create time)
- [ ] Edits API
- [ ] Rate Limitation Settings
2 changes: 2 additions & 0 deletions fastchat/protocol/openai_api_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class ChatCompletionRequest(BaseModel):
]
temperature: Optional[float] = 0.7
top_p: Optional[float] = 1.0
logprobs: Optional[int] = None
top_k: Optional[int] = -1
n: Optional[int] = 1
max_tokens: Optional[int] = None
Expand All @@ -81,6 +82,7 @@ class ChatMessage(BaseModel):

class ChatCompletionResponseChoice(BaseModel):
index: int
logprobs: Optional[LogProbs] = None
message: ChatMessage
finish_reason: Optional[Literal["stop", "length"]] = None

Expand Down
4 changes: 3 additions & 1 deletion fastchat/serve/openai_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ async def get_gen_params(
frequency_penalty: Optional[float],
max_tokens: Optional[int],
echo: Optional[bool],
logprobs: Optional[int] = None,
logprobs: Optional[int],
stop: Optional[Union[str, List[str]]],
best_of: Optional[int] = None,
use_beam_search: Optional[bool] = None,
Expand Down Expand Up @@ -431,6 +431,7 @@ async def create_chat_completion(request: ChatCompletionRequest):
frequency_penalty=request.frequency_penalty,
max_tokens=request.max_tokens,
echo=False,
logprobs=request.logprobs,
stop=request.stop,
)

Expand Down Expand Up @@ -472,6 +473,7 @@ async def create_chat_completion(request: ChatCompletionRequest):
ChatCompletionResponseChoice(
index=i,
message=ChatMessage(role="assistant", content=content["text"]),
logprobs=create_openai_logprobs(content.get("logprobs", None)),
finish_reason=content.get("finish_reason", "stop"),
)
)
Expand Down
Loading