Information
Home in { "model" : "deepseek-ai/DeepSeek-R1" , "temperature" : 0 , "messages" : [ { "role" : "system" , "content" : "You are Roo, a highly skilled software engineer ......" } , { "role" : "user" , "content" : [ { "type" : "text" , "text" : "\nwrite a python demo to chat with api.openai.com\n " } , { "type" : "text" , "text" : " ... " } ] } , # VSCode Visible Files # # VSCode Open Tabs # # Current Time # # Current Context Size (Tokens) # # Current Mode # # Current Working Directory (/Users/cc/develop/paper_research/tmp2) Files # { "role" : "assistant" , "content" : "......" } , 1. 2. 3. 4. - - - - - - - print("Chat with AI (type 'quit' to exit)") while True: try: user_input = input("\nYou: ") if user_input.lower() == 'quit': break response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": user_input}] ) print(f"\nAI: {response.choices[0].message['content']}") except openai.error.AuthenticationError: print("Error: Invalid API key. Please check your key and try again.") break except Exception as e: print(f"An error occurred: {str(e)}") break __name__ __ name __ __main__ __ main __ # OpenAI Chat Demo # ## Requirements ## - - ## Installation ## ```bashpip install openai``` ``` bash pip install openai install ``` ## Usage ## 1. [OpenAI Platform](https://platform.openai.com/api-keys) OpenAI Platform https://platform.openai.com/api-keys 2. 3. 4. 5. ## Notes ## - - - - { "role" : "user" , "content" : [ { "type" : "text" , "text" : "[write_to_file for 'chat_demo.py'] Result:" } , { "type" : "text" , "text" : "......" } , { "type" : "text" , "text" : "Tool [write_to_file] was not executed because a tool has already been used in this message. Only one tool may be used per message. You must assess the first tool's result before proceeding to use the next tool." } , { "type" : "text" , "text" : "Tool [execute_command] was not executed because a tool has already been used in this message. Only one tool may be used per message. You must assess the first tool's result before proceeding to use the next tool." } , { "type" : "text" , "text" : "Tool [attempt_completion] was not executed because a tool has already been used in this message. Only one tool may be used per message. You must assess the first tool's result before proceeding to use the next tool." } , { "type" : "text" , "text" : "\n# VSCode Visible Files\nchat_demo.py\n\n# VSCode Open Tabs\nREADME.md\nchat_demo.py\n\n# Current Time\n2/10/2025, 9:59:55 PM (Asia/Shanghai, UTC+8:00)\n\n# Current Context Size (Tokens)\n11,913,404 (9307%)\n\n# Current Mode\ncode\n " } ] } { "role" : "assistant" , "content" : "......" } , - { "role" : "user" , "content" : [ { "type" : "text" , "text" : "[apply_diff for 'chat_demo.py'] Result:" } , { "type" : "text" , "text" : "......" } , { "type" : "text" , "text" : "Tool [execute_command] was not executed because a tool has already been used in this message. Only one tool may be used per message. You must assess the first tool's result before proceeding to use the next tool." } , { "type" : "text" , "text" : "\n# VSCode Visible Files\nchat_demo.py\n\n# VSCode Open Tabs\nREADME.md\nchat_demo.py\n\n# Current Time\n2/10/2025, 10:16:26 PM (Asia/Shanghai, UTC+8:00)\n\n# Current Context Size (Tokens)\n5,205,887 (4067%)\n\n# Current Mode\ncode\n " } , { "type" : "text" , "text" : "[TASK RESUMPTION] This task was interrupted just now. It may or may not be complete, so please reassess the task context. Be aware that the project state may have changed since then. The current working directory is now '/Users/cc/develop/paper_research/tmp2'. If the task has not been completed, retry the last step before interruption and proceed with completing the task.\n\nNote: If you previously attempted a tool use that the user did not provide a result for, you should assume the tool use was not successful and assess whether you should retry. If the last tool was a browser_action, the browser has been closed and you must launch a new browser if needed." } , { "type" : "text" , "text" : "\n# VSCode Visible Files\nchat_demo.py\n\n# VSCode Open Tabs\nREADME.md\nchat_demo.py\n\n# Current Time\n2/10/2025, 10:16:44 PM (Asia/Shanghai, UTC+8:00)\n\n# Current Context Size (Tokens)\n5,205,887 (4067%)\n\n# Current Mode\ncode\n\n# Current Working Directory (/Users/cc/develop/paper_research/tmp2) Files\nchat_demo.py\nREADME.md\n " } ] } , def chat_with_openai():======= ======= >>>>>>> chat_with_openai()======= ======= >>>>>>> # OpenAI Chat Demo # ## Requirements ## - - ## Installation ## ```bashpip install openai``` ``` bash pip install openai install ``` ## Usage ## 1. [OpenAI Platform](https://platform.openai.com/api-keys) OpenAI Platform https://platform.openai.com/api-keys 2. ```bashpython chat_demo.py``` ``` bash python chat_demo.py ``` 3. 4. 5. ## Notes ## - - - - - - __name__ __ name __ __main__ __ main __ __name__ __ name __ __main__ __ main __ __name__ __ name __ __main__ __ main __ __name__ __ name __ __main__ __ main __ 1. 2. 3. { "role" : "user" , "content" : [ { "type" : "text" , "text" : "[execute_command for 'python chat_demo.py'] Result:" } , { "type" : "text" , "text" : "Command is still running in the user's terminal.\nHere's the output so far:\nEnter your OpenAI API key:\n\nYou will be updated on the terminal status and new output in the future." } , { "type" : "text" , "text" : "\n# VSCode Visible Files\nchat_demo.py\n\n# VSCode Open Tabs\nREADME.md\nchat_demo.py\n\n# Actively Running Terminals\n## Original command: `python chat_demo.py`\n\n# Current Time\n2/10/2025, 10:21:37 PM (Asia/Shanghai, UTC+8:00)\n\n# Current Context Size (Tokens)\n3,957,252 (3092%)\n\n# Current Mode\ncode\n " } ] } Your email address will not be published. Required fields are marked * * * * * ← Previous: ServiceNow 通过精调 Embedding 模型提升 RAG 的准确性 Next: Mac mini M4 vs. RTX 4090 vs. RTX 2080ti: 部署DeepSeek R1 的效果评测 → Daily LLM Papers PDF Images Extractor Daily LLM Papers PDF Images Extractor X LinkedIn — 【导语】当爆红的 DeepSeek R1 遇上编程界开挂神器 Cline (OpenRouter工具榜TOP1),是1+1>2的超神组合,还是可怕的 Token 黑洞?实测 Debug 全过程,揭秘 Cline + DeepSeek R1 的魔力 Cline 是一个开源的编程 Agent,支持编辑文件、运行 Terminal 命令、使用 Browser。Cline 可以自定义模型 API,包括OpenAI、Gemini和任意 OpenAI API 兼容的模型供应商,当前也包括在本地 localhost 部署的模型。 Cline 在 openrouter 中排第一名 1 月底至今,DeepSeek R1 火爆全球,Cline 官方也专门支持了 DeepSeek R1 模型。兼容了 DeepSeek、Gemini 的 Thinking 输出。 在体验过 DeepSeek + Cline 后,觉得它自主思考规划、自主编程和反馈的模式非常有意思。于是深入研究它是怎么做到的。 Cline 使用 Apache 2.0 开源协议,有很多 fork 版本。其中 Roo Code(原名是 Roo Cline)是最火的版本。我使用了 Roo Code。此外简单试用了原生版 Cline,感觉体验远不如 Roo Code。 在本地启动一个 deepseek r1 模型 provider 的 proxy,代理 Cline 的请求。在 proxy 里打印 Cline 发送的完整 prompt,这样就能观测到 Cline 的内部工作机制了。deepseek r1 使用的是硅基流动的 api 服务。 Cline 的 system prompt 也可以在项目源码中找到,它的 system prompt 大致有 800 行,5 万个字符。但它是静态的,不适合分析 Cline 工作时的行为。所以有了本篇文章 直接让 Cline + DeepSeek R1 写一个 openai 的 chat demo 程序 write a python demo to chat with api.openai.com Cline 使用的 system prompt 超级超级长… 输入这次编程任务的描述,这是第一次输入指令,也是本次测试中的唯一一次手动输入文字 write a python demo to chat with api.openai.com Cline 真正发送到模型的是 2 段:第一段是 包裹的输入指令,第二段是环境上下文 如下 … 所示: 其中environment_details 的内容格式化以后如下,包含了 VS Code 当前的编辑器状态等 R1 模型先输出了 思考过程,然后调用工具 , , , 。 有趣的是,R1 模型一次性输出了 2 个文件的内容,并认为已经写完,可以运行命令来验证,并尝试完成任务。 但是最后 R1 被 Cline 打断,因为 Cline 判断到了有多个命令,不符合 system prompt 里的要求 在 Cline 的对话框里,Cline 已经编辑好 chat_demo.py 并提示用户 save 点击 Save 以后,自动进入第二轮。来看看第 2 轮做了什么 第 2 轮发送了第一个 工具调用的执行结果,把它摘出来格式化显示如下: 正是由于有这个错误,触发了 Cline 继续与 R1 对话,尝试解决问题。这个过程并没有人工的干预。 这个 flake8 的报错在 VS Code 底部的 PROBLEMS 里。如下图所示(注意由于当时忘记截图了,图里的错误并不是本轮对话的错误)。 可见,Cline 可以读取这里的错误反馈给模型 第 2 轮完整的提示如下。包含了 6 部分。其中第 3~5 部分是告知 R1 第二个 工具, , 工具都没有执行,因为 system prompt 里要求 R1 一个轮对话只能输出一个工具调用命令。 由于本轮的指令要求非常明确,R1 直接给出了如何修改文件,调用 工具,diff 内容是 <<<<<<< SEARCH, >>>>>>> REPLACE 的格式输出。 注意这里的响应只包含了 Line 29 的 flake8 错误的修复 diff,没有包括 Line 4 的修复。因此又触发了 Cline 下一轮对话,来修复 Line 4 的错误 第 3 轮发送了 工具的执行结果:成功,但是还有 Line 4 的错误 第 3 轮完整的 prompt 如下 第 3 轮的响应内容是修复 Line 4 的 flake8 报错。但是同样包含了 2 段 来修复 chat_demo.py,以及 来更新 README.md 文件,以及 、 尝试执行脚本并完成任务。 同样的,R1 的输出被 Cline 打断了,因为包含超过 1 个工具调用。 由于 R1 模型始终没有正确的输出符合 flake8 要求的空白行数的要求,导致此后几轮都在反复修改、反复失败。这部分轮次的对话就忽略、不演示了 Cline 收到 R1 模型输出的工具调用命令,在执行时可能失败。 例如有一次 apply_diff 执行失败,Cline 会把执行结果反馈给 R1。这里的意思是:apply_diff 时会根据 R1 输出的 <<<<<<< SEARCH 内容在文件里查找,但是相似度的得分是 40%,Cline 要求达到 100%。这是 Cline 为了确保修改文件不要发生错误。 下一轮 DeepSeek 会反思错误,并使用 读文件的最新内容 后来我发现模型始终无法修复 flake8 的错误,于是手动修改了文件。 此时 Cline 发现文件发生了变化,有人工编辑并保存,因此重新发送了文件的全文。 至此,Cline 才最终完成了编程任务 最后 chat_demo.py 编辑完成,DeepSeek 决定执行 python chat_demo.py 命令来等我体验 最后一轮完成后的界面 这是 VS Code 底部 Terminal 的命令执行效果 Cline 作为一个 Agent 模式的 Coding 助手,可以自主思考、生成代码、执行工具(完成代码编辑)、感知 VS Code 上下文。 如果允许 Cline 自动执行所有操作,则它会一直运行下去直到它认为完成了任务,期间不需要人工干预。 画面很美好,代价也很高 这么简单的一个任务,累计消耗 token 数 1 百万,约花费 0.5 元。Cline 消耗 token 的速度实在太快了。 如果服务端支持 prompt cache,则可以显著的降低 API 调用成本。 Cline 的 prompt 里包含了所有 tool 的定义、例子,使用 xml 标签来管理,并没有使用 JSON 格式来定义。使用 xml 标签的好处是通用,不强依赖 LLM 的 function call / json response 能力;同时容错性高,解析 xml 的输出更容易从错误中恢复。 上述测试过程中,即使强如 DeepSeek R1,也没有准确的遵循 system prompt,生成的 response 包含了多个 tool 调用。 Cline 对此也能即使中断模型输出。虽然 Cline 声称是基于 Claude 3.5 Sonnet 的能力开发,但使用 DeepSeek R1 / V3、Gemini 都能跑起来,甚至在本地部署的 DeepSeek R1 Distill Qwen 14B 的 Q8 量化版本都可用。 Cline 对整个 Agent 工作流程的设计,也对 Agent 应用开发有启发意义,值得借鉴参考。 Cline 的 system prompt:https://github.com/cline/cline/blob/main/src/core/prompts/system.ts 本文测试的完整 prompt 和本地 proxy 源码地址:https://github.com/heycc/Cline-Agent-Prompt/blob/main/simple_https_proxy.py Your email address will not be published. Required fields are marked * Comment * Name * Email * Website Save my name, email, and website in this browser for the next time I comment. Heycc's blog I blog about AI, product, develop My Products Social