@@ -4,16 +4,22 @@ mod prompt;
4
4
use std:: env;
5
5
6
6
pub 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 } ;
8
8
pub use prompt:: { PromptConfig , prompt_with_config} ;
9
9
10
10
use serde:: Deserialize ;
11
11
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 ) ,
17
23
}
18
24
19
25
#[ derive( Debug , Clone , Deserialize ) ]
@@ -22,10 +28,60 @@ pub struct ModelConfig {
22
28
pub model : Option < String > ,
23
29
}
24
30
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
+ pub ( crate ) fn set_model ( & mut self , m : String ) {
41
+ match self {
42
+ Self :: OpenAI ( ModelConfig { model, .. } ) => model. replace ( m) ,
43
+ Self :: Anthropic ( ModelConfig { model, .. } ) => model. replace ( m) ,
44
+ Self :: DeepSeek ( ModelConfig { model, .. } ) => model. replace ( m) ,
45
+ } ;
46
+ }
47
+ }
48
+
25
49
#[ 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 ( )
50
+ pub ( crate ) fn openai_client ( config : Option < & AiConfig > ) -> OpenAIClient {
51
+ match config {
52
+ None => OpenAIClient :: builder ( )
53
+ . with_api_key ( env:: var ( "OPENAI_API_KEY" ) . expect ( "OPENAI_API_KEY not set" ) )
54
+ . build ( )
55
+ . unwrap ( ) ,
56
+ Some ( AiConfig :: OpenAI ( model_config) ) => {
57
+ let api_key = if model_config. api_key . is_empty ( ) {
58
+ env:: var ( "OPENAI_API_KEY" ) . expect ( "OPENAI_API_KEY not set" )
59
+ } else {
60
+ model_config. api_key . clone ( )
61
+ } ;
62
+ OpenAIClient :: builder ( )
63
+ . with_api_key ( api_key)
64
+ . build ( )
65
+ . unwrap ( )
66
+ }
67
+ Some ( AiConfig :: DeepSeek ( ModelConfig { api_key, .. } ) ) => OpenAIClient :: builder ( )
68
+ . with_endpoint ( DEEPSEEK_API_ENDPOINT )
69
+ . with_api_key ( api_key)
70
+ . build ( )
71
+ . unwrap ( ) ,
72
+ Some ( AiConfig :: Anthropic ( _) ) => unimplemented ! ( "Anthropic API not yet supported" ) ,
73
+ }
74
+ }
75
+
76
+ pub ( crate ) fn default_model ( config : Option < & AiConfig > ) -> String {
77
+ match config {
78
+ None => GPT3_5_TURBO . to_string ( ) ,
79
+ Some ( AiConfig :: OpenAI ( ModelConfig { model, .. } ) ) => {
80
+ model. clone ( ) . unwrap_or ( GPT3_5_TURBO . to_string ( ) )
81
+ }
82
+ Some ( AiConfig :: DeepSeek ( ModelConfig { model, .. } ) ) => {
83
+ model. clone ( ) . unwrap_or ( DEEPSEEK_V3 . to_string ( ) )
84
+ }
85
+ Some ( AiConfig :: Anthropic ( _) ) => unimplemented ! ( "Anthropic API not yet supported" ) ,
86
+ }
31
87
}
0 commit comments