Skip to content
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
13 changes: 10 additions & 3 deletions core/llm/llms/OpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,18 @@ class OpenAI extends BaseLLM {
}

protected getMaxStopWords(): number {
const url = new URL(this.apiBase!);

if (this.maxStopWords !== undefined) {
return this.maxStopWords;
} else if (url.host === "api.deepseek.com") {
}

// apiBase can legitimately be undefined (e.g. continue-proxy, whose
// defaultOptions don't carry one), so guard against `new URL(undefined)`.
if (!this.apiBase) {
return Infinity;
}

const url = new URL(this.apiBase);
if (url.host === "api.deepseek.com") {
return 16;
} else if (
url.port === "1337" ||
Expand Down
27 changes: 27 additions & 0 deletions core/llm/llms/OpenAI.vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,31 @@ describe("OpenAI", () => {
},
});
});

describe("getMaxStopWords", () => {
test("returns Infinity instead of throwing when apiBase is undefined", () => {
const openai = new OpenAI({
apiKey: "test-api-key",
model: "gpt-4",
apiBase: "https://api.openai.com/v1/",
});
// continue-proxy and similar providers can leave apiBase undefined
(openai as any).apiBase = undefined;

expect(() => (openai as any).getMaxStopWords()).not.toThrow();
expect((openai as any).getMaxStopWords()).toBe(Infinity);
});

test("honors maxStopWords even when apiBase is undefined", () => {
const openai = new OpenAI({
apiKey: "test-api-key",
model: "gpt-4",
apiBase: "https://api.openai.com/v1/",
});
(openai as any).apiBase = undefined;
(openai as any).maxStopWords = 7;

expect((openai as any).getMaxStopWords()).toBe(7);
});
});
});
Loading