v0.0.2
- prompt
This commit is contained in:
parent
c8a1d8968d
commit
61d8ad319b
37
README.md
37
README.md
@ -1,6 +1,6 @@
|
||||
# LangChain Learning
|
||||
|
||||
[](https://github.com/your-repo/langchain-learning)
|
||||
[](https://github.com/your-repo/langchain-learning)
|
||||
[](https://www.python.org/)
|
||||
[](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
|
||||
|
||||
BIN
prompt/__pycache__/prompt_demo.cpython-313.pyc
Normal file
BIN
prompt/__pycache__/prompt_demo.cpython-313.pyc
Normal file
Binary file not shown.
58
prompt/fewshot_demo.py
Normal file
58
prompt/fewshot_demo.py
Normal file
@ -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"})
|
||||
42
prompt/prompt_demo.py
Normal file
42
prompt/prompt_demo.py
Normal file
@ -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)
|
||||
5
prompt/prompt_from_file.json
Normal file
5
prompt/prompt_from_file.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"_type": "prompt",
|
||||
"input_variables": ["topic","role"],
|
||||
"template": "请以{role}的身份介绍一下{topic}"
|
||||
}
|
||||
5
prompt/prompt_from_file.yaml
Normal file
5
prompt/prompt_from_file.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
_type: "prompt"
|
||||
input_variables:
|
||||
- topic
|
||||
- role
|
||||
template: "请以{role}的身份介绍一下{topic}"
|
||||
5
prompt/promt_from_file.py
Normal file
5
prompt/promt_from_file.py
Normal file
@ -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":"大数据专家"}))
|
||||
Loading…
Reference in New Issue
Block a user