【无标题】

1. Langchain环境搭建-本地模型应用

langchain官网

1.1 Ollama LangChain第三方库安装

使用以下命令安装集成包:

python 复制代码
pip install langchain-ollama

使用:

python 复制代码
# 大语言模型
# 大语言模型
from langchain_ollama import ChatOllama

llm = ChatOllama(
    model="gemma3:1b",
    temperature=0,
    # other params...
)

messages = [
    (
        "system",
        "你是一个助手,请用中文回复",
    ),
    ("human","夏天适合穿什么小裙子"),
]
ai_msg = llm.invoke(messages)

print(ai_msg)
print(ai_msg.content)

2. langchain的关键对象-提示词模板

它的作用是将用户的输入或动态的数据结构嵌入到预定义的模板中 ,从而生成适合模型处理的提示词 ,提升模型输出的准确性和一致性

python 复制代码
from langchain_core.prompts import PromptTemplate

template = '你是一个{role}, 请问一个{style}的风格回答问题,回答的问题是什么{question}'

prompt = PromptTemplate.from_template(template=template)

filed_prompt = prompt.format(role ='历史老师', style='幽默风趣', question='三国是从什么时候开始的?')

ai_msg = chat.invoke(filed_prompt)

print(ai_msg.content)
python 复制代码
from langchain_core.prompts import ChatPromptTemplate

sys_template = '你是一个{role}, 请问一个{style}的风格回答问题,回答的问题是什么{question}'
user_template = '请问简单易懂的方式回答{question}'

prompt = ChatPromptTemplate.from_messages([('system', sys_template), ('human', user_template)])

sys_filed_prompt = prompt.format(role ='历史老师', style='幽默风趣', question='三国是从什么时候开始的?')

ai_msg = chat.invoke(sys_filed_prompt)

print(ai_msg.content)
相关推荐
IT_陈寒8 分钟前
用了Proxy才发现以前的JavaScript白写了
前端·人工智能·后端
甲维斯32 分钟前
Claude Opus5手搓“NewAPI Plus”首轮成果!
人工智能
程序猿DD37 分钟前
分享两个我每天都在用的 Skill,拖进豆包就能跑,限时领 30 天会员
人工智能
阿里云大数据AI技术1 小时前
Agentic Search 2.0:从单轮对话迈向企业级 AI 搜索自动驾驶Agent
人工智能·elasticsearch·agent
东风破_1 小时前
从 messages 数组到 Memory:大模型到底是怎么“记住”上一轮对话的?
人工智能
AEI1 小时前
四年间大模型的演进历程
人工智能·面试·程序员
狂师2 小时前
阿里开源:skill-up,一款Agent Skill 评测工具!
人工智能·开源·agent
武子康2 小时前
同一套 Agent Runtime,为什么 Web、Headless 和 Python SDK 仍然不是同一个产品
人工智能·llm·agent
天天代码码天天2 小时前
一个纯 C 的 OCR Runtime,又向前走了一步:lw.PPOCR.C preview.5 发布
人工智能
程序员cxuan2 小时前
Claude 官方的学习教程,太强了。
人工智能·后端·程序员