Skip to content

Allow the OpenAI URL to be configurable using an env var #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ jobs:
rust: [nightly, stable]
runs-on: ${{ matrix.os }}
continue-on-error: false
services:
fake-openai:
image: fake-openai:latest
ports:
- 8080:8080
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down Expand Up @@ -91,3 +96,4 @@ jobs:
cargo test --release
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_URL: http://localhost:8080
14 changes: 11 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ const DEFAULT_MAX_COMMIT_LENGTH: i64 = 72;
const DEFAULT_MAX_TOKENS: i64 = 2024;
const DEFAULT_MODEL: &str = "gpt-4o-mini";
const DEFAULT_API_KEY: &str = "<PLACE HOLDER FOR YOUR API KEY>";
const DEFAULT_OPENAI_HOST: &str = "https://api.openai.com/v1";

#[derive(Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
pub struct App {
pub openai_api_key: Option<String>,
pub model: Option<String>,
pub max_tokens: Option<usize>,
pub max_commit_length: Option<usize>,
pub timeout: Option<usize>
pub timeout: Option<usize>,
pub openai_host: Option<String>
}

#[derive(Debug)]
Expand All @@ -45,10 +47,10 @@ impl ConfigPaths {
}

fn ensure_exists(&self) -> Result<()> {
if !self.dir.exists() {
if (!self.dir.exists()) {
std::fs::create_dir_all(&self.dir).with_context(|| format!("Failed to create config directory at {:?}", self.dir))?;
}
if !self.file.exists() {
if (!self.file.exists()) {
File::create(&self.file).with_context(|| format!("Failed to create config file at {:?}", self.file))?;
}
Ok(())
Expand All @@ -69,6 +71,7 @@ impl App {
.set_default("max_tokens", DEFAULT_MAX_TOKENS)?
.set_default("model", DEFAULT_MODEL)?
.set_default("openai_api_key", DEFAULT_API_KEY)?
.set_default("openai_host", DEFAULT_OPENAI_HOST)?
.build()?;

config
Expand Down Expand Up @@ -104,6 +107,11 @@ impl App {
self.save_with_message("openai-api-key")
}

pub fn update_openai_host(&mut self, value: String) -> Result<()> {
self.openai_host = Some(value);
self.save_with_message("openai-host")
}

fn save_with_message(&self, option: &str) -> Result<()> {
println!("{} Configuration option {} updated!", Emoji("✨", ":-)"), option);
self.save()
Expand Down
16 changes: 16 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ enum SetSubcommand {
OpenaiApiKey {
#[structopt(help = "The OpenAI API key", name = "VALUE")]
value: String
},

#[structopt(about = "Sets the OpenAI host URL")]
Url {
#[structopt(default_value = "https://api.openai.com/v1", env = "OPENAI_URL", help = "The OpenAI host URL", name = "VALUE")]
value: String
}
}

Expand Down Expand Up @@ -181,6 +187,13 @@ fn run_config_openai_api_key(value: String) -> Result<()> {
Ok(())
}

fn run_config_openai_host(value: String) -> Result<()> {
let mut app = App::new()?;
app.update_openai_host(value)?;
println!("✅ OpenAI host URL updated");
Ok(())
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
dotenv().ok();
Expand Down Expand Up @@ -220,6 +233,9 @@ async fn main() -> Result<()> {
SetSubcommand::OpenaiApiKey { value } => {
run_config_openai_api_key(value)?;
}
SetSubcommand::Url { value } => {
run_config_openai_host(value)?;
}
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion src/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ pub async fn call(request: Request) -> Result<Response> {
"git-ai config set openai-api-key <your-key>".yellow()
))?;

let config = OpenAIConfig::new().with_api_key(api_key);
let openai_host = config::APP.openai.host.into());
let config = OpenAIConfig::new().with_api_key(api_key).with_base_url(openai_host);
let client = Client::with_config(config);

// Calculate available tokens using model's context size
Expand Down
Loading