NeMo-Guardrails icon indicating copy to clipboard operation
NeMo-Guardrails copied to clipboard

Support for Agents as Actions?

Open vintrocode opened this issue 2 years ago • 0 comments

The examples show a q/a chain that finds answers over docs. I want to use an Agent Chain as described here to run google searches given a user query. However, the bot is generating "internal error" messages and I'm not sure how to get more transparency under the hood. Here's a demo script:

import os

from nemoguardrails import LLMRails, RailsConfig
from langchain.agents import Tool
from langchain.memory import ConversationBufferMemory
from langchain.utilities import SerpAPIWrapper
from langchain.agents import initialize_agent, AgentType
from dotenv import load_dotenv


load_dotenv()

YAML_CONFIG = """
models:
  - type: main
    engine: openai
    model: text-davinci-003
"""
COLANG_CONFIG = """
define user express greeting
  "hey"
  "yo"
  "hi"
  "hello"
  "what's up"

define bot express greeting
  "Hello there!"

define bot offer to help
  "How can I help you today?"

define flow
  user express greeting
  bot express greeting
  bot offer to help

define flow
  user ...
  $answer = execute qa_chain(input=$last_user_message)
  bot $answer
"""

def main():

    search = SerpAPIWrapper()
    tools = [
        Tool(
            name = "Current Search",
            func=search.run,
            description="useful for when you need to answer questions about current events or the current state of the world. the input to this should be a single search term."
        ),
    ]

    memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

    config = RailsConfig.from_content(COLANG_CONFIG, YAML_CONFIG)
    APP = LLMRails(config, verbose=True)

    agent_chain = initialize_agent(tools, APP.llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)
    
    APP.register_action(agent_chain, name="qa_chain")

    history = [{"role": "user", "content": "How old was Obama when the Capitals won the Stanley Cup?"}]
    result = APP.generate(messages=history)
    print(result)

if __name__ == "__main__":
    main()

it seems that the colang logic correctly identifies that the qa_chain needs to be called, but after two turns it generates an "internal error"...

> Finished chain.
  ask general question
bot response for general question
  "I'm sorry, I don't know the answer to that question. However, I can help you find the answer if you'd like."


> Entering new AgentExecutor chain...


RESPONSE
--------------------
\```json
{
    "action": "Current Search",
    "action_input": "Age of Barack Obama when Capitals won Stanley Cup"
}
\```

> Entering new AgentExecutor chain...


RESPONSE
--------------------
\```json
{
    "action": "Current Search",
    "action_input": "Age of Barack Obama when Capitals won Stanley Cup"
}
\```{'role': 'assistant', 'content': "I'm sorry, an internal error has occurred."}

Are agents just not supported yet or is there something I can do to improve my outcomes here? Thanks

vintrocode avatar May 09 '23 03:05 vintrocode