1
- use crate :: models:: general:: llm:: { ChatCompletion , Message } ;
1
+ use crate :: models:: general:: llm:: { APIResponse , ChatCompletion , Message } ;
2
2
3
3
use dotenv:: dotenv;
4
4
use reqwest:: header:: { HeaderMap , HeaderValue } ;
5
5
use std:: env;
6
6
// backend_hive
7
7
8
8
// Call LLM GPT
9
- pub async fn call_gpt ( message : Vec < Message > ) {
9
+ pub async fn call_gpt ( message : Vec < Message > ) -> Result < String , Box < dyn std :: error :: Error + Send > > {
10
10
dotenv ( ) . ok ( ) ;
11
11
12
12
// Extract api keys
@@ -22,20 +22,20 @@ pub async fn call_gpt(message: Vec<Message>) {
22
22
// Create api key headers
23
23
headers. insert (
24
24
"authorization" ,
25
- HeaderValue :: from_str ( & format ! ( "Bearer {}" , api_key) ) . unwrap ( ) ,
26
- ) ;
25
+ HeaderValue :: from_str ( & format ! ( "Bearer {}" , api_key) )
26
+ . map_err ( |e| -> Box < dyn std :: error :: Error + Send > { Box :: new ( e ) } ) ? ) ;
27
27
28
28
// Create OpenAI org Header
29
29
headers. insert (
30
30
"OpenAI-Organization" ,
31
- HeaderValue :: from_str ( & api_org. as_str ( ) ) . unwrap ( ) ,
32
- ) ;
31
+ HeaderValue :: from_str ( & api_org. as_str ( ) )
32
+ . map_err ( |e| -> Box < dyn std :: error :: Error + Send > { Box :: new ( e ) } ) ? ) ;
33
33
34
34
//create client
35
35
let client = reqwest:: Client :: builder ( )
36
36
. default_headers ( headers)
37
37
. build ( )
38
- . unwrap ( ) ;
38
+ . map_err ( |e| -> Box < dyn std :: error :: Error + Send > { Box :: new ( e ) } ) ? ;
39
39
40
40
// create chat completion
41
41
let chat_completion: ChatCompletion = ChatCompletion {
@@ -44,14 +44,29 @@ pub async fn call_gpt(message: Vec<Message>) {
44
44
temperature : 0.1 ,
45
45
} ;
46
46
47
- // Troubleshooting
48
- let res_raw = client
47
+ // // Troubleshooting
48
+ // let res_raw = client
49
+ // .post(url)
50
+ // .json(&chat_completion)
51
+ // .send()
52
+ // .await.unwrap();
53
+ //
54
+ // dbg!(&res_raw.text().await.unwrap());
55
+
56
+
57
+ //GET API response
58
+ let res: APIResponse = client
49
59
. post ( url)
50
60
. json ( & chat_completion)
51
61
. send ( )
52
- . await . unwrap ( ) ;
62
+ . await
63
+ . map_err ( |e| -> Box < dyn std:: error:: Error + Send > { Box :: new ( e) } ) ?
64
+ . json ( )
65
+ . await
66
+ . map_err ( |e| -> Box < dyn std:: error:: Error + Send > { Box :: new ( e) } ) ?;
53
67
54
- dbg ! ( & res_raw. text( ) . await . unwrap( ) ) ;
68
+ //send response
69
+ Ok ( res. choices [ 0 ] . message . content . clone ( ) )
55
70
}
56
71
57
72
#[ cfg( test) ]
@@ -64,6 +79,15 @@ mod tests {
64
79
content : "hi this-is test. Give me a shot response" . to_string ( ) ,
65
80
} ;
66
81
let messages: Vec < Message > = vec ! [ message] ;
67
- call_gpt ( messages) . await ;
82
+ let res: Result < String , Box < dyn std:: error:: Error + Send > > = call_gpt ( messages) . await ;
83
+ match res {
84
+ Ok ( response_str) => {
85
+ dbg ! ( & response_str) ;
86
+ assert ! ( true ) ;
87
+ }
88
+ Err ( _) => {
89
+ assert ! ( false ) ;
90
+ }
91
+ }
68
92
}
69
93
}
0 commit comments