figoyouwei / taipy_success

This is a sample code that tests the deployment on heroku
2 stars 2 forks source link

Deeper understand the tgb.chat() #23

Closed figoyouwei closed 2 weeks ago

figoyouwei commented 1 month ago

Hey,

This code works fine.

with tgb.Page() as page_chat:
    tgb.chat(
        messages="{messages}",
        users=users, 
        on_action=evaluate, 
        sender_id="Human"
    )

First question: def evaluate(state, var_name: str, payload: dict):

In this evaluate function, what does the argument var_name do? If I remove it, it returns error. So what does it do here?

AlexandreSajus commented 1 month ago

var_name is not very important in the chat context. We just wanted to maintain consistency with other on_action callbacks in Taipy. Here, var_name will just be the name of the variable you assigned to the messages property. If you create tgb.chat(messages="{A}"), var_name should be "A"

figoyouwei commented 1 month ago

ok, I see. What I actually like to achieve is that I want to pass the whatever chat_function into the evaluate function.

def evaluate(state, var_name: str, payload: dict):
    notify(state, "I", f"We are preparing your answer...")

    # Retrieve the callback parameters
    (_, _, message_hm, sender_id) = payload.get("args", [])

    # Default message used if evaluation fails
    result = "Invalid expression"
    try:
        # Evaluate the expression and store the result
        result = chat_function(message_hm)
    except Exception:
        pass

so that every time I change the chat_function, I don't touch the evaluate function but just make a change in the tgb.chat()

For example, something like that? I change the some_chat_function

tgb.chat(
        # Note: messages is actually the "var_name" in the evaluate function
        messages="{messages}", 
        users=users, 
        on_action=evaluate(chat_function=some_chat_function), 
        sender_id="Human"
    )
AlexandreSajus commented 1 month ago

I'm sorry, I don't understand what you are trying to do here. What is the expected behavior of the application in relation to what you are trying to do? Maybe I can find a more straightforward way to fit your use case.

figoyouwei commented 1 month ago

ok, let me explain in more real code:)

I mean I tried to define a callable as argument in evaluate declaration like below, but it didn't work. I somehow want something like that to work so that I can keep evaluate function intact just change the tgb.chat code which is just another page variable.

def chatllmOne()
    # code

def chatllmTwo()
    # code

def evaluate(state, chatllm: callable, var_name: str, payload: dict):
    notify(state, "I", f"We are preparing your answer...")

    # Retrieve the callback parameters
    (_, _, message_hm, sender_id) = payload.get("args", [])

    # Default message used if evaluation fails
    result = "Invalid expression"
    try:
        # Evaluate the expression and store the result
        result = chatllm(message_hm)
    except Exception:
        pass

tgb.chat(
        # Note: messages is actually the "var_name" in the evaluate function
        messages="{messages}", 
        users=users, 
        on_action=evaluate(chatllm=chatllmOne), 
        sender_id="Human"
    )
AlexandreSajus commented 1 month ago

Yes, your approach won't work. on_action expects a function with a pre-defined signature (state, var_name, payload). You can't put a function with a different signature in on_action

What you can do is create a variable to store chatllm like so:

def chatllmOne()
    # code

def chatllmTwo()
    # code

chatllm = chatllmOne()

def evaluate(state, var_name: str, payload: dict):
    chatllm = state.chatllm
    notify(state, "I", f"We are preparing your answer...")

    # Retrieve the callback parameters
    (_, _, message_hm, sender_id) = payload.get("args", [])

    # Default message used if evaluation fails
    result = "Invalid expression"
    try:
        # Evaluate the expression and store the result
        result = chatllm(message_hm)
    except Exception:
        pass

tgb.chat(
        # Note: messages is actually the "var_name" in the evaluate function
        messages="{messages}", 
        users=users, 
        on_action=evaluate, 
        sender_id="Human"
    )