Giter Club home page Giter Club logo

Comments (7)

zsimjee avatar zsimjee commented on June 3, 2024 1

When I chain the guard after the OpenAIToolsAgentOutputParser(), a value is passed to the guard finally, however, the value includes the function calls. The problem here is that the agent is not executed by the time the results are passed to teh guard. Figuring out if there's a way to fix that

[OpenAIToolAgentAction(tool='get_retriever_docs', tool_input={'query': 'secret'}, log="\nInvoking: get_retriever_docs with {'query': 'secret'}\n\n\n", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_3WbE44YBc9F7snOpUwLjMhgK', 'function': {'arguments': '{"query":"secret"}', 'name': 'get_retriever_docs'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'})], tool_call_id='call_3WbE44YBc9F7snOpUwLjMhgK')]

from guardrails.

zsimjee avatar zsimjee commented on June 3, 2024

Hi, I'm looking into this one now.

from guardrails.

zsimjee avatar zsimjee commented on June 3, 2024

I've got reason to believe this is actually not a Guardrails specific problem but rather a chaining one in general - when I swap out the | guard line for this

topic = "*apricot*"
guard = Guard().use(RegexMatch(topic, match_type="search", on_fail="filter"))
model = ChatOpenAI(temperature=0, streaming=False)
llm = model | StrOutputParser()

I get the same RunnableSequence error.

I'm diving deeper to see what the actual issue is.

from guardrails.

zsimjee avatar zsimjee commented on June 3, 2024

ok the second use seems to be the right place to chain in (where we got the 'API must be provided' error). I attached a debugger, and found that when the guard invoke function is called, it's passed an empty input. I think something else is going on here, where LCEL is executing guard validation before it has results ready.

from guardrails.

zsimjee avatar zsimjee commented on June 3, 2024

Where we chain is very important - the place to chain the guard with output validation will be after the agent executes. See the code sample below

from langchain import hub
from langchain.agents import AgentExecutor
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain.tools import tool
from langchain_openai import ChatOpenAI
from langchain_core.documents.base import Document
from guardrails.hub import RegexMatch
from guardrails import Guard

from langchain_core.runnables import RunnablePassthrough
from langchain.agents.format_scratchpad.openai_tools import (
    format_to_openai_tool_messages,
)
from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser

prompt = hub.pull("hwchase17/openai-tools-agent")


@tool
def get_retriever_docs(query: str) -> list[Document]:
    """Returns a list of documents from the retriever."""
    return [
        Document(
            page_content="# test file\n\nThis is a test file with a secret code of 'blue-green-apricot-brownie-cake-mousepad'.",
            metadata={"source": "./test.md"},
        )
    ]


# Set up a Guard
topic = "apricot"
guard = Guard().use(RegexMatch(topic, match_type="search", on_fail="filter"))
model = ChatOpenAI(temperature=0, streaming=False)
llm = model
tools = [get_retriever_docs]


############################################ this is a copy-paste from langchain.agents.create_openai_tools_agent
llm_with_tools = llm.bind(tools=[convert_to_openai_tool(tool) for tool in tools])



agent = (
    RunnablePassthrough.assign(
        agent_scratchpad=lambda x: format_to_openai_tool_messages(
            x["intermediate_steps"]
        )
    )
    | prompt
    | llm_with_tools
    | OpenAIToolsAgentOutputParser()
)
############################################

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True).with_config(
    {"run_name": "Agent"}
)

chain = agent_executor | guard

query = "call get_retriever_docs and tell me a secret from the docs"
print(chain.invoke({"input": query}))

from guardrails.

alex-dreo-persefoni avatar alex-dreo-persefoni commented on June 3, 2024

@zsimjee Confirmed that chaining the guard after the agent_executor works in your minimal example. Intuitively, this make a lot of sense: we want the agent's output, to go through guardrails. For this reason alone, it makes sense to me to chain guard all the way at the end rather than in an intermediate step as I had tried to do in my code above.

from guardrails.

zsimjee avatar zsimjee commented on June 3, 2024

Awesome! I wish there was a way to chain the actual agent and [type]AgentOutputParser() directly into the AgentExecutor, but I couldn't get that to work. IF it does work, it would be cool to have a chain like

  RunnablePassthrough.assign(
      agent_scratchpad=lambda x: format_to_openai_tool_messages(
          x["intermediate_steps"]
      )
  )
  | prompt
  | llm_with_tools
  | OpenAIToolsAgentOutputParser()
  | AgentExecutor().with_config("run_name": "Agent")
  | guard
  | [other output parsers]

Closing for now

from guardrails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.