from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
model = ChatOpenAI(
model="deepseek-r1:1.5b",
base_url="http://localhost:11434/v1",
temperature=0.7,
max_tokens=1000,
timeout=30
)
system_msg = SystemMessage(content="你是一个聊天助理")
human_msg = HumanMessage(content="你好")
messages = [system_msg, human_msg]
response = model.invoke(messages)
print(response.content)
成功跑这段代码要确保:
1、Python环境安装了langchain相关的包
2、本地通过ollama安装了 deepseek-r1:1.5b模型
3、本地网页里输入http://localhost:11434/ 页面显示 Ollama is running
变量 model 是模型对象
参数model是模型名称
参数base_url是模型地址
变量 system_msg 是系统提示词对象
变量 human_msg 是用户提示词对象
response 是模型回复对象 content是回复内容
实现多轮会话:</

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



