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

2

7

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

Primitivism answered 25/5, 2023 at 7:42 Comment(0)
H
10

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='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'])
Heydon answered 2/6, 2023 at 5:55 Comment(2)
Is this a ReAct prompting?Amur
Yes, it is react.Heydon
S
0

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

Swanson answered 25/5, 2023 at 7:55 Comment(1)
It still doesn't work properly. Changing the code as suggested, I get the following output when I enter the topic 'Tree of Thoughts' "A blog post summarizing the Tree Of Thoughts AI algorithm should 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." In other words, it's telling me what I should do, rather than actually doing it.Primitivism

© 2022 - 2024 — McMap. All rights reserved.