# 代码助手 **Repository Path**: lhdxhl/code-assistant ## Basic Information - **Project Name**: 代码助手 - **Description**: 代码助手主要使用Pyhton开发,使用langchain集成deepseek大模型平台开发 - **Primary Language**: Python - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-13 - **Last Updated**: 2026-01-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DeepSeek Code Assistant 一个基于 LangChain 和 DeepSeek API 的智能代码助手,为开发者提供代码生成、审查、调试、优化等功能。 ## 功能特性 - **💬 智能对话**: 与代码助手进行自然语言对话,获取编程相关建议 - **🔧 代码生成**: 根据任务描述生成高质量的代码 - **🔍 代码审查**: 审查代码并提供改进建议 - **📖 代码解释**: 详细解释代码的功能和工作原理 - **🐛 代码调试**: 帮助定位和修复代码中的错误 - **⚡ 代码优化**: 优化代码性能,提供改进方案 - **💾 对话记忆**: 保持对话上下文,支持连续对话 ## 安装 ### 1. 克隆或下载项目 ```bash git clone https://gitee.com/lhdxhl/code-assistant.git cd code-assistant ``` ### 2. 安装依赖 ```bash pip install -r requirements.txt ``` ### 3. 配置环境变量 复制示例配置文件: ```bash cp .env.example .env ``` 编辑 `.env` 文件,填入你的 DeepSeek API 密钥: ```env DEEPSEEK_API_KEY=your_actual_api_key_here DEEPSEEK_BASE_URL=https://api.deepseek.com DEEPSEEK_MODEL=deepseek-chat DEEPSEEK_TEMPERATURE=0.7 DEEPSEEK_MAX_TOKENS=2000 ``` ## 快速开始 ### Web 界面使用(推荐) #### 1. 启动 Web 服务器 ```bash python app.py ``` 服务器将在 http://localhost:5000 启动。 #### 2. 访问 Web 界面 在浏览器中打开 http://localhost:5000,您将看到一个美观的交互式界面,包括: - **聊天区域**: 与代码助手进行实时对话 - **代码工具**: 快速使用各种代码功能 - 🔧 代码生成 - 🔍 代码审查 - 📖 代码解释 - 🐛 代码调试 - ⚡ 代码优化 #### 3. 使用功能 - 在聊天框输入问题,按 Enter 或点击"发送"按钮 - 使用右侧工具栏快速执行特定任务 - 点击"清空对话记忆"清除对话历史 ### 命令行使用 ```python from code_assistant import DeepSeekCodeAssistant # 初始化助手 assistant = DeepSeekCodeAssistant() # 发送消息 response = assistant.chat("什么是 Python 中的装饰器?") print(response) ``` ### 代码生成 ```python # 生成代码 code = assistant.generate_code( task_description="创建一个计算斐波那契数列的函数", language="Python" ) print(code) ``` ### 代码审查 ```python # 审查代码 code_to_review = """ def process_data(items): result = [] for item in items: result.append(item * 2) return result """ review = assistant.review_code(code_to_review, language="Python") print(review) ``` ### 代码解释 ```python # 解释代码 code_to_explain = """ numbers = [1, 2, 3, 4, 5] squared = [x**2 for x in numbers if x % 2 == 0] """ explanation = assistant.explain_code(code_to_explain, language="Python") print(explanation) ``` ### 代码调试 ```python # 调试代码 buggy_code = """ def divide(a, b): return a / b """ debug_help = assistant.debug_code( buggy_code, error_message="ZeroDivisionError: division by zero", language="Python" ) print(debug_help) ``` ### 代码优化 ```python # 优化代码 code_to_optimize = """ def find_max(numbers): max_val = 0 for n in numbers: if n > max_val: max_val = n return max_val """ optimized = assistant.optimize_code(code_to_optimize, language="Python") print(optimized) ``` ### 对话历史管理 ```python # 获取对话历史 history = assistant.get_conversation_history() for message in history: print(f"{message.type}: {message.content}") # 清空对话记忆 assistant.clear_memory() ``` ## 运行示例 项目包含一个完整的示例脚本,展示所有功能: ```bash python example_usage.py ``` ## API 参考 ### DeepSeekCodeAssistant 类 #### 初始化参数 ```python DeepSeekCodeAssistant( api_key: Optional[str] = None, # DeepSeek API 密钥 base_url: Optional[str] = None, # API 基础 URL model: Optional[str] = None, # 模型名称 temperature: Optional[float] = None, # 温度参数 (0.0-1.0) max_tokens: Optional[int] = None # 最大生成 token 数 ) ``` #### 主要方法 | 方法 | 描述 | |------|------| | `chat(message: str)` | 发送消息并获取响应 | | `generate_code(task_description, language, context)` | 生成代码 | | `review_code(code, language)` | 审查代码 | | `explain_code(code, language)` | 解释代码 | | `debug_code(code, error_message, language)` | 调试代码 | | `optimize_code(code, language)` | 优化代码 | | `clear_memory()` | 清空对话记忆 | | `get_conversation_history()` | 获取对话历史 | ## 配置说明 ### 环境变量 | 变量名 | 描述 | 默认值 | |--------|------|--------| | `DEEPSEEK_API_KEY` | DeepSeek API 密钥(必需) | - | | `DEEPSEEK_BASE_URL` | API 基础 URL | https://api.deepseek.com | | `DEEPSEEK_MODEL` | 使用的模型 | deepseek-chat | | `DEEPSEEK_TEMPERATURE` | 生成温度(0.0-1.0) | 0.7 | | `DEEPSEEK_MAX_TOKENS` | 最大 token 数 | 2000 | ### 温度参数说明 - **0.0 - 0.3**: 更确定性,适合需要精确答案的场景 - **0.4 - 0.7**: 平衡创造性和准确性(推荐) - **0.8 - 1.0**: 更创造性,适合创意性任务 ## 项目结构 ``` znkf/ ├── code_assistant.py # 核心代码助手模块 ├── example_usage.py # 使用示例 ├── requirements.txt # Python 依赖 ├── .env.example # 环境变量示例 ├── .env # 环境变量配置(需自行创建) └── README.md # 项目文档 ``` ## 技术栈 - **LangChain**: 用于构建 LLM 应用的框架 - **DeepSeek API**: 提供大语言模型服务 - **python-dotenv**: 环境变量管理 ## 常见问题 ### 1. 如何获取 DeepSeek API 密钥? 访问 [DeepSeek 官网](https://platform.deepseek.com/) 注册账号并获取 API 密钥。 ### 2. 遇到 API 密钥错误怎么办? 确保: - `.env` 文件已正确创建 - `DEEPSEEK_API_KEY` 已设置为有效的 API 密钥 - API 密钥没有多余的空格或引号 ### 3. 如何调整响应的创造性? 通过修改 `.env` 文件中的 `DEEPSEEK_TEMPERATURE` 参数: - 较低值(如 0.3)获得更确定的答案 - 较高值(如 0.8)获得更多样化的回答 ### 4. 对话历史会一直保存吗? 不会。对话历史保存在内存中,当: - 调用 `clear_memory()` 方法 - 程序结束时 都会被清空。如需持久化,需要自行实现存储逻辑。 ## 高级用法 ### 自定义提示词 ```python from code_assistant import DeepSeekCodeAssistant # 创建自定义系统提示 assistant = DeepSeekCodeAssistant() # 通过 chat 方法发送特定指令 response = assistant.chat( "请以资深架构师的角度,评估以下设计的优缺点..." ) ``` ### 多轮对话 ```python assistant = DeepSeekCodeAssistant() # 第一轮对话 response1 = assistant.chat("什么是设计模式?") print(response1) # 第二轮对话(会记住之前的上下文) response2 = assistant.chat("请举一个单例模式的例子") print(response2) ``` ### 批量代码审查 ```python code_files = ["file1.py", "file2.py", "file3.py"] for file in code_files: with open(file, 'r') as f: code = f.read() review = assistant.review_code(code, language="Python") print(f"Review for {file}:\n{review}\n") ``` ## 许可证 MIT License ## 贡献 欢迎提交 Issue 和 Pull Request! ## 联系方式 如有问题或建议,请通过以下方式联系: - 提交 Issue - 发送邮件 ## 更新日志 ### v1.0.0 (2025-01-13) - 初始版本发布 - 支持代码生成、审查、解释、调试、优化 - 支持对话记忆功能 - 提供完整的示例和文档