-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(llms): basic chatgpt integration
- Loading branch information
Showing
5 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::fs; | ||
use toml::Value; | ||
|
||
pub fn get_api_key() -> String { | ||
let config_path = format!( | ||
"{}/.config/pipelm/.api_configs.toml", | ||
std::env::var("HOME").unwrap() | ||
); | ||
let content = fs::read_to_string(config_path).expect("Failed to read the TOML file"); | ||
let value: Value = content.parse().expect("Failed to parse TOML"); | ||
|
||
// Extract the API key from the TOML table. | ||
let api_key = value | ||
.get("openai") | ||
.and_then(|table| table.get("API_KEY")) | ||
.and_then(|api_key| api_key.as_str()) | ||
.unwrap_or_else(|| { | ||
eprintln!("API_KEY not found in the TOML file."); | ||
std::process::exit(1); | ||
}); | ||
|
||
api_key.to_string() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::config::get_api_key; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Message { | ||
pub role: String, | ||
pub content: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Choice { | ||
pub index: u32, | ||
pub message: Message, | ||
pub finish_reason: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Usage { | ||
pub prompt_tokens: u32, | ||
pub completion_tokens: u32, | ||
pub total_tokens: u32, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct OpenAiResponse { | ||
pub id: String, | ||
pub object: String, | ||
pub created: u64, | ||
pub model: String, | ||
pub choices: Vec<Choice>, | ||
pub usage: Usage, | ||
pub system_fingerprint: String, | ||
} | ||
|
||
pub fn make_authenticated_request(text: &str) -> Result<ureq::Response, ureq::Error> { | ||
let api_key = get_api_key(); | ||
println!("Trying to reach openai with {}", &api_key); | ||
ureq::post("https://api.openai.com/v1/chat/completions") | ||
.set("Content-Type", "application/json") | ||
.set("Authorization", &format!("Bearer {}", api_key)) | ||
.send_json(ureq::json!({ | ||
"model": "gpt-4-1106-preview", | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": text | ||
} | ||
] | ||
}) | ||
) | ||
} |