Skip to content
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

use new function call interface #10

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
81 changes: 81 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ repository = "https://github.com/bddap/refac"
anyhow = "1.0.69"
clap = { version = "4.1.8", features = ["derive"] }
itertools = "0.10.5"
lazy_fn = "1.0.2"
lazy_static = "1.4.0"
regex = "1.8.4"
reqwest = { version = "0.11.14", features = ["blocking", "json"] }
rpassword = "7.2.0"
schemars = "0.8.12"
serde = { version = "1.0.154", features = ["derive"] }
serde_json = "1.0.94"
similar = "2.2.1"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,16 @@ Thank you very much for your time and consideration. I eagerly await your respon

Sincerely,

> refac tor '' 'command to show me how many commits I have made'
git rev-list --count HEAD

> refac tor '' 'say something'
// "Something"
// --refac
```

That last one 😂

## Using Refac From Your Favorite Text Editor

First, make sure you have:
Expand Down
68 changes: 57 additions & 11 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,71 @@ pub struct Usage {
}

/// Represents a chat message.
/// serialized examples
/// ```json
/// {"role": "system", "content": "You are a helpful chat bot."}
/// {"role": "user", "content": "What is the weather like in Boston?"},
/// {"role": "assistant", "content": null, "function_call": {"name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}"}},
/// {"role": "function", "name": "get_current_weather", "content": "{\"temperature\": "22", \"unit\": \"celsius\", \"description\": \"Sunny\"}"}
/// ```
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Message {
pub role: String,
pub content: String,
#[serde(tag = "role")]
pub enum Message {
#[serde(rename = "system")]
System { content: String },
#[serde(rename = "user")]
User { content: String },
#[serde(rename = "assistant")]
Assistant {
content: Option<String>,
function_call: Option<FunctionCall>,
},
/// The result of a function call.
#[serde(rename = "function")]
Function { name: String, content: String },
}

impl Message {
pub fn system<S: Into<String>>(content: S) -> Message {
Message {
role: "system".into(),
Message::System {
content: content.into(),
}
}

pub fn user<S: Into<String>>(content: S) -> Message {
Message {
role: "user".into(),
Message::User {
content: content.into(),
}
}

pub fn assistant<S: Into<String>>(content: S) -> Message {
Message {
role: "assistant".into(),
content: content.into(),
pub fn try_into_assistant_content(self) -> Option<String> {
match self {
Self::Assistant {
content: Some(content),
function_call: None,
} => Some(content),
_ => None,
}
}

pub fn assistant_calls<S: Into<String>, A: Into<String>>(name: S, arguments: A) -> Message {
Message::Assistant {
content: None,
function_call: Some(FunctionCall {
name: name.into(),
arguments: arguments.into(),
}),
}
}
}

/// Represents a function call requested by the llm.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct FunctionCall {
pub name: String,
pub arguments: String,
}

/// Represents a request for a chat completion.
///
/// A `ChatCompletionRequest` is used to generate completions for chat conversations
Expand Down Expand Up @@ -147,6 +183,9 @@ pub struct ChatCompletionRequest {
/// A unique identifier representing your end-user, helping OpenAI monitor and detect abuse.
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
/// Which functions the model has access to.
#[serde(skip_serializing_if = "Option::is_none")]
pub functions: Option<Vec<FunctionSpec>>,
}

impl Endpoint for ChatCompletionRequest {
Expand All @@ -159,6 +198,13 @@ impl Endpoint for ChatCompletionRequest {
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct FunctionSpec {
pub name: String,
pub description: String,
pub params: Vec<schemars::schema::Schema>,
}

/// Represents a response from the "chat/completions" endpoint.
///
/// This struct is returned after sending a ChatCompletionRequest to the OpenAI API.
Expand Down
9 changes: 0 additions & 9 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,4 @@ test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; fini
}
}
}

#[test]
#[ignore]
fn long_text_short_diff() {
let from = include_str!("common.rs");
let to = include_str!("common.rs");
let diff = diff(from, to);
assert_eq!(diff, "");
}
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod api;
mod api_client;
mod common;
mod config_files;
mod powers;
mod prompt;

use anyhow::Context;
Expand Down Expand Up @@ -94,6 +95,7 @@ fn refactor(selected: String, transform: String, sc: &Secrets) -> anyhow::Result
frequency_penalty: None,
logit_bias: None,
user: None,
functions: None,
};

let response = client.request(&request)?;
Expand All @@ -114,7 +116,8 @@ fn refactor(selected: String, transform: String, sc: &Secrets) -> anyhow::Result
.next()
.ok_or(anyhow::anyhow!("No choices returned."))?
.message
.content;
.try_into_assistant_content()
.ok_or(anyhow::anyhow!("Assistant tried to call a function."))?;

tracing::debug!("diff: \n{}", diff);

Expand Down
Loading