af5a580b5e
- 新增 `ProviderType` 枚举和 `FromStr` 解析,支持通过环境变量选择 Provider - 新增 `ProviderConfig` 结构体和 `create_provider` 工厂方法,统一 Provider 创建 - 更新示例代码使用新的工厂模式,移除直接实例化 OpenaiProvider 的方式 - 移除 Assistant 消息中未使用的 `reasoning_content` 字段
76 lines
2.3 KiB
Rust
76 lines
2.3 KiB
Rust
use std::env;
|
|
|
|
use agcore::init_tracing;
|
|
use agcore::llm::{
|
|
cycle::{CycleConfig, LlmCycle},
|
|
provider::{create_provider, ProviderConfig, ProviderType},
|
|
types::{ChatResponse, OpenaiContentPart},
|
|
};
|
|
|
|
fn extract_response_text(response: &ChatResponse) -> &str {
|
|
match &response.message {
|
|
agcore::llm::types::OpenaiChatMessage::Assistant { content, .. } => match content {
|
|
agcore::llm::types::ContentField::String(s) => s,
|
|
agcore::llm::types::ContentField::Array(parts) => {
|
|
for part in parts {
|
|
if let OpenaiContentPart::Text { text } = part {
|
|
return text;
|
|
}
|
|
}
|
|
"[无文本内容]"
|
|
}
|
|
},
|
|
_ => "[非 assistant 消息]",
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
dotenvy::dotenv().ok();
|
|
init_tracing();
|
|
|
|
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 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(provider, cycle_config)
|
|
.with_system_prompt("你是一个简洁的助手,对于任何问题都是用一句话回答。".to_string());
|
|
|
|
println!("发送请求...");
|
|
|
|
match cycle.submit("介绍一下你自己吧。".to_string(), vec![]).await {
|
|
Ok(response) => {
|
|
println!("LLM 回复:{}", extract_response_text(&response));
|
|
println!(
|
|
"Token 用量:{} 输入, {} 输出",
|
|
response.usage.prompt_tokens, response.usage.completion_tokens
|
|
);
|
|
}
|
|
Err(e) => {
|
|
eprintln!("请求失败:{e}");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
}
|