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

LangChain Agent执行Prompt异常:仅返回单句结果,模型是否适配?

LangChain Agent仅返回简短结果的原因分析

问题背景

我正在学习LangChain,编写了一段Python测试代码探究Agent工作机制,代码如下:

import os                                                                        
from langchain.agents import load_tools                                          
from langchain.agents import initialize_agent                                    
from langchain.llms import OpenAI                                                
from langchain.prompts import PromptTemplate                                     

topic = input("Topic: ")                                                         
prompt = PromptTemplate(input_variables = ['topic'],                             
          template = '''                                                         
            You have been given access to a search 
            tool. Please gather information about the 
            AI algorithm topic{topic}, and write a 
            thousand word blog post on this topic.         
           '''                                                                      
        )                                                                     

os.environ['SERPAPI_API_KEY'] = "<my serpapi key>"
llm = OpenAI(model = 'text-davinci-003', temperature = 0.7, openai_api_key = "<my openAPI key> ")

tools = load_tools(['serpapi'])                                                  
agent = initialize_agent(tools, llm, agent = 'zero-shot-react-description', verbose=True)
foo = agent.run(prompt)                                                          
print (foo)                                                                     
f = open("new_post","w")                                                         
f.write(foo)                                                                    
f.close() 

运行输出

> Entering new AgentExecutor chain...

I could use a search engine to look for the answer

Action: Search

Action Input: "Tree of Thoughts"

Observation: Title:Tree of Thoughts: Deliberate 

Problem Solving with Large Language Models ... 

Abstract: Language models are increasingly being deployed for ...

Thought: This looks like it could be the answer I'm looking for

Action: Read

Action Input: Title:Tree of Thoughts: Deliberate Problem Solving with Large Language Models
Observation: Read is not a valid tool, try another one.

Thought: I should read the abstract to get an overview of what the paper is about

Action: Read

Action Input: Abstract: Language models are increasingly being deployed for ... 

Observation: Read is not a valid tool, try another one.

Thought: I should look for other sources of information about this topic

Action: Search

Action Input: "Tree of Thoughts" + review

Observation: Percival Everett's new novel The Trees hits just the right mark. It's a racial allegory grounded in history, shrouded in mystery, and dripping ...

Thought: This looks like a review of a novel, not what I'm looking for

Action: Search

Action Input: "Tree of Thoughts" + research

Observation: To surmount these challenges, we introduce a new framework for language model inference, Tree of Thoughts (ToT), which generalizes over the ...

Thought: This looks like it could be the answer I'm looking for

Final Answer: Tree of Thoughts (ToT) is a new framework for language model inference that generalizes over the existing methods and enables more efficient problem solving.

> Finished chain.

疑问

为何仅返回这样一句话的结果?是否是模型选择不合适?

原因分析及解决方案

  • 模型能力限制:text-davinci-003属于GPT-3系列,上下文窗口较小,复杂任务处理能力弱于GPT-4或GPT-3.5-turbo。这类模型在需要多轮信息收集并生成长文本(千字博客)的任务中,容易提前终止,无法完成完整内容生成。建议更换为GPT-3.5-turbo或GPT-4,注意需改用ChatOpenAI类替代原OpenAI类。

  • Prompt引导不足:当前Prompt仅要求写千字博客,但未明确引导Agent拆解任务——比如先收集ToT的定义、核心原理、应用场景、对比优势等关键信息,再组织成完整博客结构。Zero-shot Agent本身没有任务拆解的固有逻辑,需在Prompt中明确步骤,例如:"先通过搜索收集Tree of Thoughts的核心定义、提出背景、核心机制、实际应用案例等信息,再将这些内容整理成一篇千字博客,包含引言、核心内容、总结三个部分"。

  • 工具覆盖不全:仅加载serpapi工具无法满足内容需求——Agent尝试使用"Read"工具失败,而Serpapi仅能返回搜索摘要,不足以支撑千字博客的细节。可添加arxiv工具直接读取论文全文,或配合网页读取工具获取科普类文章的完整内容,同时优化搜索关键词,避免出现无关结果。

  • Agent终止逻辑触发过早:Zero-shot-react-description Agent会在判断"已获取足够信息"时终止任务,当前Observation仅提供ToT的基础定义,Agent就认为满足需求,这是因为Prompt未明确要求收集足够多的细节信息。需在Prompt中强化"收集全面信息后再生成完整博客"的指令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 20:52:56