feat(llm): 添加 Provider 工厂方法和枚举类型
- 新增 `ProviderType` 枚举和 `FromStr` 解析,支持通过环境变量选择 Provider - 新增 `ProviderConfig` 结构体和 `create_provider` 工厂方法,统一 Provider 创建 - 更新示例代码使用新的工厂模式,移除直接实例化 OpenaiProvider 的方式 - 移除 Assistant 消息中未使用的 `reasoning_content` 字段
This commit is contained in:
@@ -3,7 +3,7 @@ use std::env;
|
||||
use agcore::init_tracing;
|
||||
use agcore::llm::{
|
||||
cycle::{CycleConfig, LlmCycle},
|
||||
provider::openai::OpenaiProvider,
|
||||
provider::{create_provider, ProviderConfig, ProviderType},
|
||||
types::{ChatResponse, OpenaiContentPart},
|
||||
};
|
||||
|
||||
@@ -31,17 +31,30 @@ async fn main() {
|
||||
|
||||
let api_key = env::var("OPENAI_API_KEY").expect("未设置 OPENAI_API_KEY 环境变量");
|
||||
let base_url = env::var("OPENAI_BASE_URL").expect("未设置 OPENAI_BASE_URL 环境变量");
|
||||
let model = env::var("OPENAI_MODEL").expect("未设置 OPENAI_MODEL 环境变量选择所要使用的模型");
|
||||
let model = env::var("OPENAI_MODEL").expect("未设置 OPENAI_MODEL 环境变量");
|
||||
|
||||
let provider = OpenaiProvider::new(base_url, api_key, model.clone());
|
||||
let config = CycleConfig {
|
||||
let provider_type = env::var("PROVIDER")
|
||||
.unwrap_or_else(|_| "openai".into())
|
||||
.parse::<ProviderType>()
|
||||
.expect("无效的 PROVIDER 值");
|
||||
|
||||
let config = ProviderConfig {
|
||||
base_url,
|
||||
api_key,
|
||||
model: model.clone(),
|
||||
};
|
||||
|
||||
let provider = create_provider(provider_type, config)
|
||||
.expect("创建 Provider 失败");
|
||||
|
||||
let cycle_config = CycleConfig {
|
||||
model,
|
||||
max_tokens: Some(65536),
|
||||
temperature: Some(1.3),
|
||||
..CycleConfig::default()
|
||||
};
|
||||
|
||||
let mut cycle = LlmCycle::new(Box::new(provider), config)
|
||||
let mut cycle = LlmCycle::new(provider, cycle_config)
|
||||
.with_system_prompt("你是一个简洁的助手,对于任何问题都是用一句话回答。".to_string());
|
||||
|
||||
println!("发送请求...");
|
||||
|
||||
Reference in New Issue
Block a user