diff --git a/README.md b/README.md index 0b57d08..4570b11 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ 本课程通过 AI 代码编辑器(如 Cursor、Windsurf)进行 **Vibe Coding**,让学生体验"用自然语言编程"的全新开发方式。但更重要的是,在这个过程中**思考人与 AI 的协作边界**,培养 AI 时代真正不可替代的能力。 **技术栈**: -- 🐍 **Python 3.14+** (2026 Stable) +- 🐍 **Python 3.12+** (2026 Recommended) - ⚡ **uv** (极速 Python 项目管理) - 🤖 **DeepSeek API** + **PydanticAI** (Agent 框架) - 🎨 **Streamlit** (Data Apps) / **Chainlit** (Chatbots) @@ -40,7 +40,7 @@ ### 1. 软件安装清单 - **Code Editor**: 推荐 [Cursor](https://cursor.com) (自带 AI) 或 VS Code + Windsurf 插件, [TRAE CN](https://www.trae.cn)。 -- **Python 3.14+**: 访问 [python.org](https://www.python.org/downloads/) 下载。 +- **Python 3.12+**: 访问 [python.org](https://www.python.org/downloads/) 下载。 - **uv**: 极速 Python 包管理器 (比 pip 快 10-100 倍)。 - Mac/Linux: `curl -LsSf https://astral.sh/uv/install.sh | sh` - Windows: `powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"` @@ -48,7 +48,7 @@ ### 2. 验证环境 在终端 (Terminal) 输入以下命令,看到版本号即成功: ```bash -python --version # 应显示 Python 3.14.x +python --version # 应显示 Python 3.12.x 或更高 uv --version # 应显示 uv 0.x.x ``` @@ -245,7 +245,7 @@ class MovieScript(BaseModel): # PydanticAI 会自动识别 'deepseek:deepseek-chat' 并从环境变量加载 API Key agent = Agent( 'deepseek:deepseek-chat', - output_type=MovieScript, + result_type=MovieScript, system_prompt="你是一个好莱坞金牌编剧,擅长写出人意料的剧本。" ) @@ -396,30 +396,38 @@ st.title("✨ 魔法文案生成器") with st.sidebar: st.header("配置") style = st.selectbox("选择风格", ["文艺风", "幽默风", "硬核科技风", "发疯文学"]) - length = st.slider("字数限制", 50, 200, 100) + length = st.slider("字数限制", 5, 100, 50) -# 3. 主界面输入 -theme = st.text_input("你想写关于什么主题的文案?", placeholder="例如:周五下班、喝咖啡、新发型") +# 3. 主界面 chat intent +# 初始化聊天记录 +if "messages" not in st.session_state: + st.session_state.messages = [] -# 4. 按钮与 AI 调用 -if st.button("🪄 生成文案"): - if not theme: - st.warning("请先输入主题!") - else: - with st.spinner("AI 正在榨干脑汁..."): - try: - prompt = f"请用{style}写一段关于'{theme}'的朋友圈文案,带Emoji,{length}字以内。" - - response = client.chat.completions.create( - model="deepseek-chat", - messages=[{"role": "user", "content": prompt}] - ) - result = response.choices[0].message.content - - st.success("生成成功!") - st.markdown(f"### 📝 结果:\n{result}") - except Exception as e: - st.error(f"发生错误:{e}") +# 显示历史消息 +for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.markdown(message["content"]) + +# 4. 处理用户输入 +if theme := st.chat_input("你想写关于什么主题的文案?(例如:周五下班)"): + # 显示用户消息 + st.session_state.messages.append({"role": "user", "content": theme}) + with st.chat_message("user"): + st.markdown(theme) + + # 生成并显示助手回复 + with st.chat_message("assistant"): + prompt = f"请用{style}写一段关于'{theme}'的朋友圈文案,带Emoji,{length}字以内。" + + # 使用流式输出 (Stream) 增加"Vibe" + stream = client.chat.completions.create( + model="deepseek-chat", + messages=[{"role": "user", "content": prompt}], + stream=True + ) + response = st.write_stream(stream) + + st.session_state.messages.append({"role": "assistant", "content": response}) ``` **运行**: ```bash @@ -442,7 +450,7 @@ client = OpenAI(api_key=os.getenv("DEEPSEEK_API_KEY"), base_url="https://api.dee @cl.on_message async def main(message: cl.Message): # 1. 准备消息历史 - # (注意:实际项目中需要维护 st.session_state 或 cl.user_session 来保留上下文,这里仅演示单轮回复) + # Chainlit 会自动管理 session,但这里我们构建一个简单的历史传给 API msg_history = [ {"role": "system", "content": "你是一只高冷的猫,说话句尾要带'喵'。"}, {"role": "user", "content": message.content} @@ -459,11 +467,12 @@ async def main(message: cl.Message): stream=True ) + # 4. 实时推送到界面 for chunk in stream: if token := chunk.choices[0].delta.content: await msg.stream_token(token) - # 4. 完成更新 + # 5. 完成更新 await msg.update() ``` **运行**: @@ -558,9 +567,9 @@ AI 不是神,它需要知道你的代码、你的报错、你的意图。 - **API 报错 401?** -> 检查 API Key 是否正确,环境变量是否生效。 ### 学习资源 -- 📚 **Streamlit 文档**:[docs.streamlit.io](https://docs.streamlit.io) +- 📚 **Streamlit 文档**:[docs.streamlit.io](https://docs.streamlit.io) (推荐阅读 `st.chat_message` 和 `st.write_stream` 章节) - 🤖 **DeepSeek API**:[platform.deepseek.com](https://platform.deepseek.com) -- 📺 **教程**:B站搜索 "Streamlit 入门" 或 "Cursor 教程" +- 📺 **教程**:B站搜索 "Streamlit Chatbot" 或 "Cursor 教程" --- @@ -672,7 +681,7 @@ AI 不是神,它需要知道你的代码、你的报错、你的意图。 ## 🔗 附录:环境配置指南 ### 1. 基础环境 -- **Python**: 推荐 3.14+ (2026 Stable) +- **Python**: 推荐 3.12+ (2026 Recommended) - **依赖管理**: **uv** (强烈推荐,替代 pip/venv) - **编辑器**: 推荐 Cursor 或 VS Code + Windsurf 插件。