@@ -9,29 +9,25 @@ use std::env;
9
9
pub async fn call_gpt ( message : Vec < Message > ) -> Result < String , Box < dyn std:: error:: Error + Send > > {
10
10
dotenv ( ) . ok ( ) ;
11
11
12
- // Extract api keys
12
+ // Extract api keys from .env
13
13
let api_key: String = env:: var ( "OPEN_AI_KEY" ) . expect ( "OPEN_AI_KEY not found" ) ;
14
14
let api_org: String = env:: var ( "OPEN_AI_ORG" ) . expect ( "OPEN_AI_ORG not found" ) ;
15
15
16
- // Confirm endpoint
16
+ // Confirm base url endpoint
17
17
let url: & str = "https://api.openai.com/v1/chat/completions" ;
18
18
19
- // Create headers
19
+ // Create client headers
20
20
let mut headers = HeaderMap :: new ( ) ;
21
-
22
- // Create api key headers
23
21
headers. insert (
24
22
"authorization" ,
25
23
HeaderValue :: from_str ( & format ! ( "Bearer {}" , api_key) )
26
24
. map_err ( |e| -> Box < dyn std:: error:: Error + Send > { Box :: new ( e) } ) ?) ;
27
-
28
- // Create OpenAI org Header
29
25
headers. insert (
30
26
"OpenAI-Organization" ,
31
27
HeaderValue :: from_str ( & api_org. as_str ( ) )
32
28
. map_err ( |e| -> Box < dyn std:: error:: Error + Send > { Box :: new ( e) } ) ?) ;
33
29
34
- //create client
30
+ // create reqwest client
35
31
let client = reqwest:: Client :: builder ( )
36
32
. default_headers ( headers)
37
33
. build ( )
@@ -53,8 +49,7 @@ pub async fn call_gpt(message: Vec<Message>) -> Result<String, Box<dyn std::erro
53
49
//
54
50
// dbg!(&res_raw.text().await.unwrap());
55
51
56
-
57
- //GET API response
52
+ //crete request to url and get response as APIResponse
58
53
let res: APIResponse = client
59
54
. post ( url)
60
55
. json ( & chat_completion)
@@ -65,10 +60,17 @@ pub async fn call_gpt(message: Vec<Message>) -> Result<String, Box<dyn std::erro
65
60
. await
66
61
. map_err ( |e| -> Box < dyn std:: error:: Error + Send > { Box :: new ( e) } ) ?;
67
62
68
- //send response
63
+ //parse 'content' from massage from choice from api call response
69
64
Ok ( res. choices [ 0 ] . message . content . clone ( ) )
70
65
}
71
66
67
+
68
+ /*
69
+
70
+ Unit Tests
71
+
72
+ */
73
+
72
74
#[ cfg( test) ]
73
75
mod tests {
74
76
use super :: * ;
0 commit comments