From e1a730f73004a707eaae31e872ea42f3bb786375 Mon Sep 17 00:00:00 2001 From: Yoshiki Takagi Date: Sat, 3 Jun 2023 14:38:44 +0900 Subject: [PATCH] Add model option to config --- README.md | 5 +++++ commands/config/set.ts | 2 +- commands/main.ts | 7 +++---- commands/main_test.ts | 2 +- lib/get_commit_message_suggestion.ts | 3 ++- lib/load_config.ts | 6 +----- types.d.ts | 7 +++++++ 7 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 types.d.ts diff --git a/README.md b/README.md index 3f86c91..378e111 100644 --- a/README.md +++ b/README.md @@ -57,5 +57,10 @@ $ cg config list api_key = sk-HogehogeXXX ``` +| Key | Description | +|---------|--------------------------------------------------------| +| api_key | API key used for authentication with OpenAI | +| model | model name for OpenAI (e.g., `gpt-3.5-turbo`, `gpt-4`) | + ## License The source code is licensed MIT. diff --git a/commands/config/set.ts b/commands/config/set.ts index f2e78c0..79aa00b 100644 --- a/commands/config/set.ts +++ b/commands/config/set.ts @@ -6,7 +6,7 @@ export class SetCommand extends Command { constructor() { super(); this.description("Update configuration with a value for the given key") - .type("config", new EnumType(["api_key"])) + .type("config", new EnumType(["api_key", "model"])) .arguments(" ") .action(async (_, key, value) => { const config = await loadConfig(); diff --git a/commands/main.ts b/commands/main.ts index 2833962..4569f1e 100644 --- a/commands/main.ts +++ b/commands/main.ts @@ -20,9 +20,7 @@ export class MainCommand extends Command { .env("OPENAI_ACCESS_TOKEN=", "OPENAI ACCESS TOKEN") .env("OPENAI_API_KEY=", "OPENAI API KEY") .type("model-type", modelType) - .option("-m --model ", "Model Type", { - default: "gpt-3.5-turbo" as const, - }) + .option("-m --model ", "Model name") .action(async (options) => { const config = await loadConfig(); const apiKey = options.openaiAccessToken || options.openaiApiKey || @@ -35,10 +33,11 @@ export class MainCommand extends Command { } const diffText = await getDiffText(); + const model = options.model || config.model || "gpt-3.5-turbo"; const commitMessageSuggestion = await getCommitMessageSuggestion( apiKey, diffText, - options.model, + model, ); console.log(commitMessageSuggestion); diff --git a/commands/main_test.ts b/commands/main_test.ts index bc39c50..b0a242d 100644 --- a/commands/main_test.ts +++ b/commands/main_test.ts @@ -13,7 +13,7 @@ Deno.test("help command", () => { Options: - -m, --model - Model Type (Default: "gpt-3.5-turbo", Values: "gpt-3.5-turbo", "gpt-4") + -m, --model - Model name (Values: "gpt-3.5-turbo", "gpt-4") Commands: diff --git a/lib/get_commit_message_suggestion.ts b/lib/get_commit_message_suggestion.ts index aedd5df..f6050a3 100644 --- a/lib/get_commit_message_suggestion.ts +++ b/lib/get_commit_message_suggestion.ts @@ -1,5 +1,6 @@ import { OpenAI } from "../deps.ts"; import { ChatCompletion } from "https://deno.land/x/openai@1.3.0/mod.ts"; +import { Model } from "../types.d.ts"; interface CustomChatCompletion extends ChatCompletion { error: { @@ -10,7 +11,7 @@ interface CustomChatCompletion extends ChatCompletion { export const getCommitMessageSuggestion = async ( openaiAccessToken: string, diffText: string, - model: "gpt-3.5-turbo" | "gpt-4", + model: Model, ): Promise => { const openAI = new OpenAI(openaiAccessToken); diff --git a/lib/load_config.ts b/lib/load_config.ts index 4f08ff4..aa4aabf 100644 --- a/lib/load_config.ts +++ b/lib/load_config.ts @@ -1,9 +1,5 @@ import { exists, parse } from "../deps.ts"; - -type Config = { - api_key?: string; - [key: string]: string | undefined; -}; +import { Config } from "../types.d.ts"; export const loadConfig = async (): Promise => { const filePath = `${Deno.env.get("HOME")}/.config/commit_genius/config.toml`; diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..7b6f945 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,7 @@ +export type Model = "gpt-3.5-turbo" | "gpt-4"; + +export type Config = { + api_key?: string; + model?: Model; + [key: string]: string | undefined; +};