No project description provided
Project description
langchain-tengits
langchain-tengits 是一个用于集成多种大语言模型和嵌入模型的 LangChain 社区包,支持多种模型服务商,提供统一的 API 接口。
功能特性
- 🤖 多模型支持: 支持多种大语言模型和嵌入模型
- 🌐 多服务商集成: 支持 OpenAI、阿里百炼、DeepSeek、SiliconFlow、百度千帆、Moonshot、智谱、Ollama、火山引擎等
- 🔄 同步/异步调用: 支持同步和异步调用方式
- 📡 流式输出: 支持流式响应输出
- ⚙️ 灵活配置: 支持自定义模型配置和参数
- 🔧 LangChain 集成: 完全兼容 LangChain 生态系统
Table of Contents
Installation
pip install langchain-tengits
Quick Start
聊天模型使用示例
import asyncio
from langchain_tengits.chat_tengits import TengitsChatClient
# 创建聊天客户端
model = TengitsChatClient(
server_name='ali_bailian', # 服务商名称
model_name='qwen-plus', # 模型名称
base_url="http://localhost:8000", # 后端服务地址
config={
"extra_body": {"enable_thinking": True} # 额外配置
},
client_timeout=30.0 # 请求超时时间
)
# 同步调用
def test_sync():
response = model.invoke([
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好,请介绍一下自己"}
])
print("同步响应:", response.content)
# 流式输出
def test_stream():
print("流式输出:")
for chunk in model.stream([
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "请用3句话介绍人工智能"}
]):
print(chunk.content, end="", flush=True)
print()
# 异步流式输出
async def test_async():
print("异步流式输出:")
async for chunk in model.astream([
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "请解释什么是机器学习"}
]):
print(chunk.content, end="", flush=True)
print()
# 执行测试
test_sync()
test_stream()
asyncio.run(test_async())
嵌入模型使用示例
from langchain_tengits.embedding_tengits import TengitsEmbeddingClient
# 创建嵌入客户端
embedding_model = TengitsEmbeddingClient(
model_name='BAAI/bge-m3', # 模型名称
server_name='siliconflow', # 服务商名称
base_url="http://localhost:8000", # 后端服务地址
client_timout=30.0 # 请求超时时间
)
# 单文本嵌入
def test_single_embedding():
text = "这是一个测试文本"
embedding = embedding_model.embed_query(text)
print(f"文本: {text}")
print(f"嵌入向量维度: {len(embedding)}")
print(f"嵌入向量前5个值: {embedding[:5]}")
# 批量文本嵌入
def test_batch_embedding():
texts = [
"人工智能是未来的发展方向",
"机器学习是人工智能的子领域",
"深度学习推动了AI的快速发展"
]
embeddings = embedding_model.embed_documents(texts)
print(f"批量处理 {len(texts)} 个文本")
for i, embedding in enumerate(embeddings):
print(f"文本 {i+1} 向量维度: {len(embedding)}")
# 执行测试
test_single_embedding()
test_batch_embedding()
API Reference
聊天模型 (TengitsChatClient)
参数说明
base_url(str, 必需): 后端服务地址server_name(str, 必需): 模型服务商名称model_name(str, 必需): 模型名称model_id(int, 可选): 模型ID,如果不提供会自动获取config(dict, 可选): 模型配置参数client_timeout(float, 可选): 请求超时时间(秒)
主要方法
invoke(messages): 同步调用,返回完整响应stream(messages): 同步流式调用,返回生成器astream(messages): 异步流式调用,返回异步生成器
嵌入模型 (TengitsEmbeddingClient)
参数说明
base_url(str, 必需): 后端服务地址server_name(str, 必需): 模型服务商名称model_name(str, 必需): 模型名称client_timout(float, 可选): 请求超时时间(秒),默认30秒
主要方法
embed_query(text): 获取单个文本的嵌入向量embed_documents(texts): 批量获取多个文本的嵌入向量
Supported Providers
目前支持的模型服务商包括:
openai: OpenAI 官方APIali_bailian: 阿里云百炼deepseek: DeepSeeksiliconflow: SiliconFlowbaidu_qianfan: 百度千帆moonshot: Moonshot AIzhipu: 智谱AIollama: Ollamavolcengine: 火山引擎
Configuration
聊天模型配置示例
config = {
"stream": True, # 启用流式输出
"temperature": 0.7, # 温度参数,控制随机性
"max_tokens": 1000, # 最大输出token数
"extra_body": { # 额外请求体参数
"enable_thinking": True, # 启用思考模式
# 其他自定义参数...
},
"extra_headers": { # 额外HTTP头
"X-Custom-Header": "value"
}
}
错误处理
try:
response = model.invoke(messages)
print(response.content)
except Exception as e:
print(f"调用失败: {e}")
发布流程
-
构建包:
hatch build -
发布到测试PyPI:
hatch publish -r test -u __token__
然后输入 TestPyPI 的账号 token
-
从测试PyPI安装:
pip install -i https://test.pypi.org/simple/ langchain-tengits
License
langchain-tengits is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
langchain_tengits-0.0.3.tar.gz
(11.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_tengits-0.0.3.tar.gz.
File metadata
- Download URL: langchain_tengits-0.0.3.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aecebfc501eb6f8cc8cd0b34f087ac7f25b770e0610976b2a64ac6e5f1646c4f
|
|
| MD5 |
497ebbfe2541c1a04f223d0ac4039273
|
|
| BLAKE2b-256 |
96d5b060f6ab04dcb3cca10ed517a8cd7a250f8c7bb70c663b2503d8c0ae3fe6
|
File details
Details for the file langchain_tengits-0.0.3-py3-none-any.whl.
File metadata
- Download URL: langchain_tengits-0.0.3-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a24ba865d049d1e8be00c812d30b972904c56c753e1052d29580dafd44aada8
|
|
| MD5 |
dc481abec15075627dc652addc880cb2
|
|
| BLAKE2b-256 |
b6b502bb9efa0a0fc5bcd500dd3aac69d0415c209252276b947163dcb98eb206
|