LangChain-12 Routing By Semantic Similarity 让GPT根据不明确的问题 自动选择工具集 根据语义自动路由 原创

请添加图片描述

问题背景

平常我们设计程序的时候,会这么写:

// 随便举例
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: 微积分是数学中的一个分支,主要涉及研究函数的变化率和积分。它可以用来解决许多实际问题,例如物理学、工程学和经济学等领域的问题。微积分可以帮助我们理解和描述物体的运动、变化和增长等现象。在微积分中,常用的概念包括导数、极限、积分和微分方程等。

在这里插入图片描述

posted @   武子康  阅读(0)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示