Skip to content
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
5 changes: 3 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn clap() -> clap::Command {
.env("OPENAI_KEY")
.hide_env_values(true)
.required(true)
.value_parser(clap::value_parser!(String))
.value_parser(value_parser!(String))
.num_args(1),
)
.arg(
Expand All @@ -38,7 +38,8 @@ pub fn clap() -> clap::Command {
.long("model")
.help("which LLM to use")
.env("PLEASE_MODEL")
.value_parser(value_parser!(super::Model)),
.default_value("gpt-5")
.value_parser(value_parser!(String)),
)
.arg(
Arg::new("temp")
Expand Down
42 changes: 10 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::error::Error;
use std::io::{stdout, Read, Write};

use anyhow::anyhow;
use clap::{ArgMatches, ValueEnum};
use clap::ArgMatches;

use openai::{
chat::{ChatCompletion, ChatCompletionDelta, ChatCompletionMessage, ChatCompletionMessageRole},
Expand All @@ -13,23 +13,6 @@ use tokio::sync::mpsc::Receiver;

mod cli;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Default)]
pub(crate) enum Model {
/// Use gpt-3.5-turbo
#[default]
#[clap(name = "gpt-3.5-turbo", alias = "gpt-3")]
GPT3_5,
/// Use gpt-4
#[clap(name = "gpt-4")]
GPT4,
/// Use gpt-4o
#[clap(name = "gpt-4o")]
GPT4o,
/// Use gpt-4o-mini
#[clap(name = "gpt-4o-mini")]
GPT4oMini,
}

#[tokio::main]
async fn main() {
let matches = cli::clap().get_matches();
Expand All @@ -51,9 +34,7 @@ async fn main() {
}

async fn handle_task(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
let model: Model = matches
.get_one::<Model>("model")
.map_or_else(Model::default, |m| *m);
let model = matches.get_one::<String>("model").unwrap();

let task: Vec<String> = matches
.get_many("task")
Expand All @@ -69,7 +50,7 @@ async fn handle_task(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
return Err(anyhow!("running on tty, no task given").into());
}
prompt = "You are an assistant returning Linux shell commands that accomplish the following task. Don't add explanations or notes.".to_string();
openai_request(model, &prompt, &task, temperature).await?;
openai_request(&model, &prompt, &task, temperature).await?;
} else {
if task.is_empty() {
task.push_str("Please fix this.");
Expand All @@ -81,14 +62,14 @@ async fn handle_task(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {

std::io::stdin().read_to_string(&mut buffer).unwrap();

openai_request(model, &prompt, &buffer, temperature).await?;
openai_request(&model, &prompt, &buffer, temperature).await?;
}

Ok(())
}

async fn openai_request(
model: Model,
model: &str,
prompt: &str,
task: &str,
temperature: f32,
Expand All @@ -113,14 +94,11 @@ async fn openai_request(
tool_calls: Some(Vec::new()),
});

let chat_stream = ChatCompletionDelta::builder(
model.to_possible_value().unwrap().get_name(),
messages.clone(),
)
.credentials(credentials)
.temperature(temperature)
.create_stream()
.await?;
let chat_stream = ChatCompletionDelta::builder(model, messages.clone())
.credentials(credentials)
.temperature(temperature)
.create_stream()
.await?;

listen_for_tokens(chat_stream).await;
Ok(())
Expand Down
Loading