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

Custom action registration fails with error "AttributeError: 'function return value' object has no attribute 'run''

Open analyticalpicasso opened this issue 1 year ago • 3 comments

hi,

I am trying to register custom action in rails but it always fails. Unable to understand the error.

Even if the function is as simple as returning a simple string like below:

def _get_name():
    return "admin"

 get_name = _get_name()
app.register_action(action=get_name, name="get_name")

general.co

define flow
  user ...
  $auth1 = execute get_name()
Error:
AttributeError: 'str' object has no attribute 'run'

I am getting similar error If i am returning bool value like True or False

Error:
AttributeError: 'Bool' object has no attribute 'run'

Since when I am using some register action from example like below then its working:

Function:

def _get_qa_chain(llm):
    """Initializes a QA chain using the jobs report.

    It uses OpenAI embeddings.
    """
    qa_chain = RetrievalQA.from_chain_type(
        llm=llm, chain_type="stuff", retriever=vector_index.as_retriever()
    )

    return qa_chain

qa_chain = _get_qa_chain(llm=llm)
app.register_action(qa_chain, name="qa_chain")

general.co

    $answer = execute qa_chain(query=$last_user_message)

Please let me know where I am going wrong with this,

analyticalpicasso avatar Feb 29 '24 17:02 analyticalpicasso

The issue is here:

def _get_name():
    return "admin"

- get_name = _get_name()
app.register_action(action=get_name, name="get_name")

Because you're calling the _get_name function, you're actually registering a string (the returned value) as an action. In the example that works, the returned value is a chain, which can be registered as an action. You should just register directly the function:

def _get_name():
    return "admin"

+ app.register_action(action=_get_name, name="get_name")

Let me know if this works.

drazvan avatar Feb 29 '24 19:02 drazvan

@drazvan Thanks for quick response.

The above approach worked.

But how will I pass parameter to the function e.g.

def validate_user_name(request: gr.Request):
    if request.username == "admin":
        return True
    else:
        return False

app.register_action(validate_user_name(request), name="user_auth") There in above function I need to pass session variable where username is available and check for user authentication.

How will the functions work if I need to pass values from application end.

analyticalpicasso avatar Mar 01 '24 11:03 analyticalpicasso

@drazvan Is there any way that I cam pass my function parameters to the rails file?

analyticalpicasso avatar Mar 06 '24 12:03 analyticalpicasso