[langchain]使用langchain构建第一个llm应用

Build a simple LLM application with chat models and prompt templates

https://python.langchain.com/docs/tutorials/llm_chain/

langchain解决了哪些问题?通过本文的一些演示可以看到,langchain至少解决了1、不同大语言模型的调用方式封装成类似的,例如openai\gemini\claude\deepseeek都是作为一个chatmodal 2、消息格式的转化3、prompt_template

ChatModels的概念

ChatModels are instances of LangChain Runnables, which means they expose a standard interface for interacting with them.
关于langchian 中的Runnable参考https://python.langchain.com/docs/concepts/runnables/

Messages的概念

from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
如果你使用了langchain里面的Message,那么你不需要手动去设置究竟是system\user\assistant\tool了。
在这里插入图片描述

代码演示

在下面的代码演示中,我们可以看到三种invoke的参数,字符串,openai格式的,还有就是langchain_core.messages下的类型


import getpass
import os
from dotenv import load_dotenv
load_dotenv()
from langchain_core.messages import SystemMessage, HumanMessage

from langchain.chat_models import init_chat_model

model = init_chat_model("deepseek-chat", model_provider="deepseek")

messages = [
    SystemMessage(content="Translate the following from English into Chinese."),
    HumanMessage(content="I love programming.")

]

result = model.invoke(messages)
print(result)

print(model.invoke("你是谁?"))

openai_format_messages = [
    {"role": "system", "content": "你是一个信达雅翻译官,把中文翻译成英文"},
    {"role": "user", "content": "这是什么?"},
 
]
print(model.invoke(openai_format_messages))

steam流式调用

import getpass
import os
from dotenv import load_dotenv
load_dotenv()
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage

from langchain.chat_models import init_chat_model

model = init_chat_model("deepseek-chat", model_provider="deepseek")



for token in model.stream("生命的意义?"):
    print(token.content, end="|", flush=True)

Prompt Templates的概念

这里只是简介,想深入了解看:https://python.langchain.com/docs/concepts/prompt_templates/

from langchain_core.prompts import ChatPromptTemplate

import getpass
import os
from dotenv import load_dotenv
load_dotenv()
from langchain_core.prompts import ChatPromptTemplate

from langchain.chat_models import init_chat_model

model = init_chat_model("deepseek-chat", model_provider="deepseek")



prompt_template = ChatPromptTemplate.from_messages(
[("system", "Translate the following from English into {language}, user will provide a sentence in <user_content> and </user_content>, you just need to translate the sentence in <user_content> and </user_content>, and return the translated sentence without any other content."), 
 ("user", "this is user want to translate <user_content> {text}</user_content>")]
)
prompt = prompt_template.invoke({"language": "Janpanse", "text": "I love programming."})

print(prompt.to_messages())

result = model.invoke(prompt.to_messages())
print(result)

プログラミングが大好きです。

langSmith显示的日志:

在这里插入图片描述

问题

好奇HumanMessage是怎么变成 {“role”: “user”, “content”: “这是什么?”}类似的格式的。


colab是啥?


一般来说一个屏幕内的代码行数大概是30行,我看30行代码不吃力,但是多余30行代码对我来说就有点难度了。


当用户说,帮我查看下我的短信内容,用户的意图是什么?以及实现该意图需要的todo

在这里插入图片描述


安全相关:
web用xss攻击
sql有sql注入
os有远程代码执行
提示词注入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值