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有远程代码执行
提示词注入