Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Fixes #429 (ignore chunks with empty choices array) (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlovandenhoek authored Jan 1, 2024
1 parent 013a3e5 commit 8145c6a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ You'll need to have API access to GPT-4 to run Mentat. There are a few options t

Mentat also works with the Azure OpenAI API. To use the Azure API, provide the `AZURE_OPENAI_ENDPOINT` (`https://<your-instance-name>.openai.azure.com/`) and `AZURE_OPENAI_KEY` environment variables instead of `OPENAI_API_KEY`.

In addition, Mentat uses the `gpt-4-1106-preview` by default. On Azure, this model is available under a different name: `gpt-4-1106-Preview` (with a capital P). To use it, override the default model as described in [configuration.md](docs/configuration.md).

> **_Important:_** Due to changes in the OpenAI Python SDK, you can no longer use `OPENAI_API_BASE` to access the Azure API with Mentat.
## Configuration
Expand Down
3 changes: 3 additions & 0 deletions mentat/cost_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ async def response_logger_wrapper(
full_response = ""
start_time = default_timer()
async for chunk in response:
# On Azure OpenAI, the first chunk streamed may contain only metadata relating to content filtering.
if len(chunk.choices) == 0:
continue
full_response += chunk.choices[0].delta.content or ""
yield chunk
time_elapsed = default_timer() - start_time
Expand Down
4 changes: 3 additions & 1 deletion mentat/llm_api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:

# Ensures that each chunk will have at most one newline character
def chunk_to_lines(chunk: ChatCompletionChunk) -> list[str]:
content = chunk.choices[0].delta.content
content = None
if len(chunk.choices) > 0:
content = chunk.choices[0].delta.content
return ("" if content is None else content).splitlines(keepends=True)


Expand Down

0 comments on commit 8145c6a

Please sign in to comment.