test-ai.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // 简单的AI接口测试脚本
  2. const axios = require('axios');
  3. const AI_CONFIG = {
  4. baseURL: 'https://open.bigmodel.cn/api/paas/v4/',
  5. apiKey: 'a835b9f6866d48ec956d341418df8a50.NuhlKYn58EkCb5iP',
  6. model: 'glm-4-flash-250414',
  7. temperature: 0.7,
  8. timeout: 30000
  9. };
  10. async function testAI() {
  11. try {
  12. console.log('🧪 开始测试智谱AI接口...');
  13. const response = await axios.post(`${AI_CONFIG.baseURL}/chat/completions`, {
  14. model: AI_CONFIG.model,
  15. messages: [
  16. {
  17. role: 'system',
  18. content: '你是一位专业的厨师,请根据用户提供的食材和菜系要求,生成详细的菜谱。请严格按照JSON格式返回,不要包含任何其他文字。'
  19. },
  20. {
  21. role: 'user',
  22. content: `你是一位川菜大师,精通四川菜系。川菜以麻辣鲜香、口味多变著称,有"一菜一格,百菜百味"的美誉。请根据用户提供的食材,设计一道地道的川菜,突出麻辣特色和层次丰富的口感。回答要包含菜名、详细制作步骤、调料配比和川菜技法。
  23. 用户提供的食材:土豆、肉丝
  24. 请按照以下JSON格式返回菜谱:
  25. {
  26. "name": "菜品名称",
  27. "ingredients": ["食材1", "食材2"],
  28. "steps": [
  29. {
  30. "step": 1,
  31. "description": "步骤描述",
  32. "time": 5,
  33. "temperature": "中火"
  34. }
  35. ],
  36. "cookingTime": 30,
  37. "difficulty": "medium",
  38. "tips": ["技巧1", "技巧2"]
  39. }`
  40. }
  41. ],
  42. temperature: AI_CONFIG.temperature,
  43. max_tokens: 2000,
  44. stream: false
  45. }, {
  46. headers: {
  47. 'Content-Type': 'application/json',
  48. 'Authorization': `Bearer ${AI_CONFIG.apiKey}`
  49. },
  50. timeout: AI_CONFIG.timeout
  51. });
  52. console.log('✅ AI接口调用成功!');
  53. console.log('📝 响应内容:');
  54. console.log(response.data.choices[0].message.content);
  55. // 尝试解析JSON
  56. try {
  57. const content = response.data.choices[0].message.content.trim();
  58. let cleanContent = content;
  59. if (cleanContent.startsWith('```json')) {
  60. cleanContent = cleanContent.replace(/```json\s*/, '').replace(/```\s*$/, '');
  61. } else if (cleanContent.startsWith('```')) {
  62. cleanContent = cleanContent.replace(/```\s*/, '').replace(/```\s*$/, '');
  63. }
  64. const recipe = JSON.parse(cleanContent);
  65. console.log('🍽️ 解析后的菜谱:');
  66. console.log(JSON.stringify(recipe, null, 2));
  67. } catch (parseError) {
  68. console.log('⚠️ JSON解析失败,但接口调用成功');
  69. }
  70. } catch (error) {
  71. console.error('❌ AI接口测试失败:');
  72. if (error.response) {
  73. console.error('状态码:', error.response.status);
  74. console.error('错误信息:', error.response.data);
  75. } else {
  76. console.error('错误详情:', error.message);
  77. }
  78. }
  79. }
  80. // 运行测试
  81. testAI();