Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/caijunduo_learning-ai-application-development.json

1 line
22 KiB
JSON

{"repo": "caijunduo/learning-ai-application-development", "files": {"phase-1/01-simple-call/main.go": {"81e592da556681c5a68b50c3aa33314dcab18c55": {"t": 1785745639.3638434, "n": 1699, "h": "bbd805796a3bb8b0", "c": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n)\n\n// Deepseek API 兼容 OpenAI Chat Completions 格式\nconst baseURL = \"https://api.deepseek.com/v1/chat/completions\"\n\ntype ChatRequest struct {\n\tModel string `json:\"model\"`\n\tMessages []Message `json:\"messages\"`\n}\n\ntype Message struct {\n\tRole string `json:\"role\"`\n\tContent string `json:\"content\"`\n}\n\ntype ChatResponse struct {\n\tChoices []struct {\n\t\tMessage Message `json:\"message\"`\n\t} `json:\"choices\"`\n\tUsage struct {\n\t\tPromptTokens int `json:\"prompt_tokens\"`\n\t\tCompletionTokens int `json:\"completion_tokens\"`\n\t\tTotalTokens int `json:\"total_tokens\"`\n\t} `json:\"usage\"`\n}\n\nfunc main() {\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tif apiKey == \"\" {\n\t\tfmt.Println(\"请设置环境变量 DEEPSEEK_API_KEY\")\n\t\tos.Exit(1)\n\t}\n\n\treq := ChatRequest{\n\t\tModel: \"deepseek-v4-flash\",\n\t\tMessages: []Message{\n\t\t\t{Role: \"user\", Content: \"用一句话解释什么是 Go 语言的 goroutine\"},\n\t\t},\n\t}\n\n\tbody, _ := json.Marshal(req)\n\n\thttpReq, _ := http.NewRequest(\"POST\", baseURL, bytes.NewReader(body))\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+apiKey)\n\n\tresp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\tfmt.Println(\"请求失败:\", err)\n\t\tos.Exit(1)\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\n\tvar result ChatResponse\n\tjson.Unmarshal(respBody, &result)\n\n\tif len(result.Choices) > 0 {\n\t\tfmt.Println(result.Choices[0].Message.Content)\n\t\tfmt.Println()\n\t\tfmt.Printf(\"📊 Token 用量: prompt=%d, completion=%d, total=%d\\n\",\n\t\t\tresult.Usage.PromptTokens,\n\t\t\tresult.Usage.CompletionTokens,\n\t\t\tresult.Usage.TotalTokens)\n\t} else {\n\t\tfmt.Println(\"没有返回结果,原始响应:\", string(respBody))\n\t}\n}\n"}, "287865d06d2748827d7122bb3f06baa73049a1cc": {"t": 1785755166.4387758, "n": 1699, "h": "bbd805796a3bb8b0", "c": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n)\n\n// Deepseek API 兼容 OpenAI Chat Completions 格式\nconst baseURL = \"https://api.deepseek.com/v1/chat/completions\"\n\ntype ChatRequest struct {\n\tModel string `json:\"model\"`\n\tMessages []Message `json:\"messages\"`\n}\n\ntype Message struct {\n\tRole string `json:\"role\"`\n\tContent string `json:\"content\"`\n}\n\ntype ChatResponse struct {\n\tChoices []struct {\n\t\tMessage Message `json:\"message\"`\n\t} `json:\"choices\"`\n\tUsage struct {\n\t\tPromptTokens int `json:\"prompt_tokens\"`\n\t\tCompletionTokens int `json:\"completion_tokens\"`\n\t\tTotalTokens int `json:\"total_tokens\"`\n\t} `json:\"usage\"`\n}\n\nfunc main() {\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tif apiKey == \"\" {\n\t\tfmt.Println(\"请设置环境变量 DEEPSEEK_API_KEY\")\n\t\tos.Exit(1)\n\t}\n\n\treq := ChatRequest{\n\t\tModel: \"deepseek-v4-flash\",\n\t\tMessages: []Message{\n\t\t\t{Role: \"user\", Content: \"用一句话解释什么是 Go 语言的 goroutine\"},\n\t\t},\n\t}\n\n\tbody, _ := json.Marshal(req)\n\n\thttpReq, _ := http.NewRequest(\"POST\", baseURL, bytes.NewReader(body))\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+apiKey)\n\n\tresp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\tfmt.Println(\"请求失败:\", err)\n\t\tos.Exit(1)\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\n\tvar result ChatResponse\n\tjson.Unmarshal(respBody, &result)\n\n\tif len(result.Choices) > 0 {\n\t\tfmt.Println(result.Choices[0].Message.Content)\n\t\tfmt.Println()\n\t\tfmt.Printf(\"📊 Token 用量: prompt=%d, completion=%d, total=%d\\n\",\n\t\t\tresult.Usage.PromptTokens,\n\t\t\tresult.Usage.CompletionTokens,\n\t\t\tresult.Usage.TotalTokens)\n\t} else {\n\t\tfmt.Println(\"没有返回结果,原始响应:\", string(respBody))\n\t}\n}\n"}, "1f69d6530bcbb4eb01d74bd11c590e447e1b08f0": {"t": 1785755168.71827, "n": 1350, "h": "56efdee8e0f859e9", "c": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n)\n\n// Deepseek API 兼容 OpenAI Chat Completions 格式\nconst baseURL = \"https://api.deepseek.com/v1/chat/completions\"\n\ntype ChatRequest struct {\n\tModel string `json:\"model\"`\n\tMessages []Message `json:\"messages\"`\n}\n\ntype Message struct {\n\tRole string `json:\"role\"`\n\tContent string `json:\"content\"`\n}\n\ntype ChatResponse struct {\n\tChoices []struct {\n\t\tMessage Message `json:\"message\"`\n\t} `json:\"choices\"`\n}\n\nfunc main() {\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tif apiKey == \"\" {\n\t\tfmt.Println(\"请设置环境变量 DEEPSEEK_API_KEY\")\n\t\tos.Exit(1)\n\t}\n\n\treq := ChatRequest{\n\t\tModel: \"deepseek-v4-flash\",\n\t\tMessages: []Message{\n\t\t\t{Role: \"user\", Content: \"用一句话解释什么是 Go 语言的 goroutine\"},\n\t\t},\n\t}\n\n\tbody, _ := json.Marshal(req)\n\n\thttpReq, _ := http.NewRequest(\"POST\", baseURL, bytes.NewReader(body))\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+apiKey)\n\n\tresp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\tfmt.Println(\"请求失败:\", err)\n\t\tos.Exit(1)\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\n\tvar result ChatResponse\n\tjson.Unmarshal(respBody, &result)\n\n\tif len(result.Choices) > 0 {\n\t\tfmt.Println(result.Choices[0].Message.Content)\n\t} else {\n\t\tfmt.Println(\"没有返回结果,原始响应:\", string(respBody))\n\t}\n}\n"}}, "phase-1/03-multi-turn/main.go": {"81e592da556681c5a68b50c3aa33314dcab18c55": {"t": 1785745646.5866232, "n": 3601, "h": "9dfbb25dec59dfee", "c": "package main\n\nimport (\n\t\"bufio\"\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n\t\"strings\"\n)\n\nconst baseURL = \"https://api.deepseek.com/v1/chat/completions\"\n\ntype ChatRequest struct {\n\tModel string `json:\"model\"`\n\tMessages []Message `json:\"messages\"`\n}\n\ntype Message struct {\n\tRole string `json:\"role\"`\n\tContent string `json:\"content\"`\n}\n\ntype ChatResponse struct {\n\tChoices []struct {\n\t\tMessage Message `json:\"message\"`\n\t} `json:\"choices\"`\n\tUsage struct {\n\t\tPromptTokens int `json:\"prompt_tokens\"`\n\t\tCompletionTokens int `json:\"completion_tokens\"`\n\t\tTotalTokens int `json:\"total_tokens\"`\n\t} `json:\"usage\"`\n}\n\nfunc callAPI(messages []Message, apiKey string) (string, int, int, int, error) {\n\treq := ChatRequest{\n\t\tModel: \"deepseek-v4-flash\",\n\t\tMessages: messages,\n\t}\n\n\tbody, _ := json.Marshal(req)\n\n\thttpReq, _ := http.NewRequest(\"POST\", baseURL, bytes.NewReader(body))\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+apiKey)\n\n\tresp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn \"\", 0, 0, 0, err\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\n\tvar result ChatResponse\n\tjson.Unmarshal(respBody, &result)\n\n\tif len(result.Choices) > 0 {\n\t\treturn result.Choices[0].Message.Content,\n\t\t\tresult.Usage.PromptTokens,\n\t\t\tresult.Usage.CompletionTokens,\n\t\t\tresult.Usage.TotalTokens,\n\t\t\tnil\n\t}\n\treturn \"\", 0, 0, 0, fmt.Errorf(\"没有返回结果: %s\", string(respBody))\n}\n\nfunc main() {\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tif apiKey == \"\" {\n\t\tfmt.Println(\"请设置环境变量 DEEPSEEK_API_KEY\")\n\t\tos.Exit(1)\n\t}\n\n\tsystemPrompt := `角色:Go代码审查官\n约束:只需要审查Go代码即可,其他无关的事情直接回复无法处理\n背景:只审查Go1.x.x版本的代码\n流程:\n1. 先逐行阅读代码,站在全局视角看代码,标注所有涉及错误处理或逻辑错误的位置\n2. 对照Go官方错误处理规范,判断每次是否合规\n3. 汇总发现,区分好的写法和问题写法\n4. 按照输出格式生成报告\n任务:审查Go代码的安全性、错误处理、代码规范等,重点关注错误处理是否规范\n输出:按顺序输出【审查报告】【好的处理】【坏的处理】`\n\n\tmessages := []Message{\n\t\t{Role: \"system\", Content: systemPrompt},\n\t}\n\n\tfmt.Println(\"=== Go 代码审查工具 ===\")\n\tfmt.Println(\"请输入 Go 代码(输入空行结束,输入 'quit' 退出):\")\n\tfmt.Println()\n\n\tscanner := bufio.NewScanner(os.Stdin)\n\n\tfor {\n\t\tfmt.Print(\">>> 请输入 Go 代码:\\n\")\n\n\t\t// 读取多行代码,空行结束\n\t\tvar codeLines []string\n\t\tfor scanner.Scan() {\n\t\t\tline := scanner.Text()\n\t\t\tif line == \"\" {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tif strings.TrimSpace(line) == \"quit\" {\n\t\t\t\tfmt.Println(\"再见!\")\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcodeLines = append(codeLines, line)\n\t\t}\n\n\t\tcode := strings.TrimSpace(strings.Join(codeLines, \"\\n\"))\n\t\tif code == \"\" {\n\t\t\tcontinue\n\t\t}\n\n\t\t// 快速检测:如果不是 Go 代码,直接跳过\n\t\tif !looksLikeGo(code) {\n\t\t\tfmt.Println(\"⚠️ 这看起来不是 Go 代码,请输入 Go 代码。\")\n\t\t\tfmt.Println()\n\t\t\tcontinue\n\t\t}\n\n\t\t// 构建审查请求\n\t\treviewMessages := append(messages, Message{\n\t\t\tRole: \"user\",\n\t\t\tContent: fmt.Sprintf(\"请审查以下 Go 代码:\\n\\n```go\\n%s\\n```\", code),\n\t\t})\n\n\t\treply, promptTokens, completionTokens, totalTokens, err := callAPI(reviewMessages, apiKey)\n\t\tif err != nil {\n\t\t\tfmt.Println(\"调用失败:\", err)\n\t\t\tcontinue\n\t\t}\n\n\t\tfmt.Println()\n\t\tfmt.Println(reply)\n\t\tfmt.Println()\n\t\tfmt.Printf(\"📊 Token 用量: prompt=%d, completion=%d, total=%d\\n\",\n\t\t\tpromptTokens, completionTokens, totalTokens)\n\t\tfmt.Println()\n\n\t\t// 询问是否继续\n\t\tfmt.Print(\">>> 继续审查?(直接回车继续 / 输入 quit 退出): \")\n\t\tif !scanner.Scan() {\n\t\t\tbreak\n\t\t}\n\t\tchoice := strings.TrimSpace(scanner.Text())\n\t\tif choice == \"quit\" {\n\t\t\tfmt.Println(\"再见!\")\n\t\t\treturn\n\t\t}\n\t\tfmt.Println()\n\t}\n}\n\n// looksLikeGo 简单判断输入是否像 Go 代码\nfunc looksLikeGo(code string) bool {\n\tcode = strings.TrimSpace(code)\n\t// 检查是否包含 Go 关键字特征\n\tgoKeywords := []string{\"package \", \"func \", \"import \", \"type \", \"var \", \"const \"}\n\tfor _, kw := range goKeywords {\n\t\tif strings.Contains(code, kw) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"}, "40a01d5904268838d982dca4e8fa3f7cac1f4f89": {"t": 1785755164.7169604, "n": 3601, "h": "9dfbb25dec59dfee", "c": "package main\n\nimport (\n\t\"bufio\"\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n\t\"strings\"\n)\n\nconst baseURL = \"https://api.deepseek.com/v1/chat/completions\"\n\ntype ChatRequest struct {\n\tModel string `json:\"model\"`\n\tMessages []Message `json:\"messages\"`\n}\n\ntype Message struct {\n\tRole string `json:\"role\"`\n\tContent string `json:\"content\"`\n}\n\ntype ChatResponse struct {\n\tChoices []struct {\n\t\tMessage Message `json:\"message\"`\n\t} `json:\"choices\"`\n\tUsage struct {\n\t\tPromptTokens int `json:\"prompt_tokens\"`\n\t\tCompletionTokens int `json:\"completion_tokens\"`\n\t\tTotalTokens int `json:\"total_tokens\"`\n\t} `json:\"usage\"`\n}\n\nfunc callAPI(messages []Message, apiKey string) (string, int, int, int, error) {\n\treq := ChatRequest{\n\t\tModel: \"deepseek-v4-flash\",\n\t\tMessages: messages,\n\t}\n\n\tbody, _ := json.Marshal(req)\n\n\thttpReq, _ := http.NewRequest(\"POST\", baseURL, bytes.NewReader(body))\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+apiKey)\n\n\tresp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn \"\", 0, 0, 0, err\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\n\tvar result ChatResponse\n\tjson.Unmarshal(respBody, &result)\n\n\tif len(result.Choices) > 0 {\n\t\treturn result.Choices[0].Message.Content,\n\t\t\tresult.Usage.PromptTokens,\n\t\t\tresult.Usage.CompletionTokens,\n\t\t\tresult.Usage.TotalTokens,\n\t\t\tnil\n\t}\n\treturn \"\", 0, 0, 0, fmt.Errorf(\"没有返回结果: %s\", string(respBody))\n}\n\nfunc main() {\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tif apiKey == \"\" {\n\t\tfmt.Println(\"请设置环境变量 DEEPSEEK_API_KEY\")\n\t\tos.Exit(1)\n\t}\n\n\tsystemPrompt := `角色:Go代码审查官\n约束:只需要审查Go代码即可,其他无关的事情直接回复无法处理\n背景:只审查Go1.x.x版本的代码\n流程:\n1. 先逐行阅读代码,站在全局视角看代码,标注所有涉及错误处理或逻辑错误的位置\n2. 对照Go官方错误处理规范,判断每次是否合规\n3. 汇总发现,区分好的写法和问题写法\n4. 按照输出格式生成报告\n任务:审查Go代码的安全性、错误处理、代码规范等,重点关注错误处理是否规范\n输出:按顺序输出【审查报告】【好的处理】【坏的处理】`\n\n\tmessages := []Message{\n\t\t{Role: \"system\", Content: systemPrompt},\n\t}\n\n\tfmt.Println(\"=== Go 代码审查工具 ===\")\n\tfmt.Println(\"请输入 Go 代码(输入空行结束,输入 'quit' 退出):\")\n\tfmt.Println()\n\n\tscanner := bufio.NewScanner(os.Stdin)\n\n\tfor {\n\t\tfmt.Print(\">>> 请输入 Go 代码:\\n\")\n\n\t\t// 读取多行代码,空行结束\n\t\tvar codeLines []string\n\t\tfor scanner.Scan() {\n\t\t\tline := scanner.Text()\n\t\t\tif line == \"\" {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tif strings.TrimSpace(line) == \"quit\" {\n\t\t\t\tfmt.Println(\"再见!\")\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcodeLines = append(codeLines, line)\n\t\t}\n\n\t\tcode := strings.TrimSpace(strings.Join(codeLines, \"\\n\"))\n\t\tif code == \"\" {\n\t\t\tcontinue\n\t\t}\n\n\t\t// 快速检测:如果不是 Go 代码,直接跳过\n\t\tif !looksLikeGo(code) {\n\t\t\tfmt.Println(\"⚠️ 这看起来不是 Go 代码,请输入 Go 代码。\")\n\t\t\tfmt.Println()\n\t\t\tcontinue\n\t\t}\n\n\t\t// 构建审查请求\n\t\treviewMessages := append(messages, Message{\n\t\t\tRole: \"user\",\n\t\t\tContent: fmt.Sprintf(\"请审查以下 Go 代码:\\n\\n```go\\n%s\\n```\", code),\n\t\t})\n\n\t\treply, promptTokens, completionTokens, totalTokens, err := callAPI(reviewMessages, apiKey)\n\t\tif err != nil {\n\t\t\tfmt.Println(\"调用失败:\", err)\n\t\t\tcontinue\n\t\t}\n\n\t\tfmt.Println()\n\t\tfmt.Println(reply)\n\t\tfmt.Println()\n\t\tfmt.Printf(\"📊 Token 用量: prompt=%d, completion=%d, total=%d\\n\",\n\t\t\tpromptTokens, completionTokens, totalTokens)\n\t\tfmt.Println()\n\n\t\t// 询问是否继续\n\t\tfmt.Print(\">>> 继续审查?(直接回车继续 / 输入 quit 退出): \")\n\t\tif !scanner.Scan() {\n\t\t\tbreak\n\t\t}\n\t\tchoice := strings.TrimSpace(scanner.Text())\n\t\tif choice == \"quit\" {\n\t\t\tfmt.Println(\"再见!\")\n\t\t\treturn\n\t\t}\n\t\tfmt.Println()\n\t}\n}\n\n// looksLikeGo 简单判断输入是否像 Go 代码\nfunc looksLikeGo(code string) bool {\n\tcode = strings.TrimSpace(code)\n\t// 检查是否包含 Go 关键字特征\n\tgoKeywords := []string{\"package \", \"func \", \"import \", \"type \", \"var \", \"const \"}\n\tfor _, kw := range goKeywords {\n\t\tif strings.Contains(code, kw) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"}, "287865d06d2748827d7122bb3f06baa73049a1cc": {"t": 1785755165.8917537, "n": 2633, "h": "d0da5eea0e7ae09f", "c": "package main\n\nimport (\n\t\"bufio\"\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n\t\"strings\"\n)\n\nconst baseURL = \"https://api.deepseek.com/v1/chat/completions\"\n\ntype ChatRequest struct {\n\tModel string `json:\"model\"`\n\tMessages []Message `json:\"messages\"`\n}\n\ntype Message struct {\n\tRole string `json:\"role\"`\n\tContent string `json:\"content\"`\n}\n\ntype ChatResponse struct {\n\tChoices []struct {\n\t\tMessage Message `json:\"message\"`\n\t} `json:\"choices\"`\n\tUsage struct {\n\t\tPromptTokens int `json:\"prompt_tokens\"`\n\t\tCompletionTokens int `json:\"completion_tokens\"`\n\t\tTotalTokens int `json:\"total_tokens\"`\n\t} `json:\"usage\"`\n}\n\nfunc callAPI(messages []Message, apiKey string) (string, int, int, int, error) {\n\treq := ChatRequest{\n\t\tModel: \"deepseek-v4-flash\",\n\t\tMessages: messages,\n\t}\n\n\tbody, _ := json.Marshal(req)\n\n\thttpReq, _ := http.NewRequest(\"POST\", baseURL, bytes.NewReader(body))\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+apiKey)\n\n\tresp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn \"\", 0, 0, 0, err\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\n\tvar result ChatResponse\n\tjson.Unmarshal(respBody, &result)\n\n\tif len(result.Choices) > 0 {\n\t\treturn result.Choices[0].Message.Content,\n\t\t\tresult.Usage.PromptTokens,\n\t\t\tresult.Usage.CompletionTokens,\n\t\t\tresult.Usage.TotalTokens,\n\t\t\tnil\n\t}\n\treturn \"\", 0, 0, 0, fmt.Errorf(\"没有返回结果: %s\", string(respBody))\n}\n\nfunc main() {\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tif apiKey == \"\" {\n\t\tfmt.Println(\"请设置环境变量 DEEPSEEK_API_KEY\")\n\t\tos.Exit(1)\n\t}\n\n\t// 维护一个 messages 数组,这就是模型的\"记忆\"\n\tmessages := []Message{\n\t\t{Role: \"system\", Content: \"你是一个简洁的助手,回答尽量简短。\"},\n\t}\n\n\tscanner := bufio.NewScanner(os.Stdin)\n\n\tfmt.Println(\"=== 多轮对话(共 3 轮)===\")\n\tfmt.Println()\n\n\tfor turn := 1; turn <= 3; turn++ {\n\t\tfmt.Printf(\"--- 第 %d 轮 ---\\n\", turn)\n\t\tfmt.Print(\"你: \")\n\n\t\tif !scanner.Scan() {\n\t\t\tbreak\n\t\t}\n\t\tuserInput := strings.TrimSpace(scanner.Text())\n\t\tif userInput == \"\" {\n\t\t\tturn-- // 空输入不计轮次\n\t\t\tcontinue\n\t\t}\n\n\t\t// 把用户消息加入历史\n\t\tmessages = append(messages, Message{Role: \"user\", Content: userInput})\n\n\t\t// 把完整历史发给模型\n\t\treply, promptTokens, completionTokens, totalTokens, err := callAPI(messages, apiKey)\n\t\tif err != nil {\n\t\t\tfmt.Println(\"调用失败:\", err)\n\t\t\tcontinue\n\t\t}\n\n\t\t// 把模型回复也加入历史\n\t\tmessages = append(messages, Message{Role: \"assistant\", Content: reply})\n\n\t\tfmt.Printf(\"AI: %s\\n\", reply)\n\t\tfmt.Printf(\"📊 Token 用量: prompt=%d, completion=%d, total=%d\\n\\n\",\n\t\t\tpromptTokens, completionTokens, totalTokens)\n\t}\n\n\tfmt.Println(\"=== 对话结束 ===\")\n\tfmt.Printf(\"\\n最终 messages 数组长度: %d 条\\n\", len(messages))\n}\n"}, "1f69d6530bcbb4eb01d74bd11c590e447e1b08f0": {"t": 1785755167.1434755, "n": 2171, "h": "59604df5052b0c6c", "c": "package main\n\nimport (\n\t\"bufio\"\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n\t\"strings\"\n)\n\nconst baseURL = \"https://api.deepseek.com/v1/chat/completions\"\n\ntype ChatRequest struct {\n\tModel string `json:\"model\"`\n\tMessages []Message `json:\"messages\"`\n}\n\ntype Message struct {\n\tRole string `json:\"role\"`\n\tContent string `json:\"content\"`\n}\n\ntype ChatResponse struct {\n\tChoices []struct {\n\t\tMessage Message `json:\"message\"`\n\t} `json:\"choices\"`\n}\n\nfunc callAPI(messages []Message, apiKey string) (string, error) {\n\treq := ChatRequest{\n\t\tModel: \"deepseek-v4-flash\",\n\t\tMessages: messages,\n\t}\n\n\tbody, _ := json.Marshal(req)\n\n\thttpReq, _ := http.NewRequest(\"POST\", baseURL, bytes.NewReader(body))\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+apiKey)\n\n\tresp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, _ := io.ReadAll(resp.Body)\n\n\tvar result ChatResponse\n\tjson.Unmarshal(respBody, &result)\n\n\tif len(result.Choices) > 0 {\n\t\treturn result.Choices[0].Message.Content, nil\n\t}\n\treturn \"\", fmt.Errorf(\"没有返回结果: %s\", string(respBody))\n}\n\nfunc main() {\n\tapiKey := os.Getenv(\"DEEPSEEK_API_KEY\")\n\tif apiKey == \"\" {\n\t\tfmt.Println(\"请设置环境变量 DEEPSEEK_API_KEY\")\n\t\tos.Exit(1)\n\t}\n\n\t// 维护一个 messages 数组,这就是模型的\"记忆\"\n\tmessages := []Message{\n\t\t{Role: \"system\", Content: \"你是一个简洁的助手,回答尽量简短。\"},\n\t}\n\n\tscanner := bufio.NewScanner(os.Stdin)\n\n\tfmt.Println(\"=== 多轮对话(共 3 轮)===\")\n\tfmt.Println()\n\n\tfor turn := 1; turn <= 3; turn++ {\n\t\tfmt.Printf(\"--- 第 %d 轮 ---\\n\", turn)\n\t\tfmt.Print(\"你: \")\n\n\t\tif !scanner.Scan() {\n\t\t\tbreak\n\t\t}\n\t\tuserInput := strings.TrimSpace(scanner.Text())\n\t\tif userInput == \"\" {\n\t\t\tturn-- // 空输入不计轮次\n\t\t\tcontinue\n\t\t}\n\n\t\t// 把用户消息加入历史\n\t\tmessages = append(messages, Message{Role: \"user\", Content: userInput})\n\n\t\t// 把完整历史发给模型\n\t\treply, err := callAPI(messages, apiKey)\n\t\tif err != nil {\n\t\t\tfmt.Println(\"调用失败:\", err)\n\t\t\tcontinue\n\t\t}\n\n\t\t// 把模型回复也加入历史\n\t\tmessages = append(messages, Message{Role: \"assistant\", Content: reply})\n\n\t\tfmt.Printf(\"AI: %s\\n\\n\", reply)\n\t}\n\n\tfmt.Println(\"=== 对话结束 ===\")\n\tfmt.Printf(\"\\n最终 messages 数组长度: %d 条\\n\", len(messages))\n}\n"}}}}