问题背景
平常我们设计程序的时候,会这么写:
// 随便举例
String type = paramDTO.getType();
if (type.equals("吃饭")) {
// do
} else if (type.equals("喝水")) {
// do
} else {
// do
}
此时如果type传入的不是数字,那我们就没法处理。
比如用户说:我想喝水
,此时我们的程序就无法进行了,会走到else
分支下。
那我们该如何处理这种问题?借助大模型的推理能力,可以帮助理解用户的问题,并推理出对应的方案。
安装依赖
pip install --upgrade --quiet langchain-core langchain langchain-openai
编写代码
定义两个方案的Prompt
模板
# 物理模板
physics_template = """You are a very smart physics professor. \
You are great at answering questions about physics in a concise and easy to understand manner. \
When you don't know the answer to a question you admit that you don't know.
Here is a question:
{query}"""
# 数学模板
math_template = """You are a very good mathematician. You are great at answering math questions. \
You are so good because you are able to break down hard problems into their component parts, \
answer the component parts, and then put them together to answer the broader question.
Here is a question:
{query}"""
定义好我们的Chain
之后,丢出两个问题将进行测试
message1 = chain.invoke("What is the speed of light?")
print(f"message1: {message1}")
message2 = chain.invoke("什么是微积分?")
print(f"message2: {message2}")
完整的代码如下:
from langchain.utils.math import cosine_similarity
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
physics_template = """You are a very smart physics professor. \
You are great at answering questions about physics in a concise and easy to understand manner. \
When you don't know the answer to a question you admit that you don't know.
Here is a question:
{query}"""
math_template = """You are a very good mathematician. You are great at answering math questions. \
You are so good because you are able to break down hard problems into their component parts, \
answer the component parts, and then put them together to answer the broader question.
Here is a question:
{query}"""
embeddings = OpenAIEmbeddings()
prompt_templates = [physics_template, math_template]
prompt_embeddings = embeddings.embed_documents(prompt_templates)
def prompt_router(input):
query_embedding = embeddings.embed_query(input["query"])
similarity = cosine_similarity([query_embedding], prompt_embeddings)[0]
most_similar = prompt_templates[similarity.argmax()]
print("Using MATH" if most_similar == math_template else "Using PHYSICS")
return PromptTemplate.from_template(most_similar)
chain = (
{"query": RunnablePassthrough()}
| RunnableLambda(prompt_router)
| ChatOpenAI()
| StrOutputParser()
)
message1 = chain.invoke("What is the speed of light?")
print(f"message1: {message1}")
message2 = chain.invoke("什么是微积分?")
print(f"message2: {message2}")
运行结果
➜ python3 test12.py
Using PHYSICS
message1: The speed of light is approximately 299,792,458 meters per second, which is the fastest speed at which any object can travel in the universe. It is a fundamental constant of nature and is denoted by the letter "c" in physics equations.
Using MATH
message2: 微积分是数学中的一个分支,主要涉及研究函数的变化率和积分。它可以用来解决许多实际问题,例如物理学、工程学和经济学等领域的问题。微积分可以帮助我们理解和描述物体的运动、变化和增长等现象。在微积分中,常用的概念包括导数、极限、积分和微分方程等。