我无法使langchain代理模块实际执行我的提示。

huangapple go评论65阅读模式
英文:

I can't get the langchain agent module to actually execute my prompt

问题

这里发生了什么?为什么我只得到了这一个句子作为输出?我是不是在使用错误的模型?
感谢。

英文:

I am learning how to use langchain and I have written
a small exercise to try and figure out how agents work.

I have a small Python program that looks like this:

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() 

When I run this, I get the following output:

> 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.

What is going on here? Why am I only getting this one sentence as output? Am I using the wrong model?
Thanks

答案1

得分: 7

以下是您可以执行的方式。根据您的需要,更改PREFIXSUFFIXFORMAT_INSTRUCTION中的内容,经过几次尝试和测试后进行更改。

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

llm = OpenAI(model_name='text-davinci-003', temperature=0.7, openai_api_key='<我的 OpenAPI 密钥>')
tools = load_tools(['serpapi'], llm=llm)

PREFIX = '''您是一位人工智能数据科学家。您已经进行了多年的研究,研究了所有人工智能算法。您还喜欢写作。在空闲时间里,您为阐明您对不同人工智能算法的学习所得撰写博客文章。不要忘记包括有关该算法的优点、缺点和应用的信息。此外,博客文章应解释该算法如何将模型推理提高了惊人的 70%,以及它是一个即插即用版本,可以与其他组件无缝连接。'''
FORMAT_INSTRUCTIONS = '''要使用工具,请使用以下格式:
'''
当您收集了有关人工智能算法的所有信息后只需以博客文章的形式向用户编写
'''
SUFFIX = '''开始

先前的对话历史记录
{chat_history}

说明{input}
{agent_scratchpad}
'''

agent = initialize_agent(
    tools=tools,
    llm=llm,
    agent="zero-shot-react-description",
    verbose=True,
    return_intermediate_steps=True,
    agent_kwargs={
        'prefix': PREFIX,
        'format_instructions': FORMAT_INSTRUCTIONS,
        'suffix': SUFFIX
    }
)

res = agent({"input": query.strip()})
print(res['output'])
英文:

Here is how you can do it. Change the content in PREFIX, SUFFIX, and FORMAT_INSTRUCTION according to your need after tying and testing few times.

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

llm = OpenAI(model_name=&#39;text-davinci-003&#39;, temperature = 0.7, openai_api_key = &quot;&lt;my openAPI key&gt; &quot;)
tools = load_tools([&#39;serpapi&#39;], llm=llm)

PREFIX = &#39;&#39;&#39;You are an AI data scientist. You have done years of research in studying all the AI algorthims. You also love to write. On free time you write blog post for articulating what you have learned about different AI algorithms. Do not forget to include information on the algorithm&#39;s benefits, disadvantages, and applications. Additionally, the blog post should explain how the algorithm advances model reasoning by a whopping 70% and how it is a plug in and play version, connecting seamlessly to other components.
&#39;&#39;&#39;

FORMAT_INSTRUCTIONS = &quot;&quot;&quot;To use a tool, please use the following format:
&#39;&#39;&#39;
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
&#39;&#39;&#39;

When you have gathered all the information regarding AI algorithm, just write it to the user in the form of a blog post.

&#39;&#39;&#39;
Thought: Do I need to use a tool? No
AI: [write a blog post]
&#39;&#39;&#39;
&quot;&quot;&quot;

SUFFIX = &#39;&#39;&#39;

Begin!

Previous conversation history:
{chat_history}

Instructions: {input}
{agent_scratchpad}
&#39;&#39;&#39;

agent = initialize_agent(
    tools=tools,
    llm=llm,
    agent=&quot;zero-shot-react-description&quot;,
    verbose=True,
    return_intermediate_steps=True,
    agent_kwargs={
        &#39;prefix&#39;: PREFIX, 
        &#39;format_instructions&#39;: FORMAT_INSTRUCTIONS,
        &#39;suffix&#39;: SUFFIX
    }
)

res = agent({&quot;input&quot;: query.strip()})
print(res[&#39;output&#39;])

</details>



# 答案2
**得分**: 0

你接近了,但实际上你没有填写好提示模板。

`foo = agent.run(prompt.format_prompt(topic=topic))`。

这应该让你能够开始使用了。就我所知,我认为这应该是一个错误,而不应该让提示模板静默地渲染为字符串。

<details>
<summary>英文:</summary>

your close but your not actually filling in the prompt template

`foo = agent.run(prompt.format_prompt(topic=topic))`. 

that should have you away to the races.  for what its worth I think this should be an error rather than the PromptTemplate quietly rendering as a string 



</details>



huangapple
  • 本文由 发表于 2023年5月25日 15:42:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定