mixvideo-v2/cargos/text-video-agent-rust-sdk/examples/file_upload_test.rs

176 lines
7.2 KiB
Rust

use text_video_agent_client::apis::configuration::Configuration;
use text_video_agent_client::apis::llm_api;
use std::path::PathBuf;
use std::fs;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建支持 HTTPS 的客户端
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()?;
// 创建配置
let config = Configuration {
base_path: "https://bowongai-dev--text-video-agent-fastapi-app.modal.run".to_string(),
user_agent: Some("text-video-agent-rust-client/1.0.6".to_string()),
client,
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
api_key: None,
};
println!("📁 文件上传和媒体分析测试");
println!("==========================");
println!("API 地址: {}", config.base_path);
println!();
// 创建一个测试文本文件
let test_file_path = "test_file.txt";
let test_content = "这是一个测试文件,用于验证文件上传功能。\nThis is a test file for verifying file upload functionality.";
println!("📝 创建测试文件: {}", test_file_path);
fs::write(test_file_path, test_content)?;
println!("✅ 测试文件创建成功");
println!();
// 1. 测试文件上传到 Google 存储
println!("☁️ 测试文件上传到 Google 存储...");
let file_path = PathBuf::from(test_file_path);
match llm_api::google_file_upload(&config, file_path.clone()).await {
Ok(response) => {
println!("✅ 文件上传成功: {}", response);
// 尝试从响应中提取文件URI
if let Some(data) = response.get("data").and_then(|v| v.as_str()) {
println!("📎 文件URI: {}", data);
// 2. 使用上传的文件进行媒体分析
println!();
println!("🔍 测试媒体分析(同步)...");
let analysis_prompts = vec![
"请分析这个文件的内容",
"这个文件包含什么信息?",
"请总结文件的主要内容",
];
for (i, prompt) in analysis_prompts.iter().enumerate() {
println!("🔸 分析 {}: {}", i + 1, prompt);
match llm_api::invoke_media_analysis_api_llm_google_sync_media_analysis_post(
&config,
prompt,
data, // 使用上传后的文件URI
).await {
Ok(analysis_response) => {
println!("✅ 分析结果: {}", analysis_response);
}
Err(e) => {
println!("❌ 分析失败: {:?}", e);
}
}
println!();
// 添加延迟
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
}
// 3. 测试异步媒体分析
println!("⚡ 测试媒体分析(异步)...");
let async_prompt = "请详细分析这个文件,包括语言、内容主题和结构";
match llm_api::submit_media_inference_api_llm_google_async_media_analysis_post(
&config,
async_prompt,
data,
).await {
Ok(async_response) => {
println!("✅ 异步分析任务提交成功: {}", async_response);
// 尝试获取任务ID
if let Some(task_id) = async_response.get("task_id").and_then(|v| v.as_str()) {
println!("📋 任务ID: {}", task_id);
// 轮询任务状态
println!("⏳ 查询任务状态...");
for attempt in 1..=5 {
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
match llm_api::llm_task_id(&config, task_id).await {
Ok(status_response) => {
println!("📊 第{}次查询 - 任务状态: {}", attempt, status_response);
// 检查任务是否完成
if let Some(status) = status_response.get("status") {
if status.is_boolean() && status.as_bool() == Some(true) {
println!("🎉 异步分析任务完成!");
if let Some(data) = status_response.get("data") {
println!("📄 分析结果: {}", data);
}
break;
}
}
}
Err(e) => {
println!("❌ 查询状态失败: {:?}", e);
}
}
}
}
}
Err(e) => {
println!("❌ 异步分析任务提交失败: {:?}", e);
}
}
}
}
Err(e) => {
println!("❌ 文件上传失败: {:?}", e);
}
}
println!();
// 4. 测试不同类型的媒体URI分析
println!("🌐 测试在线媒体分析...");
let online_media_tests = vec![
("https://example.com/sample.jpg", "请描述这张图片的内容"),
("https://example.com/sample.mp4", "请分析这个视频的主要内容"),
("https://example.com/sample.mp3", "请分析这个音频文件"),
];
for (media_uri, prompt) in online_media_tests.iter() {
println!("🔗 分析媒体: {}", media_uri);
println!("❓ 提示: {}", prompt);
match llm_api::invoke_media_analysis_api_llm_google_sync_media_analysis_post(
&config,
prompt,
media_uri,
).await {
Ok(response) => {
println!("✅ 分析结果: {}", response);
}
Err(e) => {
println!("❌ 分析失败: {:?}", e);
}
}
println!();
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
}
// 清理测试文件
println!("🧹 清理测试文件...");
if let Err(e) = fs::remove_file(test_file_path) {
println!("⚠️ 删除测试文件失败: {}", e);
} else {
println!("✅ 测试文件已删除");
}
println!("🎯 文件上传和媒体分析测试完成!");
Ok(())
}