diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d9a9e7e --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +OPENAI_API_KEY=your_openai_api_key \ No newline at end of file diff --git a/01 Agent 入门 & 快速运行/Agent-demo.py b/01 Agent 入门 & 快速运行/Agent-demo.py new file mode 100644 index 0000000..3a9a2ab --- /dev/null +++ b/01 Agent 入门 & 快速运行/Agent-demo.py @@ -0,0 +1,21 @@ +import os # 导入环境变量 +from dotenv import load_dotenv + +load_dotenv() +api_key = os.getenv("OPENAI_API_KEY") + + +# 让ai说一句话 +from langchain_openai import ChatOpenAI + +# 配置deepseek +llm = ChatOpenAI( + model="deepseek-chat", + api_key=api_key, + # 注:在.env里把OPENAI_API_KEY改成你自己的api-key即可 + base_url="https://api.deepseek.com" +) + +# 调用模型 +response = llm.invoke("你好呀") +print(response.content) \ No newline at end of file diff --git a/02 LLM基础与调用/1. llm基础调用.py b/02 LLM基础与调用/1. llm基础调用.py new file mode 100644 index 0000000..1c1a4d8 --- /dev/null +++ b/02 LLM基础与调用/1. llm基础调用.py @@ -0,0 +1,23 @@ +import os # 导入环境变量 +from dotenv import load_dotenv + +load_dotenv() +api_key = os.getenv("OPENAI_API_KEY") + + +from openai import OpenAI + +client = OpenAI( + api_key=api_key, + base_url="https://api.deepseek.com") + +response = client.chat.completions.create( + model="deepseek-chat", + messages=[ + {"role": "system", "content": "You are a helpful assistant"}, # 提示词角色 + {"role": "user", "content": "Hello"}, # 用户输入的对话 + ], + stream=False # 非流式输出,只会等语句全部生成才返回 +) + +print(response.choices[0].message.content) \ No newline at end of file diff --git a/02 LLM基础与调用/2. 问答agent--多轮对话.py b/02 LLM基础与调用/2. 问答agent--多轮对话.py new file mode 100644 index 0000000..4c8dc42 --- /dev/null +++ b/02 LLM基础与调用/2. 问答agent--多轮对话.py @@ -0,0 +1,36 @@ +import os # 导入环境变量 +from dotenv import load_dotenv +from openai import OpenAI + +load_dotenv() + +def create_client(): + api_key = os.getenv("OPENAI_API_KEY") + return OpenAI(api_key=api_key,base_url="https://api.deepseek.com") + +def chat_loop(agent_client): + messages = [ + {"role":"system","content":"你是一个历史老师,你会耐心的教导我有关的事情。同时你的回答会相对精简,在五十字内。"}, + {"role":"user","content":"汉朝存在了多久,其中哪个皇帝你认为最厉害?"} + ] + while 1: + response = agent_client.chat.completions.create( + model="deepseek-chat", + messages=messages + ) + answer = response.choices[0].message.content + print(f'回答:{answer}') + + # 询问是否还有其他问题 + user_input =input('您还有其他想继续问的吗 | (exit退出)\n') + if user_input == "exit": + break + + # 继续记录对话(涵盖用户追问 + ai上句回答) + messages.append({"role":"user","content":user_input}) + messages.append({"role":"assistant","content":answer}) + + +if __name__ == '__main__': + client = create_client() + chat_loop(client) diff --git a/README.md b/README.md index 6d1187b..5260c0b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,85 @@ -# ai-agent-knowledege -AI知识仓库,包含博客配套展示代码 +# 💻 Agent Craft + +> 智能体开发教学库 | A beginner-friendly guide to building AI agents + + +🔥 从零开始上手agent智能体,掌握langchain、rag、prompt、langgraph等核心技术栈 + +本仓库专注于 AI Agent 开发的学习与实践,通过系统化的案例和详细的代码注释,帮助开发者快速掌握从基础LLM调用到复杂Agent构建的完整技能栈。 + +💡 项目将持续更新,目标是构建一个系统、实用的 AI Agent 学习案例库。 + +📘 本仓库开箱即用,仅需配置 API_KEY。 + +✍️ 代码可与博客配套使用,博客会重点讲解Agent知识构建与流程解析。 + +博客同步发布: + +👉 [我的CSDN主页](https://blog.csdn.net/2401_87328929) + +👉 [CSDN对应专栏](https://blog.csdn.net/2401_87328929/category_12970267.html) + +🙋‍♂️ 技术交流 / 更新 / 答疑:微信 **a19731567148**(备注 GitHub) + +--- +## 🚅 目录跳转 + +| 仓库 | 博客 | 技术栈 | 知识点 | 难度 | +|---------------------------------------------------|------------------------------------------------|------------------|----------------------|-----| +| [01 Agent 入门 & 快速运行](01%20Agent%20入门%20&%20环境搭建/) | [🏠](01%20Agent%20入门%20&%20环境搭建/Agent-demo.py) | 基础LLM | Agent概念 | ⭐ | +| [02 LLM基础与调用](02%20LLM基础与调用/1.%20llm基础调用.py) | [🏠](02%20LLM基础与调用/1.%20llm基础调用.py) | LLM/API调用/Prompt | 调用模型接口、初识prompt、多轮对话 | ⭐ | + + +--- + +## 🚀 快速开始 + +### 1️⃣ 环境准备 + +```bash +# 克隆项目 +git clone https://github.com/Annyfee/agent-craft.git +cd agent-craft + +# 安装依赖 +pip install -r requirements.txt +``` + +### 2️⃣ API Key 配置 + +```bash +# 复制环境变量模板 +cp .env.example .env +``` + +在 `.env` 文件中配置: +```env +OPENAI_API_KEY=your_deepseek_api_key_here +``` + +> 💡 **获取API Key**: 访问 [DeepSeek官网](https://platform.deepseek.com/) 注册并获取API Key + +### 3️⃣ 运行示例 + +```bash +python "01 Agent 入门 & 环境搭建/Agent-demo.py" +``` +--- + +## 📚 学习路径 + +### 🎯 第一章:Agent 入门 & 快速运行 +- **核心技能**: 运行Agent-demo +- **学习目标**: 了解Agent是什么,怎么用 +- **技术要点**: 环境配置 + +### 🎯 第二章:LLM基础与调用 +- **核心技能**: 多轮对话Agent构建 +- **学习目标**: 实现智能对话系统 +- **技术要点**: 对话历史、上下文管理、角色设定 + +## 🤝 贡献指南 +欢迎贡献代码 / 提交 Issue / Pull Request +- Fork 仓库 +- 创建分支进行修改 +- 提交 PR 并描述修改内容 diff --git a/main.py b/main.py deleted file mode 100644 index 5596b44..0000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# This is a sample Python script. - -# Press Shift+F10 to execute it or replace it with your code. -# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. - - -# Press the green button in the gutter to run the script. -if __name__ == '__main__': - print_hi('PyCharm') - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4da8042 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +dotenv~=0.9.9 +python-dotenv~=1.1.1 +openai~=2.6.0 \ No newline at end of file