feat(examples): 添加 LLM 单轮对话示例
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
use std::env;
|
||||
|
||||
use agcore::init_tracing;
|
||||
use agcore::llm::{
|
||||
cycle::{CycleConfig, LlmCycle},
|
||||
provider::openai::OpenaiProvider,
|
||||
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 = OpenaiProvider::new(base_url, api_key, model.clone());
|
||||
let config = CycleConfig {
|
||||
model,
|
||||
max_tokens: Some(65536),
|
||||
temperature: Some(1.3),
|
||||
..CycleConfig::default()
|
||||
};
|
||||
|
||||
let mut cycle = LlmCycle::new(Box::new(provider), 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user