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

red*_*123 6 python langchain

我正在学习如何使用 langchain,并且我编写了一个小练习来尝试弄清楚代理是如何工作的。

我有一个小的 Python 程序,如下所示:

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() 
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到以下输出:

> 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.
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?为什么我只得到这一句话作为输出?我使用了错误的型号吗?谢谢

Mee*_*iya 8

以下是您可以如何做到这一点。多次绑定测试后,根据需要更改PREFIX、 、SUFFIX中的内容。FORMAT_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 = "<my openAPI key> ")
tools = load_tools(['serpapi'], llm=llm)

PREFIX = '''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'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.
'''

FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:
'''
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
'''

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

'''
Thought: Do I need to use a tool? No
AI: [write a blog post]
'''
"""

SUFFIX = '''

Begin!

Previous conversation history:
{chat_history}

Instructions: {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'])
Run Code Online (Sandbox Code Playgroud)


Nat*_*ath 0

您已关闭但并未实际填写提示模板

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

那应该让你去参加比赛。就其价值而言,我认为这应该是一个错误,而不是 PromptTemplate 静静地呈现为字符串