mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
feat: optional libcurl linking
This commit is contained in:
parent
b4c7dd74c0
commit
609c7609d7
3 changed files with 69 additions and 36 deletions
|
|
@ -33,7 +33,11 @@ pay-respects-parser = "0.2.6"
|
|||
|
||||
[features]
|
||||
runtime-rules = ["dep:serde", "dep:toml"]
|
||||
request-ai = ["dep:serde", "dep:serde_json", "dep:curl", "dep:textwrap"]
|
||||
request-ai = ["dep:serde", "dep:serde_json", "dep:textwrap"]
|
||||
|
||||
# linking to libcurl dynamically requires openssl when compiling and
|
||||
# complicates cross compilation
|
||||
libcurl = ["dep:curl"]
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
|
|
|
|||
|
|
@ -69,11 +69,15 @@ fn print_version() {
|
|||
println!("compile features:");
|
||||
#[cfg(feature = "runtime-rules")]
|
||||
{
|
||||
println!("\t- runtime-rules");
|
||||
println!(" - runtime-rules");
|
||||
}
|
||||
#[cfg(feature = "request-ai")]
|
||||
{
|
||||
println!("\t- request-ai");
|
||||
println!(" - request-ai");
|
||||
}
|
||||
#[cfg(feature = "libcurl")]
|
||||
{
|
||||
println!(" - libcurl");
|
||||
}
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,8 @@
|
|||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use curl::easy::Easy as Curl;
|
||||
use curl::easy::List;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Input {
|
||||
role: String,
|
||||
|
|
@ -93,6 +89,7 @@ The command `{last_command}` returns the following error message: `{error_msg}`.
|
|||
"#
|
||||
);
|
||||
|
||||
let res;
|
||||
let messages = Messages {
|
||||
messages: vec![Input {
|
||||
role: "user".to_string(),
|
||||
|
|
@ -101,6 +98,12 @@ The command `{last_command}` returns the following error message: `{error_msg}`.
|
|||
model,
|
||||
};
|
||||
|
||||
#[cfg(feature = "libcurl")]
|
||||
{
|
||||
use curl::easy::Easy as Curl;
|
||||
use curl::easy::List;
|
||||
use std::io::Read;
|
||||
|
||||
let str_json = serde_json::to_string(&messages).unwrap();
|
||||
let mut data = str_json.as_bytes();
|
||||
|
||||
|
|
@ -135,8 +138,30 @@ The command `{last_command}` returns the following error message: `{error_msg}`.
|
|||
transfer.perform().expect("Failed to perform request");
|
||||
}
|
||||
|
||||
let res = String::from_utf8(dst).unwrap();
|
||||
res = String::from_utf8(dst).unwrap();
|
||||
}
|
||||
#[cfg(not(feature = "libcurl"))]
|
||||
{
|
||||
let proc = std::process::Command::new("curl")
|
||||
.arg("-X")
|
||||
.arg("POST")
|
||||
.arg("-H")
|
||||
.arg(format!("Authorization: Bearer {}", api_key))
|
||||
.arg("-H")
|
||||
.arg("Content-Type: application/json")
|
||||
.arg("-d")
|
||||
.arg(serde_json::to_string(&messages).unwrap())
|
||||
.arg(request_url)
|
||||
.output();
|
||||
|
||||
let out = match proc {
|
||||
Ok(proc) => proc.stdout,
|
||||
Err(_) => {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
res = String::from_utf8(out).unwrap();
|
||||
}
|
||||
let json: Value = serde_json::from_str(&res).unwrap();
|
||||
|
||||
let content = &json["choices"][0]["message"]["content"];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue