You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Langchain技术问题:如何合并多个Prompt的输出结果?

解决SequentialChain仅返回单一输出的问题

当前代码存在两个核心问题:

  • SequentialChain的output_variables仅指定了["dob"],因此仅返回出生日期结果,未包含全名和国籍的输出
  • 输入判断条件if input:有误,应使用if input_text:来判断用户是否输入内容

以下是两种可行的解决方案:

方案一:手动拼接结果(高效固定格式)

修改SequentialChain的输出变量,获取所有字段后手动拼接成预期格式:

import os
from constent import openai_key

from langchain_community.llms import OpenAI
import streamlit as st
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain

os.environ["OPENAI_API_KEY"] = openai_key

st.title('Search about person')

input_text = st.text_input("Search about the person")

llm = OpenAI(temperature = 1.0)

# Prompt Engineering
prompt1 = PromptTemplate(
  input_variables = ["name"],
  template = "Give the full name of {name}"
)
chain1 = LLMChain(llm = llm, prompt = prompt1, verbose = True, output_key = 'person')

prompt2 = PromptTemplate(
  input_variables = ["person"],
  template = "Give the name of the country that {person} belongs to?"
)
chain2 = LLMChain(llm = llm, prompt = prompt2, verbose = True, output_key = 'country')

prompt3 = PromptTemplate(
  input_variables = ["person"],
  template = "What is the date of birth of {person}?"
)
chain3 = LLMChain(llm = llm, prompt = prompt3, verbose = True, output_key = 'dob')

# 修改output_variables,包含所有需要的输出字段
combined_chain = SequentialChain(
    chains=[chain1, chain2, chain3],
    input_variables = ["name"],
    output_variables=["person", "country", "dob"],
    verbose = True
)

# 修正输入判断条件
if input_text:
    # 获取所有结果
    results = combined_chain.run({"name": input_text})
    # 手动拼接成预期格式
    final_output = f"{results['person']},出生日期为{results['dob']},他来自{results['country']}"
    st.write(final_output)

方案二:新增格式化链(灵活适配格式)

新增一个PromptTemplate,让LLM自动将所有结果整理成自然语句,适合需要动态调整输出格式的场景:

import os
from constent import openai_key

from langchain_community.llms import OpenAI
import streamlit as st
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain

os.environ["OPENAI_API_KEY"] = openai_key

st.title('Search about person')

input_text = st.text_input("Search about the person")

llm = OpenAI(temperature = 1.0)

# Prompt Engineering
prompt1 = PromptTemplate(
  input_variables = ["name"],
  template = "Give the full name of {name}"
)
chain1 = LLMChain(llm = llm, prompt = prompt1, verbose = True, output_key = 'person')

prompt2 = PromptTemplate(
  input_variables = ["person"],
  template = "Give the name of the country that {person} belongs to?"
)
chain2 = LLMChain(llm = llm, prompt = prompt2, verbose = True, output_key = 'country')

prompt3 = PromptTemplate(
  input_variables = ["person"],
  template = "What is the date of birth of {person}?"
)
chain3 = LLMChain(llm = llm, prompt = prompt3, verbose = True, output_key = 'dob')

# 新增格式化用的Prompt和Chain
prompt4 = PromptTemplate(
    input_variables = ["person", "country", "dob"],
    template = "将以下信息整理成自然的中文句子:全名{person},国籍{country},出生日期{dob}。格式要求:{person},出生日期为{dob},他来自{country}"
)
chain4 = LLMChain(llm = llm, prompt = prompt4, verbose = True, output_key = 'final_output')

# 修改SequentialChain,包含格式化链
combined_chain = SequentialChain(
    chains=[chain1, chain2, chain3, chain4],
    input_variables = ["name"],
    output_variables=["final_output"],
    verbose = True
)

# 修正输入判断条件
if input_text:
    st.write(combined_chain.run({"name": input_text}))

内容的提问来源于stack exchange,提问作者Hari Adhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:07:15