Skip to content

Commit 7197e4c

Browse files
authored
Merge pull request #20 from ferenci84/add_temperature
add OPENAI_TEMPERATURE configuration option
2 parents 4aee0bc + ee9c9dd commit 7197e4c

5 files changed

Lines changed: 23 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ In the VSCode settings, locate the "ai-commit" configuration options and configu
6767
| AZURE_API_VERSION | string | None | No | AZURE_API_VERSION |
6868
| AI_COMMIT_LANGUAGE | string | en | Yes | Supports 19 languages |
6969
| SYSTEM_PROMPT | string | None | No | Custom system prompt |
70+
| OPENAI_TEMPERATURE | number | 0.7 | No | Controls randomness in the output. Range: 0-2. Lower values: more focused, Higher values: more creative |
7071

7172
## ⌨️ Local Development
7273

README.zh_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
| AZURE_API_VERSION | string | None || AZURE_API_VERSION |
6868
| AI_COMMIT_LANGUAGE | string | en || 支持 19 种语言 |
6969
| SYSTEM_PROMPT | string | None || 自定义系统提示词 |
70+
| OPENAI_TEMPERATURE | number | 0.7 || 控制输出的随机性。范围:0-2。较低的值:更加集中,较高的值:更有创造性 |
7071

7172
## ⌨️ 本地开发
7273

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@
117117
"type": "string",
118118
"default": "",
119119
"description": "Custom system prompt for generating commit messages"
120+
},
121+
"ai-commit.OPENAI_TEMPERATURE": {
122+
"type": "number",
123+
"default": 0.7,
124+
"minimum": 0,
125+
"maximum": 2,
126+
"description": "OpenAI temperature setting (0-2). Higher values make output more random, lower values more deterministic."
120127
}
121128
},
122129
"title": "AI Commit"

src/config.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import { createOpenAIApi } from './openai-utils';
1010
* @property {string} AZURE_API_VERSION - The version of Azure API.
1111
* @property {string} AI_COMMIT_LANGUAGE - The language for AI commit messages.
1212
* @property {string} SYSTEM_PROMPT - The system prompt for generating commit messages.
13+
* @property {string} OPENAI_TEMPERATURE - The temperature setting for OpenAI API.
1314
*/
1415
export enum ConfigKeys {
1516
OPENAI_API_KEY = 'OPENAI_API_KEY',
1617
OPENAI_BASE_URL = 'OPENAI_BASE_URL',
1718
OPENAI_MODEL = 'OPENAI_MODEL',
1819
AZURE_API_VERSION = 'AZURE_API_VERSION',
1920
AI_COMMIT_LANGUAGE = 'AI_COMMIT_LANGUAGE',
20-
SYSTEM_PROMPT = 'AI_COMMIT_SYSTEM_PROMPT'
21+
SYSTEM_PROMPT = 'AI_COMMIT_SYSTEM_PROMPT',
22+
OPENAI_TEMPERATURE = 'OPENAI_TEMPERATURE'
2123
}
2224

2325
/**
@@ -34,9 +36,9 @@ export class ConfigurationManager {
3436
this.disposable = vscode.workspace.onDidChangeConfiguration((event) => {
3537
if (event.affectsConfiguration('ai-commit')) {
3638
this.configCache.clear();
37-
38-
if (event.affectsConfiguration('ai-commit.OPENAI_BASE_URL') ||
39-
event.affectsConfiguration('ai-commit.OPENAI_API_KEY')) {
39+
40+
if (event.affectsConfiguration('ai-commit.OPENAI_BASE_URL') ||
41+
event.affectsConfiguration('ai-commit.OPENAI_API_KEY')) {
4042
this.updateModelList();
4143
}
4244
}
@@ -69,14 +71,14 @@ export class ConfigurationManager {
6971
try {
7072
const openai = createOpenAIApi();
7173
const models = await openai.models.list();
72-
74+
7375
// Save available models to extension state
7476
await this.context.globalState.update('availableModels', models.data.map(model => model.id));
75-
77+
7678
// Get the current selected model
7779
const config = vscode.workspace.getConfiguration('ai-commit');
7880
const currentModel = config.get<string>('OPENAI_MODEL');
79-
81+
8082
// If the current selected model is not in the available list, set it to the default value
8183
const availableModels = models.data.map(model => model.id);
8284
if (!availableModels.includes(currentModel)) {

src/openai-utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ export function createOpenAIApi() {
5353
*/
5454
export async function ChatGPTAPI(messages: ChatCompletionMessageParam[]) {
5555
const openai = createOpenAIApi();
56-
const model = ConfigurationManager.getInstance().getConfig<string>(
57-
ConfigKeys.OPENAI_MODEL
58-
);
56+
const configManager = ConfigurationManager.getInstance();
57+
const model = configManager.getConfig<string>(ConfigKeys.OPENAI_MODEL);
58+
const temperature = configManager.getConfig<number>(ConfigKeys.OPENAI_TEMPERATURE, 0.7);
5959

6060
const completion = await openai.chat.completions.create({
6161
model,
62-
messages: messages as ChatCompletionMessageParam[]
62+
messages: messages as ChatCompletionMessageParam[],
63+
temperature
6364
});
6465

6566
return completion.choices[0]!.message?.content;

0 commit comments

Comments
 (0)