test-api.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // 测试 linux.do 的 API 接口
  2. const API_KEY = "ah-52ff02ce8f7c1e6028e3bec33ba488a92cdaec659756c8cee92c2b4ef5ee83fe";
  3. const BASE_URL = "https://hub.linux.do/v1";
  4. async function testModelList() {
  5. console.log("=== 测试模型列表接口 ===");
  6. const url = `${BASE_URL}/models`;
  7. console.log(`请求 URL: ${url}`);
  8. console.log(`API Key: ${API_KEY.substring(0, 10)}...`);
  9. try {
  10. // 测试 1: 标准请求
  11. console.log("\n1. 标准 Bearer 认证请求:");
  12. let response = await fetch(url, {
  13. method: "GET",
  14. headers: {
  15. "Authorization": `Bearer ${API_KEY}`,
  16. "Content-Type": "application/json"
  17. }
  18. });
  19. console.log(`状态码: ${response.status}`);
  20. console.log(`状态文本: ${response.statusText}`);
  21. const text1 = await response.text();
  22. console.log(`响应内容: ${text1.substring(0, 500)}`);
  23. if (response.status === 403) {
  24. // 测试 2: 添加浏览器 User-Agent
  25. console.log("\n2. 添加浏览器 User-Agent:");
  26. response = await fetch(url, {
  27. method: "GET",
  28. headers: {
  29. "Authorization": `Bearer ${API_KEY}`,
  30. "Content-Type": "application/json",
  31. "Accept": "application/json",
  32. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) QMaiWrite"
  33. }
  34. });
  35. console.log(`状态码: ${response.status}`);
  36. const text2 = await response.text();
  37. console.log(`响应内容: ${text2.substring(0, 500)}`);
  38. if (response.status === 403) {
  39. // 测试 3: 去掉 Origin,添加更多兼容性 header
  40. console.log("\n3. 移除 Origin,添加兼容性 headers:");
  41. response = await fetch(url, {
  42. method: "GET",
  43. headers: {
  44. "Authorization": `Bearer ${API_KEY}`,
  45. "Accept": "application/json",
  46. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
  47. }
  48. });
  49. console.log(`状态码: ${response.status}`);
  50. const text3 = await response.text();
  51. console.log(`响应内容: ${text3.substring(0, 500)}`);
  52. }
  53. }
  54. if (response.ok) {
  55. const data = await response.json();
  56. console.log("\n✅ 成功获取模型列表:");
  57. console.log(`模型数量: ${data.data?.length || 0}`);
  58. if (data.data && data.data.length > 0) {
  59. console.log("前5个模型:", data.data.slice(0, 5).map(m => m.id).join(", "));
  60. }
  61. }
  62. } catch (error) {
  63. console.error("❌ 请求失败:", error.message);
  64. }
  65. }
  66. async function testChatCompletion() {
  67. console.log("\n\n=== 测试聊天接口 ===");
  68. const url = `${BASE_URL}/chat/completions`;
  69. const body = {
  70. model: "deepseek-v4-flash",
  71. messages: [
  72. { role: "user", content: "你好,请回复'测试成功'" }
  73. ],
  74. stream: false
  75. };
  76. try {
  77. console.log("\n使用兼容性 headers:");
  78. const response = await fetch(url, {
  79. method: "POST",
  80. headers: {
  81. "Authorization": `Bearer ${API_KEY}`,
  82. "Content-Type": "application/json",
  83. "Accept": "application/json",
  84. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) QMaiWrite"
  85. },
  86. body: JSON.stringify(body)
  87. });
  88. console.log(`状态码: ${response.status}`);
  89. const text = await response.text();
  90. console.log(`响应内容: ${text.substring(0, 500)}`);
  91. if (response.ok) {
  92. const data = JSON.parse(text);
  93. console.log("\n✅ 聊天接口正常:");
  94. console.log(`回复: ${data.choices?.[0]?.message?.content}`);
  95. }
  96. } catch (error) {
  97. console.error("❌ 请求失败:", error.message);
  98. }
  99. }
  100. // 运行测试
  101. testModelList().then(() => testChatCompletion());