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

如何在LangChain中将自定义数据集整合到多提示模板中?

解决方案

针对你在LangChain中使用自定义提示模板同时整合pandas数据集的问题,以下两种方案适配你当前的版本(langchain0.0.336、langchain-experimental0.0.42):

方案一:修改现有Pandas DataFrame Agent的提示模板

create_pandas_dataframe_agent生成的OPENAI_FUNCTIONS类型agent,可通过修改内部LLM链的prompt来整合自定义内容,需保留让模型调用工具处理数据集的核心指令:

from langchain.agents import AgentType
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from langchain.chat_models import AzureChatOpenAI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder

# 初始化LLM(保留原有代码)
llm = AzureChatOpenAI(
    openai_api_base=OPENAI_API_BASE, 
    openai_api_key=OPENAI_API_KEY, 
    deployment_name=deployment_name, 
    model_name=deployment_name, 
    openai_api_version=OPENAI_DEPLOYMENT_VERSION
)

# 创建初始agent
agent = create_pandas_dataframe_agent(
    llm,
    [dataset1, dataset2],
    verbose=False,
    agent_type=AgentType.OPENAI_FUNCTIONS,
)

# 自定义提示模板,需包含调用工具的核心逻辑
custom_template = """
You are an expert in {data}.
You have access to tools that can query the provided pandas dataframes to answer questions.
Use the tools as needed to get necessary data before formulating a final answer.
"""

# 构建新的ChatPromptTemplate,保留agent必需的占位符
prompt = ChatPromptTemplate.from_messages([
    ("system", custom_template),
    MessagesPlaceholder(variable_name="chat_history", optional=True),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad"),
])

# 更新agent的prompt
agent.agent.llm_chain.prompt = prompt

# 调用agent并传入自定义参数
response = agent.run({
    "input": "What diagnosis are suitable if presenting with cancer?",
    "data": "AIDS"
})

方案二:手动构建Agent(灵活可控)

手动将数据集转换为可访问的工具,结合自定义提示模板构建AgentExecutor,完全掌控提示逻辑:

from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_experimental.tools.python.tool import PythonAstREPLTool
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import AzureChatOpenAI

# 初始化LLM(保留原有代码)
llm = AzureChatOpenAI(
    openai_api_base=OPENAI_API_BASE, 
    openai_api_key=OPENAI_API_KEY, 
    deployment_name=deployment_name, 
    model_name=deployment_name, 
    openai_api_version=OPENAI_DEPLOYMENT_VERSION
)

# 将数据集注册到Python工具中,让Agent可访问
repl_tool = PythonAstREPLTool(locals={"df1": dataset1, "df2": dataset2})
tools = [repl_tool]

# 自定义提示模板,明确告知Agent可使用工具查询数据集
custom_prompt = ChatPromptTemplate.from_messages([
    ("system", """
You are an expert in {data}.
You have access to a Python REPL tool that can query dataframes df1 and df2.
Use the tool to retrieve relevant data before answering the user's question.
"""),
    ("human", "{query}"),
    ("placeholder", "{agent_scratchpad}")
])

# 创建OPENAI_FUNCTIONS类型agent并封装为执行器
agent = create_openai_functions_agent(llm, tools, custom_prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)

# 调用执行器
response = agent_executor.invoke({
    "data": "AIDS",
    "query": "What diagnosis are suitable if presenting with cancer?"
})

print(response["output"])

关键说明

  • 方案一适合快速修改现有agent,无需重构代码;方案二更适合需要高度自定义提示和工具逻辑的场景。
  • 无论哪种方案,必须在提示中明确告知模型可使用工具查询数据集,否则模型会忽略数据集直接生成回答,导致无法整合数据。

内容的提问来源于stack exchange,提问作者wierdly-looking

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 07:47:09