@@ -4,13 +4,84 @@ 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
10+ use serde:: Deserialize ;
11+
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 ) ,
23+ }
24+
25+ #[ derive( Debug , Clone , Deserialize ) ]
26+ pub struct ModelConfig {
27+ pub api_key : String ,
28+ pub model : Option < String > ,
29+ }
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+
1049#[ allow( unused) ]
11- pub ( crate ) fn openai_client ( ) -> OpenAIClient {
12- OpenAIClient :: builder ( )
13- . with_api_key ( env:: var ( "OPENAI_API_KEY" ) . unwrap ( ) . to_string ( ) )
14- . build ( )
15- . 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+ }
1687}
0 commit comments