99 * File Created: 2025-03-01 21:55:58
1010 *
1111 * Modified By: mingcheng ([email protected] ) 12- * Last Modified: 2025-03-03 23:58:02
12+ * Last Modified: 2025-03-05 10:46:26
1313 */
1414
1515use askama:: Template ;
1616use async_openai:: config:: OPENAI_API_BASE ;
1717use async_openai:: {
18+ Client ,
1819 config:: OpenAIConfig ,
1920 types:: { ChatCompletionRequestMessage , CreateChatCompletionRequestArgs } ,
20- Client ,
2121} ;
2222use log:: trace;
23+ use reqwest:: header:: { HeaderMap , HeaderValue } ;
2324use reqwest:: { ClientBuilder , Proxy } ;
2425use std:: env;
2526use std:: error:: Error ;
27+ use std:: time:: Duration ;
2628use tracing:: debug;
2729
30+ use crate :: cli;
31+
2832#[ derive( Template ) ]
2933#[ template( path = "user.txt" ) ]
3034struct PromptTemplate < ' a > {
@@ -42,33 +46,52 @@ impl Default for OpenAI {
4246}
4347
4448impl OpenAI {
49+ /// Create a new OpenAI client instance.
50+ /// This function sets up the OpenAI client with the API key, base URL, and optional proxy settings.
4551 pub fn new ( ) -> Self {
52+ // Set up OpenAI client configuration
4653 let ai_config = OpenAIConfig :: new ( )
4754 . with_api_key ( env:: var ( "OPENAI_API_TOKEN" ) . unwrap_or_else ( |_| String :: from ( "" ) ) )
4855 . with_api_base (
4956 env:: var ( "OPENAI_API_BASE" ) . unwrap_or_else ( |_| String :: from ( OPENAI_API_BASE ) ) ,
50- ) ;
51- let proxy_addr = env:: var ( "OPENAI_APT_PROXY" ) . unwrap_or_else ( |_| String :: from ( "" ) ) ;
57+ )
58+ . with_org_id ( cli:: CMD ) ;
59+
60+ // Set up HTTP client builder with default headers
61+ let mut http_client_builder = ClientBuilder :: new ( ) . user_agent ( cli:: CMD ) . default_headers ( {
62+ let mut headers = HeaderMap :: new ( ) ;
63+ headers. insert ( "HTTP-Referer" , HeaderValue :: from_static ( cli:: CMD_ABOUT_URL ) ) ;
64+ headers. insert ( "X-Title" , HeaderValue :: from_static ( cli:: CMD ) ) ;
65+ headers
66+ } ) ;
5267
53- let mut client = Client :: with_config ( ai_config) ;
68+ // Set up proxy if specified
69+ let proxy_addr = env:: var ( "OPENAI_APT_PROXY" ) . unwrap_or_else ( |_| String :: from ( "" ) ) ;
5470 if !proxy_addr. is_empty ( ) {
5571 trace ! ( "Using proxy: {}" , proxy_addr) ;
56- client = client. with_http_client ( {
57- let proxy = Proxy :: all ( proxy_addr) . unwrap ( ) ;
58- ClientBuilder :: new ( ) . proxy ( proxy) . build ( ) . unwrap ( )
59- } )
60- } ;
72+ http_client_builder = http_client_builder. proxy ( Proxy :: all ( proxy_addr) . unwrap ( ) ) ;
73+ }
74+
75+ // Set up timeout and build the HTTP client
76+ let http_client = http_client_builder
77+ . timeout ( Duration :: from_secs ( 10 ) )
78+ . build ( )
79+ . unwrap ( ) ;
6180
81+ let client = Client :: with_config ( ai_config) . with_http_client ( http_client) ;
6282 OpenAI { client }
6383 }
6484
85+ #[ deprecated]
86+ /// Check if the OpenAI API is reachable.
6587 pub async fn check ( & self ) -> Result < ( ) , Box < dyn Error > > {
6688 match self . client . models ( ) . list ( ) . await {
6789 Ok ( _) => Ok ( ( ) ) ,
6890 Err ( e) => Err ( e. into ( ) ) ,
6991 }
7092 }
7193
94+ /// Send a chat message to the OpenAI API and return the response.
7295 pub async fn chat (
7396 & self ,
7497 model_name : & str ,
0 commit comments