首页 > 其他分享 >MultiPromptChain--精简版

MultiPromptChain--精简版

时间:2024-05-14 14:42:47浏览次数:12  
标签:MultiPromptChain prompt 精简版 chain -- template router input llm

from langchain_community.llms import Ollama
from langchain.chains.router import MultiPromptChain
from langchain.chains import ConversationChain
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate

# 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:
# {input}"""
#
# 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:
# {input}"""

# prompt_infos = [
# {
# "name": "physics",
# "description": "Good for answering questions about physics",
# "prompt_template": physics_template,
# },
# {
# "name": "math",
# "description": "Good for answering math questions",
# "prompt_template": math_template,
# },
# ]



# 以下是定义的提示模板
cigerate_template = """
如果问题是关于香烟的,请用英文回答问题
下面是需要你来回答的问题:
{input}
"""

conversion_template = """
你是一位聊天大师,擅长解答日常生活中的问题,把答复翻译成语句通顺的中文。
下面是需要你来回答的问题:
{input}
"""


# 构建提示信息
prompt_infos = [
{
"name": "cig",
"description": "适合回答所有香烟的问题",
"prompt_template": cigerate_template
},
{
"name": "talk",
"description": "适合回答日常问题",
"prompt_template": conversion_template
},
# 历史模板被注释掉了,所以这里不包括它
# {
# "name": "History",
# "description": "适合回答历史的问题",
# "prompt_template": history_template
# }
]


MULTI_PROMPT_ROUTER_TEMPLATE = """
将原始文本输入到语言模型中,选择最合适输入的提示词。
你将获得最适合的提示词名称以及相应的描述。
<< FORMATTING >>
返回一个Markdown代码片段,其中包含格式化为JSON对象的内容:
```json
{{{{
"destination": string \\ 要使用的prompt的name,或者"DEFAULT"
"next_inputs": string \\ 原始的input,或者可能的原始输入的修改版本
}}}}
```
记住:"destination"必须是下面指定的候选提示名之一,如果包含香烟二字"destination"是"cig",或者如果输入不适合任何候选提示,则可以是"DEFAULT"
记住:"next_inputs"如果你认为不需要进行任何修改,可以直接使用原始输入。

<< CANDIDATE PROMPTS >>
{destinations}

<<INPUT>>
{{input}}

<<OUTPUT(必须以```json开头作为回复)>>
<< OUTPUT (must end with ```) >>
"""

# 初始化语言模型
llm = Ollama(base_url='http://127.0.0.1:11434', model='phi3:3.8b')

# destination_chains = {}
# for p_info in prompt_infos:
# name = p_info["name"]
# prompt_template = p_info["prompt_template"]
# prompt = PromptTemplate(template=prompt_template, input_variables=["input"])
# chain = LLMChain(llm=llm, prompt=prompt)
# # chain = llm | prompt
# destination_chains[name] = chain

# default_chain = ConversationChain(llm=llm, output_key="text")

# from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser

# from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE

# destinations = [f"{p['name']}: {p['description']}" for p in prompt_infos]

# destinations_str = "\n".join(destinations)

test_chain = MultiPromptChain.from_prompts(llm,prompt_infos=prompt_infos,verbose=True)


# router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=test_chain.destinations_str)

# router_prompt = PromptTemplate(
# template=router_template,
# input_variables=["input"],
# output_parser=RouterOutputParser(),
# )

# router_chain = LLMRouterChain.from_llm(llm, router_prompt)

# chain = MultiPromptChain(
# router_chain=router_chain,
# destination_chains=destination_chains,
# default_chain=default_chain,
# verbose=True,
# )

res=test_chain.invoke({'input': "中国美女最多的省份是哪个省?"})

print(res.get('text'))
 

标签:MultiPromptChain,prompt,精简版,chain,--,template,router,input,llm
From: https://www.cnblogs.com/Zhouzg-2018/p/18191242

相关文章

  • vue+element-ui在表格中计算两数差值并让差值>20的字体显示为红色
    <el-table-columnprop="differential"label="差量"class-name="small-paddingfixed-width"><templateslot-scope="scope"><span:c......
  • easyUI datagrid 数据不显示,tablebody 高度为0 的问题
    如下图,接口请求回来数据,表头也显示出来了。打开元素,可以看到table的bbody里面是有数据的,数据也渲染到了页面表格中,但是table的height为0。 排查以下问题:检查数据源格式是否正确,如是JSON数组还是符合easyui要求的对象。确保在datagrid初始化后正确触发数据加载......
  • 系统安全性设计报告
    1.系统密码功能要求安全性要求是无纸化电子公文传输系统首先要满足的要求。由于网络环境的广泛性和复杂性等特点,普通电子文件很容易在网络传输过程中被截取或篡改。而电子公文文件必须具有保密性、严肃性和不可抵赖性的特性,绝对不允许出现此类安全漏洞。为此我们采用了RSA不对称......
  • 加固计划书
    加固计划书SDL介绍安全开发生命周期(SDL)是一种软件开发方法论,旨在通过将安全性纳入软件开发的各个阶段来创建更安全、更健壮的软件。SDL由一系列阶段和活动组成,涵盖了从需求分析到维护的整个软件开发生命周期。以下是SDL的主要阶段和相关活动:需求分析阶段:安全需求收集:确定......
  • Jenkins - [02] 安装部署
    题记部分 一、Jenkins是什么  Jenkins,原名Hudson,2011年改为现在的名字,它是一个开源的实现持续集成的软件工具。官网:https://www.jenkins.io/官网:https://www.jenkins.io/zh/(中文)  Jenkins能实时监控集成中存在的错误,提供详细的日志文件和提醒功能,还能用图表的形式......
  • OpenTelemetry agent 对 Spring Boot 应用的影响:一次 SPI 失效的
    背景前段时间公司领导让我排查一个关于在JDK21环境中使用SpringBoot配合一个JDK18新增的一个SPI(java.net.spi.InetAddressResolverProvider)不生效的问题。但这个不生效的前置条件有点多:JDK的版本得在18+SpringBoot3.x还在额外再配合使用-javaagent:openteleme......
  • 密码方案的应用
    密码方案可以应用于多种场景,包括但不限于以下几个方面:1.个人账户保护:密码方案可以用于保护个人账户的安全,例如电子邮箱、社交媒体账户、在线银行账户等。用户可以设置强密码,并定期更改密码,以防止黑客入侵。2.企业网络安全:密码方案可以用于保护企业内部网络的安全,确保只有授权......
  • Windows系统C盘瘦身
    1.windows系统休眠关闭C:\Hiberfil.sys#关闭系统休眠(可以节省6G空间)powercfg-hoff 2.将windows虚拟内存交换文件设置到D盘C:\Pagefile.sys->D:\ 2.1打开如下开关:  查看C盘根目录下的:C:\Pagefile.sys文件大小,比如12G2.2我的电脑,空白处右键-》属性 -》高级......
  • 阿里云Windows server 2016服务器Antimalware Service Executable进程占比高,cpu接近10
    问题描述:阿里云Windowsserver2016服务器AntimalwareServiceExecutable进程占比高,cpu接近100%,需要强制关闭该进程,排查问题,进入系统服务关闭,提示拒绝访问(该服务关闭后有风险,需评估后操作)  操作步骤:1.进入服务器,进入Windowspowershell界面; 2. 输入 regadd"HKEY_L......
  • 使用Redis执行多个命令
    单个命令执行packagemainimport("context""fmt""github.com/go-redis/redis/v8""time")funcmain(){//创建Redis客户端rdb:=redis.NewClient(&redis.Options{Addr:"localhos......