NVIDIA / NeMo-Guardrails

NeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.
Other
4.18k stars 396 forks source link

Support for Agents as Actions? #19

Closed vintrocode closed 2 weeks ago

vintrocode commented 1 year ago

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

drazvan commented 1 year ago

Hi @vintrocode!

To get a more detailed log of what's happening under the hood, you can set the logging level to INFO at the beginning of the script:

import logging
logging.basicConfig(level=logging.INFO)

I've just tested your code. I was getting an internal error when the SERP_API_KEY was not set. After I've set it, I was able to get the chain working:

The last lines from the output:

...

AI:

RESPONSE
--------------------
\```json
{
    "action": "Final Answer",
    "action_input": "President Barack Obama welcomed the five-time Stanley Cup Champion Chicago Blackhawks to the White House to honor the team and their 2013 Stanley Cup victory."
}
\```

> Finished chain.
{'role': 'assistant', 'content': 'President Barack Obama welcomed the five-time Stanley Cup Champion Chicago Blackhawks to the White House to honor the team and their 2013 Stanley Cup victory.'}

If you can't get it to work using the detailed log, do paste it here so we can have a look at it.

Nabil-iium commented 1 year ago

@drazvan I tried something like this code, but I have used ChromaDB RetrieverQA in tools. I am getting this error. (Basically I want to use it with conv chat memory.)

ERROR:nemoguardrails.actions.action_dispatcher:Error Missing some input keys: {'input'} while execution qa_chain Traceback (most recent call last): File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\nemoguardrails\actions\action_dispatcher.py", line 133, in execute_action result = await chain.arun( File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 532, in arun await self.acall( File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 302, in acall inputs = self.prep_inputs(inputs) File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 389, in prep_inputs self._validate_inputs(inputs) File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 147, in _validate_inputs raise ValueError(f"Missing some input keys: {missingkeys}") ValueError: Missing some input keys: {'input'}

Siddhijain16 commented 1 year ago

@drazvan I tried something like this code, but I have used ChromaDB RetrieverQA in tools. I am getting this error. (Basically I want to use it with conv chat memory.)

ERROR:nemoguardrails.actions.action_dispatcher:Error Missing some input keys: {'input'} while execution qa_chain Traceback (most recent call last): File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\nemoguardrails\actions\action_dispatcher.py", line 133, in execute_action result = await chain.arun( File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 532, in arun await self.acall( File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 302, in acall inputs = self.prep_inputs(inputs) File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 389, in prep_inputs self._validate_inputs(inputs) File "c:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\langchain\chains\base.py", line 147, in _validate_inputs raise ValueError(f"Missing some input keys: {missingkeys}") ValueError: Missing some input keys: {'input'}

Hi @Nabil-iium, You have to replace query with input while calling in a colang flow block like this - $answer = execute qa_chain(input=$last_user_message)