diff --git a/README.md b/README.md index 27ee9e1..5ce1507 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LangChain Learning -[![](https://img.shields.io/badge/version-0.0.1-blue.svg)](https://github.com/your-repo/langchain-learning) +[![](https://img.shields.io/badge/version-0.0.2-blue.svg)](https://github.com/your-repo/langchain-learning) [![](https://img.shields.io/badge/python-3.11+-green.svg)](https://www.python.org/) [![](https://img.shields.io/badge/LangChain-v1.2-orange.svg)](https://www.langchain.com/) @@ -10,6 +10,7 @@ - **多 LLM 集成**:支持 OpenAI API、SiliconFlow 及 LangChain 抽象层 - **流式响应**:实时流式输出,带来更好的使用体验 +- **Prompt 工程**:多种 Prompt 模板构建方式 - **实战示例**:从基础到进阶的使用模式 ## 快速开始 @@ -31,25 +32,22 @@ SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1 ### 3. 运行示例 -**直接调用 API(requests)** -```bash -python helloworld/helloworld.py -``` +**Hello World 示例** -**LangChain + ChatOpenAI 接口** -```bash -python helloworld/helloworld_langchain_openai.py -``` +| 示例 | 命令 | 说明 | +|------|------|------| +| 直接调用 API | `python helloworld/helloworld.py` | 使用 requests 直接调用 SiliconFlow API | +| LangChain + ChatOpenAI | `python helloworld/helloworld_langchain_openai.py` | 通过 OpenAI 接口调用 LLM | +| LangChain + ChatSiliconFlow | `python helloworld/helloworld_siliconflow.py` | 使用 LangChain SiliconFlow 集成 | +| OpenAI 客户端 + SiliconFlow | `python helloworld/openai_siliconflow.py` | OpenAI 客户端兼容 SiliconFlow | -**LangChain + ChatSiliconFlow** -```bash -python helloworld/helloworld_siliconflow.py -``` +**Prompt 示例** -**OpenAI 客户端 + SiliconFlow** -```bash -python helloworld/openai_siliconflow.py -``` +| 示例 | 命令 | 说明 | +|------|------|------| +| PromptTemplate | `python prompt/prompt_demo.py` | 演示 PromptTemplate 模板构建 | +| Few-shot Learning | `python prompt/fewshot_demo.py` | 带示例的少样本提示学习 | +| 从文件加载 Prompt | `python prompt/promt_from_file.py` | 从 YAML 文件加载提示词模板 | ## 项目结构 @@ -60,6 +58,11 @@ langchain-learning/ │ ├── helloworld_langchain_openai.py # LangChain + ChatOpenAI │ ├── helloworld_siliconflow.py # LangChain + ChatSiliconFlow │ └── openai_siliconflow.py # OpenAI 客户端 + SiliconFlow +├── prompt/ +│ ├── prompt_demo.py # PromptTemplate 模板示例 +│ ├── fewshot_demo.py # Few-shot Learning 示例 +│ ├── promt_from_file.py # 从文件加载 Prompt +│ └── prompt_from_file.yaml # Prompt 模板文件 ├── main.py # 入口文件 ├── pyproject.toml # 项目配置 └── README.md diff --git a/prompt/__pycache__/prompt_demo.cpython-313.pyc b/prompt/__pycache__/prompt_demo.cpython-313.pyc new file mode 100644 index 0000000..c1e8ce5 Binary files /dev/null and b/prompt/__pycache__/prompt_demo.cpython-313.pyc differ diff --git a/prompt/fewshot_demo.py b/prompt/fewshot_demo.py new file mode 100644 index 0000000..70a9fae --- /dev/null +++ b/prompt/fewshot_demo.py @@ -0,0 +1,58 @@ +import logging + +from langchain_core.prompts import FewShotChatMessagePromptTemplate, ChatPromptTemplate +from langchain_openai import ChatOpenAI +import os +import dotenv + +dotenv.load_dotenv() + +## 设置环境变量 +os.environ['OPENAI_API_KEY'] = os.getenv("SILICONFLOW_API_KEY") +os.environ['OPENAI_BASE_URL'] = os.getenv("SILICONFLOW_BASE_URL") + +# 默认的 'model_name': 'deepseek-ai/DeepSeek-V3.1', +llm = ChatOpenAI(model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B") + +logging.basicConfig( + level=logging.DEBUG, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" +) + +examples = [ + {"input": "1 || 1", "output": "2"}, + {"input": "1 || 2", "output": "3"}, + {"input": "1 || 3", "output": "4"} +] + +example_prompt = ChatPromptTemplate.from_messages( + [ + ("human", "{input}"), + ("ai", "{output}"), + ] +) + +few_show_prompt = FewShotChatMessagePromptTemplate( + examples=examples, + example_prompt=example_prompt +) + + +final_prompt = ChatPromptTemplate.from_messages( + [ + ("system","你是一个数学天才"), + few_show_prompt, + ("human", "{input}") + ] +) +# question = final_prompt.invoke(input = {"input":"1 || 10"}) +# # llm : 1||10 ? +# response = llm.invoke(question) +# print(response) +# 链式调用 +llm.invoke(final_prompt.invoke(input = {"input":"1 || 10"})) + + +# 提示词的invoke输出给到了llm作为输入,和管道的概念一模一样 +chain = final_prompt | llm +chain.invoke(input = {"input":"1 || 10"}) \ No newline at end of file diff --git a/prompt/prompt_demo.py b/prompt/prompt_demo.py new file mode 100644 index 0000000..eb11ae9 --- /dev/null +++ b/prompt/prompt_demo.py @@ -0,0 +1,42 @@ +import logging + +from langchain_core.messages import SystemMessage, HumanMessage +from langchain_core.prompts import PromptTemplate +import langchain + +from langchain_openai import ChatOpenAI +import os +import dotenv + +dotenv.load_dotenv() + +## 设置环境变量 +os.environ['OPENAI_API_KEY'] = os.getenv("SILICONFLOW_API_KEY") +os.environ['OPENAI_BASE_URL'] = os.getenv("SILICONFLOW_BASE_URL") + +# 默认的 'model_name': 'deepseek-ai/DeepSeek-V3.1', +llm = ChatOpenAI(model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B") + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" +) +print(langchain.__version__) + +## prompt +system_message = SystemMessage( + content="你是一个大数据方向的专家,用户提问时,你只需要精简的回答问题,回答内容不超过100个token") +human_message = HumanMessage(content="我现在想要学习hive,你帮我指定一个学习计划把") +message = [system_message, human_message] + +print(human_message) + +## 1. 创建PromptTemplate +template = PromptTemplate.from_template(template="我现在想要学习{topic}和{topic2},你帮我指定一个学习计划把") +## 2. 构建完整的提示词 +hadoop_prompt = template.format(topic="hadoop",topic2="spark") +hadoop_prompt2 = template.invoke(input={"topic":"hadoop","topic2":"spark"}) +print(hadoop_prompt) +print(hadoop_prompt2) +# response = llm.invoke(hadoop_prompt) +# print(response) diff --git a/prompt/prompt_from_file.json b/prompt/prompt_from_file.json new file mode 100644 index 0000000..f078488 --- /dev/null +++ b/prompt/prompt_from_file.json @@ -0,0 +1,5 @@ +{ + "_type": "prompt", + "input_variables": ["topic","role"], + "template": "请以{role}的身份介绍一下{topic}" +} \ No newline at end of file diff --git a/prompt/prompt_from_file.yaml b/prompt/prompt_from_file.yaml new file mode 100644 index 0000000..cbab758 --- /dev/null +++ b/prompt/prompt_from_file.yaml @@ -0,0 +1,5 @@ +_type: "prompt" +input_variables: + - topic + - role +template: "请以{role}的身份介绍一下{topic}" \ No newline at end of file diff --git a/prompt/promt_from_file.py b/prompt/promt_from_file.py new file mode 100644 index 0000000..bc396bc --- /dev/null +++ b/prompt/promt_from_file.py @@ -0,0 +1,5 @@ +from langchain_core.prompts import load_prompt + +prompt = load_prompt("prompt_from_file.yaml",encoding="utf-8") +print(prompt.format(topic = "spark",role = "老师")) +print(prompt.invoke(input = {"topic":"hive","role":"大数据专家"}))