Langchain使用DeepSeek模型
在langchain使用deepseek模型进行推理,llm开发
·
Langchain使用DeepSeek模型
title: Langchain使用DeepSeek模型
tags: [RAG, LLM]
date: 2025-1-28 0:56:00
1、什么是Langchain
Langchain本质上是大模型开发的一个框架(framework),它通过连接LLM和其他组件,如向量数据库、工具、提示工程,为应用程序赋能,使得应用程序能够具备上下文意识和推理能力。
2、安装
2.1 直接使用pip安装
pip install langchain-deepseek
2.2 从源码安装
git clone https://github.com/SyJarvis/langchain-deepseek
cd langchain_deepseek
pip install -e .
3、使用
3.0 前提
创建一个.env文件,写入api_key
DEEPSEEK_API_KEY = "sk-..."
import os
from dotenv import load_dotenv, find_dotenv
# .env 存储api_key
load_dotenv()
密钥可以从deepseek官网获取,链接如下:
- https://platform.deepseek.com
3.1 快速使用
from langchain_deepseek import ChatDeepSeek
llm = ChatDeepSeek(
model="deepseek-chat"
)
response = llm.invoke("9.11和9.6哪个大")
print(response.content)
3.2 进阶使用
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are the technical writer"),
("user", "{input}") # {input}为变量
])
# 我们可以把prompt和具体llm的调用和在一起(通过chain,chain可以理解为sequence of calls to take)
api_key = "sk-..."
llm = ChatDeepSeek(
model="deepseek-chat",
api_key=api_key
)
chain = prompt | llm
response = chain.invoke({"input": "9.11和9.6哪个大"})
print(response.content)
from langchain_core.output_parsers import StrOutputParser
output_parser = StrOutputParser() # 输出string
chain = prompt | llm | output_parser
content = chain.invoke({"input": "9.11和9.6哪个大"})
print(content)
4、参考资料
- https://github.com/SyJarvis/langchain-deepseek
- https://python.langchain.com/docs/introduction/
- https://api-docs.deepseek.com/zh-cn/
更多推荐




所有评论(0)