Skip to content

Commit 6ee118e

Browse files
authored
Merge pull request #86 from RooVetGit/fix-openai-compatible-streaming
Add 'Include stream options' checkbox for OpenAI-compatible providers
2 parents 34fac1c + 2cfd76c commit 6ee118e

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/api/providers/openai.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,24 @@ export class OpenAiHandler implements ApiHandler {
3232
}
3333
}
3434

35+
// Include stream_options for OpenAI Compatible providers if the checkbox is checked
3536
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
3637
const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
3738
{ role: "system", content: systemPrompt },
3839
...convertToOpenAiMessages(messages),
3940
]
40-
const stream = await this.client.chat.completions.create({
41+
const requestOptions: OpenAI.Chat.ChatCompletionCreateParams = {
4142
model: this.options.openAiModelId ?? "",
4243
messages: openAiMessages,
4344
temperature: 0,
4445
stream: true,
45-
stream_options: { include_usage: true },
46-
})
46+
}
47+
48+
if (this.options.includeStreamOptions ?? true) {
49+
requestOptions.stream_options = { include_usage: true }
50+
}
51+
52+
const stream = await this.client.chat.completions.create(requestOptions)
4753
for await (const chunk of stream) {
4854
const delta = chunk.choices[0]?.delta
4955
if (delta?.content) {

src/shared/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export interface ApiHandlerOptions {
3434
openAiNativeApiKey?: string
3535
azureApiVersion?: string
3636
openRouterUseMiddleOutTransform?: boolean
37+
includeStreamOptions?: boolean
38+
setAzureApiVersion?: boolean
3739
}
3840

3941
export type ApiConfiguration = ApiHandlerOptions & {

webview-ui/src/components/settings/ApiOptions.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,24 @@ const ApiOptions = ({ showModelOptions, apiErrorMessage, modelIdErrorMessage }:
445445
placeholder={"Enter Model ID..."}>
446446
<span style={{ fontWeight: 500 }}>Model ID</span>
447447
</VSCodeTextField>
448+
<div style={{ display: 'flex', alignItems: 'center' }}>
449+
<VSCodeCheckbox
450+
checked={apiConfiguration?.includeStreamOptions ?? true}
451+
onChange={(e: any) => {
452+
const isChecked = e.target.checked
453+
setApiConfiguration({
454+
...apiConfiguration,
455+
includeStreamOptions: isChecked
456+
})
457+
}}>
458+
Include stream options
459+
</VSCodeCheckbox>
460+
<span
461+
className="codicon codicon-info"
462+
title="Stream options are for { include_usage: true }. Some providers may not support this option."
463+
style={{ marginLeft: '5px', cursor: 'help' }}
464+
></span>
465+
</div>
448466
<VSCodeCheckbox
449467
checked={azureApiVersionSelected}
450468
onChange={(e: any) => {

0 commit comments

Comments
 (0)