test_https_proxy/
test_https_proxy.rs1use ai_lib::types::common::Content;
2use ai_lib::{AiClient, ChatCompletionRequest, Message, Provider, Role};
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 println!("🔒 HTTPS代理测试");
7 println!("================");
8
9 std::env::set_var("AI_PROXY_URL", "https://192.168.2.13:8889");
11 println!("🌐 使用HTTPS代理: https://192.168.2.13:8889");
12
13 if std::env::var("OPENAI_API_KEY").is_err() {
14 println!("❌ 未设置OPENAI_API_KEY");
15 return Ok(());
16 }
17
18 println!("\n🤖 测试OpenAI (HTTPS代理):");
20 let client = AiClient::new(Provider::OpenAI)?;
21
22 let request = ChatCompletionRequest::new(
23 "gpt-3.5-turbo".to_string(),
24 vec![Message {
25 role: Role::User,
26 content: Content::Text("Say 'HTTPS proxy works!' exactly.".to_string()),
27 function_call: None,
28 }],
29 )
30 .with_max_tokens(10);
31
32 match client.chat_completion(request).await {
33 Ok(response) => {
34 println!("✅ HTTPS代理测试成功!");
35 println!(
36 " 响应: '{}'",
37 response.choices[0].message.content.as_text()
38 );
39 println!(" Token使用: {}", response.usage.total_tokens);
40 }
41 Err(e) => {
42 println!("❌ HTTPS代理测试失败: {}", e);
43
44 let error_str = e.to_string();
46 if error_str.contains("you must provide a model parameter") {
47 println!(" → 这可能是代理服务器的问题,而不是HTTPS协议问题");
48 } else if error_str.contains("certificate") || error_str.contains("tls") {
49 println!(" → HTTPS证书或TLS相关问题");
50 } else if error_str.contains("connection") {
51 println!(" → 连接问题,可能是代理服务器配置");
52 }
53 }
54 }
55
56 println!("\n🚀 测试Groq (HTTPS代理对比):");
58 if std::env::var("GROQ_API_KEY").is_ok() {
59 let groq_client = AiClient::new(Provider::Groq)?;
60 let groq_request = ChatCompletionRequest::new(
61 "llama3-8b-8192".to_string(),
62 vec![Message {
63 role: Role::User,
64 content: Content::Text("Say 'Groq HTTPS proxy works!' exactly.".to_string()),
65 function_call: None,
66 }],
67 )
68 .with_max_tokens(10);
69
70 match groq_client.chat_completion(groq_request).await {
71 Ok(response) => {
72 println!("✅ Groq HTTPS代理成功!");
73 println!(
74 " 响应: '{}'",
75 response.choices[0].message.content.as_text()
76 );
77 }
78 Err(e) => {
79 println!("❌ Groq HTTPS代理失败: {}", e);
80 }
81 }
82 }
83
84 println!("\n💡 HTTPS代理测试结论:");
85 println!(" • 如果Groq成功而OpenAI失败,说明是OpenAI特定问题");
86 println!(" • 如果都失败,可能是HTTPS代理配置问题");
87 println!(" • 如果都成功,说明HTTPS代理完全支持");
88
89 Ok(())
90}