Skip to content

Commit 36c3bcb

Browse files
committed
extend comments to call_gpt fn
1 parent 23df911 commit 36c3bcb

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/api_handler/call_request.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,25 @@ use std::env;
99
pub async fn call_gpt(message: Vec<Message>) -> Result<String, Box<dyn std::error::Error + Send>> {
1010
dotenv().ok();
1111

12-
// Extract api keys
12+
// Extract api keys from .env
1313
let api_key: String = env::var("OPEN_AI_KEY").expect("OPEN_AI_KEY not found");
1414
let api_org: String = env::var("OPEN_AI_ORG").expect("OPEN_AI_ORG not found");
1515

16-
// Confirm endpoint
16+
// Confirm base url endpoint
1717
let url: &str = "https://api.openai.com/v1/chat/completions";
1818

19-
// Create headers
19+
// Create client headers
2020
let mut headers = HeaderMap::new();
21-
22-
// Create api key headers
2321
headers.insert(
2422
"authorization",
2523
HeaderValue::from_str(&format!("Bearer {}", api_key))
2624
.map_err(|e| -> Box<dyn std::error::Error + Send>{ Box::new(e) })?);
27-
28-
// Create OpenAI org Header
2925
headers.insert(
3026
"OpenAI-Organization",
3127
HeaderValue::from_str(&api_org.as_str())
3228
.map_err(|e| -> Box<dyn std::error::Error + Send>{ Box::new(e) })?);
3329

34-
//create client
30+
// create reqwest client
3531
let client = reqwest::Client::builder()
3632
.default_headers(headers)
3733
.build()
@@ -53,8 +49,7 @@ pub async fn call_gpt(message: Vec<Message>) -> Result<String, Box<dyn std::erro
5349
//
5450
// dbg!(&res_raw.text().await.unwrap());
5551

56-
57-
//GET API response
52+
//crete request to url and get response as APIResponse
5853
let res: APIResponse = client
5954
.post(url)
6055
.json(&chat_completion)
@@ -65,10 +60,17 @@ pub async fn call_gpt(message: Vec<Message>) -> Result<String, Box<dyn std::erro
6560
.await
6661
.map_err(|e| -> Box<dyn std::error::Error + Send>{ Box::new(e) })?;
6762

68-
//send response
63+
//parse 'content' from massage from choice from api call response
6964
Ok(res.choices[0].message.content.clone())
7065
}
7166

67+
68+
/*
69+
70+
Unit Tests
71+
72+
*/
73+
7274
#[cfg(test)]
7375
mod tests {
7476
use super::*;

0 commit comments

Comments
 (0)