@@ -4,16 +4,22 @@ mod prompt;
44use std:: env;
55
66pub use agent:: { Agent , run_agent} ;
7- use openai_api_rs:: v1:: api:: OpenAIClient ;
7+ use openai_api_rs:: v1:: { api:: OpenAIClient , common :: GPT3_5_TURBO } ;
88pub use prompt:: { PromptConfig , prompt_with_config} ;
99
1010use serde:: Deserialize ;
1111
12- #[ derive( Debug , Clone , Deserialize , Default ) ]
13- pub struct AiConfig {
14- pub openai : Option < ModelConfig > ,
15- pub anthropic : Option < ModelConfig > ,
16- pub deepseek : Option < ModelConfig > ,
12+ const DEEPSEEK_API_ENDPOINT : & str = "https://api.deepseek.com/v1" ;
13+ const DEEPSEEK_V3 : & str = "deepseek-chat" ;
14+
15+ #[ derive( Debug , Clone , Deserialize ) ]
16+ pub enum AiConfig {
17+ #[ serde( rename = "openai" ) ]
18+ OpenAI ( ModelConfig ) ,
19+ #[ serde( rename = "anthropic" ) ]
20+ Anthropic ( ModelConfig ) ,
21+ #[ serde( rename = "deepseek" ) ]
22+ DeepSeek ( ModelConfig ) ,
1723}
1824
1925#[ derive( Debug , Clone , Deserialize ) ]
@@ -22,10 +28,52 @@ pub struct ModelConfig {
2228 pub model : Option < String > ,
2329}
2430
31+ impl AiConfig {
32+ pub ( crate ) fn take_model ( & mut self ) -> Option < String > {
33+ match self {
34+ Self :: OpenAI ( ModelConfig { model, .. } ) => model. take ( ) ,
35+ Self :: Anthropic ( ModelConfig { model, .. } ) => model. take ( ) ,
36+ Self :: DeepSeek ( ModelConfig { model, .. } ) => model. take ( ) ,
37+ }
38+ }
39+ }
40+
2541#[ allow( unused) ]
26- pub ( crate ) fn openai_client ( ) -> OpenAIClient {
27- OpenAIClient :: builder ( )
28- . with_api_key ( env:: var ( "OPENAI_API_KEY" ) . unwrap ( ) . to_string ( ) )
29- . build ( )
30- . unwrap ( )
42+ pub ( crate ) fn openai_client ( config : Option < & AiConfig > ) -> OpenAIClient {
43+ match config {
44+ None => OpenAIClient :: builder ( )
45+ . with_api_key ( env:: var ( "OPENAI_API_KEY" ) . expect ( "OPENAI_API_KEY not set" ) )
46+ . build ( )
47+ . unwrap ( ) ,
48+ Some ( AiConfig :: OpenAI ( model_config) ) => {
49+ let api_key = if model_config. api_key . is_empty ( ) {
50+ env:: var ( "OPENAI_API_KEY" ) . expect ( "OPENAI_API_KEY not set" )
51+ } else {
52+ model_config. api_key . clone ( )
53+ } ;
54+ OpenAIClient :: builder ( )
55+ . with_api_key ( api_key)
56+ . build ( )
57+ . unwrap ( )
58+ }
59+ Some ( AiConfig :: DeepSeek ( ModelConfig { api_key, .. } ) ) => OpenAIClient :: builder ( )
60+ . with_endpoint ( DEEPSEEK_API_ENDPOINT )
61+ . with_api_key ( api_key)
62+ . build ( )
63+ . unwrap ( ) ,
64+ Some ( AiConfig :: Anthropic ( _) ) => unimplemented ! ( "Anthropic API not yet supported" ) ,
65+ }
66+ }
67+
68+ pub ( crate ) fn default_model ( config : Option < & AiConfig > ) -> String {
69+ match config {
70+ None => GPT3_5_TURBO . to_string ( ) ,
71+ Some ( AiConfig :: OpenAI ( ModelConfig { model, .. } ) ) => {
72+ model. clone ( ) . unwrap_or ( GPT3_5_TURBO . to_string ( ) )
73+ }
74+ Some ( AiConfig :: DeepSeek ( ModelConfig { model, .. } ) ) => {
75+ model. clone ( ) . unwrap_or ( DEEPSEEK_V3 . to_string ( ) )
76+ }
77+ Some ( AiConfig :: Anthropic ( _) ) => unimplemented ! ( "Anthropic API not yet supported" ) ,
78+ }
3179}
0 commit comments