diff --git a/action/index.cjs b/action/index.cjs index f199b2b..bbcf676 100644 --- a/action/index.cjs +++ b/action/index.cjs @@ -147441,6 +147441,7 @@ class Chat { console.time('code-review cost'); const prompt = this.generatePrompt(patch); const isReasoning = this.isReasoningModel; + const temperature = Number(process.env.temperature || 1); const res = await this.openai.chat.completions.create({ messages: [ { @@ -147450,7 +147451,7 @@ class Chat { ], model: this.model, ...(isReasoning ? {} : { - temperature: +(process.env.temperature || 0) || 1, + temperature: Number.isFinite(temperature) ? temperature : 1, top_p: +(process.env.top_p || 0) || 1, }), max_tokens: process.env.max_tokens ? +process.env.max_tokens : undefined, diff --git a/src/chat.ts b/src/chat.ts index 408cb1c..f506e9f 100644 --- a/src/chat.ts +++ b/src/chat.ts @@ -99,6 +99,7 @@ export class Chat { const prompt = this.generatePrompt(patch); const isReasoning = this.isReasoningModel; + const temperature = Number(process.env.temperature || 1); const res = await this.openai.chat.completions.create({ messages: [ @@ -109,7 +110,7 @@ export class Chat { ], model: this.model, ...(isReasoning ? {} : { - temperature: +(process.env.temperature || 0) || 1, + temperature: Number.isFinite(temperature) ? temperature : 1, top_p: +(process.env.top_p || 0) || 1, }), max_tokens: process.env.max_tokens ? +process.env.max_tokens : undefined, diff --git a/test/chat.test.ts b/test/chat.test.ts index 527534c..4df5697 100644 --- a/test/chat.test.ts +++ b/test/chat.test.ts @@ -218,6 +218,23 @@ describe("Chat", () => { expect(callArgs.response_format).toEqual({ type: "json_object" }); }); + test.each(["0", "0.0"])("preserves zero temperature %s", async (temperature) => { + process.env.MODEL = "gpt-4o"; + process.env.temperature = temperature; + const chat = new Chat("test-key"); + await chat.codeReview("@@ -1 +1 @@\n-old\n+new"); + expect(mockCreate.mock.calls[0][0].temperature).toBe(0); + }); + + test.each([undefined, "", "invalid"])("defaults temperature %s to one", async (temperature) => { + process.env.MODEL = "gpt-4o"; + if (temperature === undefined) delete process.env.temperature; + else process.env.temperature = temperature; + const chat = new Chat("test-key"); + await chat.codeReview("@@ -1 +1 @@\n-old\n+new"); + expect(mockCreate.mock.calls[0][0].temperature).toBe(1); + }); + test("returns empty review for empty patch", async () => { const chat = new Chat("test-key"); const result = await chat.codeReview("");