Skip to content

Add async_openai API for completions #68

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

Merged
merged 4 commits into from
Mar 3, 2023
Merged
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
279 changes: 279 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/main.rs"

[dependencies]
anyhow = "1.0.69"
async-openai = "0.8.0"
async-std = "1.12.0"
async-trait = "0.1.64"
clap = { version = "4.1.8", features = ["derive"] }
Expand Down
5 changes: 4 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ install:
cargo install --path .

e2e: install
sh -eux -c 'for i in ./e2e/test_*.sh ; do sh -x "$i" ; done'
sh -eux -c 'for i in ./tests/e2e/test_*.sh ; do sh -x "$i" ; done'

test *args: e2e
cargo test
alias t := test

bench: install
sh ./tests/bench/run_bench.sh

lint:
cargo fmt --all -- --check
cargo clippy --all-features --all-targets -- -D warnings --allow deprecated
Expand Down
2 changes: 1 addition & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) fn get_hooks_path() -> Result<PathBuf> {
// create dirs first otherwise canonicalize will fail
fs::create_dir_all(&rel_hooks_path)?;
#[cfg(unix)]
fs::set_permissions(&rel_hooks_path, Permissions::from_mode(0o755))?;
fs::set_permissions(&rel_hooks_path, Permissions::from_mode(0o700))?;
// turn relative path into absolute path
let hooks_path = std::fs::canonicalize(rel_hooks_path)?;
Ok(hooks_path)
Expand Down
85 changes: 26 additions & 59 deletions src/llms/openai.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use std::time::Duration;

use anyhow::{anyhow, bail, Result};

use async_trait::async_trait;
use colored::Colorize;
use reqwest::{Client, ClientBuilder};
use serde_json::{json, Value};

use tiktoken_rs::tiktoken::{p50k_base, CoreBPE};

use crate::settings::OpenAISettings;
use async_openai::{types::CreateCompletionRequestArgs, Client};

use super::llm_client::LlmClient;

#[derive(Clone, Debug)]
pub(crate) struct OpenAIClient {
api_key: String,
model: String,
client: Client,
}
Expand All @@ -30,13 +26,8 @@ impl OpenAIClient {
bail!("No OpenAI model configured.")
}

let timeout = Duration::new(15, 0);
let client = ClientBuilder::new().timeout(timeout).build()?;
Ok(Self {
api_key,
model,
client,
})
let client = Client::new().with_api_key(&api_key);
Ok(Self { model, client })
}

pub(crate) fn get_prompt_token_limit_for_model(&self) -> usize {
Expand Down Expand Up @@ -73,55 +64,31 @@ impl LlmClient for OpenAIClient {
bail!(error_msg)
}

let json_data = json!({
"model": self.model,
"prompt": prompt,
"temperature": 0.5,
"max_tokens": output_length,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
});

let request = self
.client
.post("https://api.openai.com/v1/completions")
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", self.api_key))
.json(&json_data);
// Create request using builder pattern
let request = CreateCompletionRequestArgs::default()
.model(&self.model)
.prompt(prompt)
.max_tokens(output_length as u16)
.temperature(0.5)
.top_p(1.)
.frequency_penalty(0.)
.presence_penalty(0.)
.build()?;

debug!("Sending request to OpenAI:\n{:?}", request);

let response = request.send().await?;
let response_body = response.text().await?;
let json_response: Value = serde_json::from_str(&response_body).map_err(|e| {
anyhow!(
"Could not decode JSON response: {}\nResponse body: {:?}",
e,
response_body
)
})?;
Ok(json_response["choices"][0]["text"]
.as_str()
.ok_or_else(|| {
let error_message: &str = json_response
.get("error")
.and_then(|e| e.get("message"))
.and_then(|m| m.as_str())
.unwrap_or_default();
if !error_message.is_empty() {
return anyhow!(
"{}",
format!("OpenAI error: {error_message}").bold().yellow()
);
}
let response = self
.client
.completions() // Get the API "group" (completions, images, etc.) from the client
.create(request) // Make the API call in that "group"
.await?;

let completion = response
.choices
.first()
.ok_or(anyhow!("No completion results"))
.map(|c| c.text.clone());

anyhow!(
"Unexpected API response:\n{}",
json_response.to_string().yellow()
)
})?
.trim()
.to_string())
return completion;
}
}
6 changes: 6 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{collections::HashMap, fs, path::PathBuf, str::FromStr};
#[cfg(unix)]
use std::{fs::Permissions, os::unix::prelude::PermissionsExt};

use config::{
builder::DefaultState, Config, ConfigBuilder, ConfigError, Environment, File, Source,
Expand Down Expand Up @@ -271,6 +273,8 @@ pub fn get_local_config_path() -> Option<PathBuf> {
.join("gptcommit.toml");
if !config_path.exists() {
fs::write(&config_path, "").ok()?;
#[cfg(unix)]
fs::set_permissions(&config_path, Permissions::from_mode(0o600)).unwrap();
}
return Some(config_path);
}
Expand All @@ -287,6 +291,8 @@ pub fn get_user_config_path() -> Option<PathBuf> {
let config_path = config_dir.join("config.toml");
if !config_path.exists() {
fs::write(&config_path, "").ok()?;
#[cfg(unix)]
fs::set_permissions(&config_path, Permissions::from_mode(0o600)).unwrap();
}
return Some(config_path);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/bench/bench_githook_openai.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

gptcommit prepare-commit-msg \
--git-diff-content "${1}" \
--commit-msg-file "${2}" \
--commit-source ""
7 changes: 7 additions & 0 deletions tests/bench/bench_githook_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

GPTCOMMIT__MODEL_PROVIDER="tester-foobar" \
gptcommit prepare-commit-msg \
--git-diff-content "${1}" \
--commit-msg-file "${2}" \
--commit-source ""
21 changes: 21 additions & 0 deletions tests/bench/run_bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh
set -eu

ROOT="$(pwd)"
DIFF_CONTENT_PATH="$(pwd)/tests/data/example_1.diff"

export TEMPDIR=$(mktemp -d)
(
cd "${TEMPDIR}"
git init

export TEMPFILE=$(mktemp)
echo "foo" > $TEMPFILE

hyperfine \
--warmup 1 \
--max-runs 10 \
"$ROOT/tests/bench/bench_githook_test.sh"\ "${DIFF_CONTENT_PATH}"\ "${TEMPFILE}" \
"$ROOT/tests/bench/bench_githook_openai.sh"\ "${DIFF_CONTENT_PATH}"\ "${TEMPFILE}" \
)
rm -rf "${TEMPDIR}"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.