- helloworld
This commit is contained in:
kennethcheng 2026-04-12 17:39:33 +08:00
commit c8a1d8968d
11 changed files with 2310 additions and 0 deletions

4
.env Normal file
View File

@ -0,0 +1,4 @@
# Qwen/Qwen3.5-4B
# deepseek-ai/DeepSeek-R1-0528-Qwen3-8B
SILICONFLOW_API_KEY = "sk-sylilrjrtxlvecwhfusjkutclmppzuzhncfcfxtekxrzyjee"
SILICONFLOW_BASE_URL = "https://api.siliconflow.cn/v1"

1
.python-version Normal file
View File

@ -0,0 +1 @@
3.11

83
README.md Normal file
View File

@ -0,0 +1,83 @@
# 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/python-3.11+-green.svg)](https://www.python.org/)
[![](https://img.shields.io/badge/LangChain-v1.2-orange.svg)](https://www.langchain.com/)
> LangChain 框架学习项目,集成 SiliconFlow API
## 功能特性
- **多 LLM 集成**:支持 OpenAI API、SiliconFlow 及 LangChain 抽象层
- **流式响应**:实时流式输出,带来更好的使用体验
- **实战示例**:从基础到进阶的使用模式
## 快速开始
### 1. 安装依赖
```bash
pip install langchain>=1.2.15 langchain-community>=0.4.1 langchain-siliconflow>=1.0.0 requests>=2.33.1
```
### 2. 配置环境变量
在项目根目录创建 `.env` 文件:
```env
SILICONFLOW_API_KEY=your_api_key_here
SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1
```
### 3. 运行示例
**直接调用 APIrequests**
```bash
python helloworld/helloworld.py
```
**LangChain + ChatOpenAI 接口**
```bash
python helloworld/helloworld_langchain_openai.py
```
**LangChain + ChatSiliconFlow**
```bash
python helloworld/helloworld_siliconflow.py
```
**OpenAI 客户端 + SiliconFlow**
```bash
python helloworld/openai_siliconflow.py
```
## 项目结构
```
langchain-learning/
├── helloworld/
│ ├── helloworld.py # 直接调用 SiliconFlow API
│ ├── helloworld_langchain_openai.py # LangChain + ChatOpenAI
│ ├── helloworld_siliconflow.py # LangChain + ChatSiliconFlow
│ └── openai_siliconflow.py # OpenAI 客户端 + SiliconFlow
├── main.py # 入口文件
├── pyproject.toml # 项目配置
└── README.md
```
## 可用模型
- `deepseek-ai/DeepSeek-R1-0528-Qwen3-8B`
- `Qwen/Qwen3.5-4B`
## 技术栈
| 类别 | 技术 |
|------|------|
| 框架 | LangChain |
| LLM 提供商 | SiliconFlow |
| 语言 | Python 3.11+ |
## 许可证
MIT License

Binary file not shown.

21
helloworld/helloworld.py Normal file
View File

@ -0,0 +1,21 @@
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
payload = {
"model": "Qwen/Qwen3.5-4B",
"messages": [
{
"role": "user",
"content": "你是谁"
}
]
}
headers = {
"Authorization": "Bearer sk-sylilrjrtxlvecwhfusjkutclmppzuzhncfcfxtekxrzyjee", #填写自己的api-key
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())

View File

@ -0,0 +1,21 @@
import logging
from langchain_openai import ChatOpenAI
import os
import dotenv
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
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")
response = llm.invoke("一句话介绍一下你自己50字以内")
print(response)

View File

@ -0,0 +1,27 @@
import logging
from langchain_siliconflow import ChatSiliconFlow
import os
import dotenv
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
dotenv.load_dotenv()
## 设置环境变量
os.environ['SILICONFLOW_API_KEY'] = os.getenv("SILICONFLOW_API_KEY")
os.environ['SILICONFLOW_BASE_URL'] = os.getenv("SILICONFLOW_BASE_URL")
# 默认的 'model_name': 'deepseek-ai/DeepSeek-V3.1',
llm = ChatSiliconFlow(model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B")
for chunk in llm.stream("一句话介绍一下你自己50字以内"):
print(chunk.text, end="", flush=True)
# response = llm.invoke("一句话介绍一下你自己50字以内")
# print(response)

View File

@ -0,0 +1,28 @@
from openai import OpenAI
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")
client = OpenAI()
response = client.chat.completions.create(
# model='Pro/deepseek-ai/DeepSeek-R1',
model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
messages=[
{'role': 'user',
'content': "推理模型会给市场带来哪些新的机会"}
],
stream=True
)
for chunk in response:
if not chunk.choices:
continue
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.choices[0].delta.reasoning_content:
print(chunk.choices[0].delta.reasoning_content, end="", flush=True)

6
main.py Normal file
View File

@ -0,0 +1,6 @@
def main():
print("Hello from langchain-learning!")
if __name__ == "__main__":
main()

12
pyproject.toml Normal file
View File

@ -0,0 +1,12 @@
[project]
name = "langchain-learning"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"langchain>=1.2.15",
"langchain-community>=0.4.1",
"langchain-siliconflow>=1.0.0",
"requests>=2.33.1",
]

2107
uv.lock Normal file

File diff suppressed because it is too large Load Diff