baptisteArno / typebot.io

💬 Typebot is a powerful chatbot builder that you can self-host.
https://typebot.io
Other
7.38k stars 2.05k forks source link

FR: semantic similarity condition #1324

Closed 7flash closed 7 months ago

7flash commented 7 months ago

Currently I can branch out the conversation based on exact user input, but now I'd also like to be able approximate user message(even when written with typos) to one of available categories of intent and handle correspondingly

SweetJenna commented 7 months ago

@7flash you can use open ai blocks for it. @baptisteArno am I right? Is there a better way?

baptisteArno commented 7 months ago

Indeed, you can definitely do that with a AI completion block. See the OpenAI condition template for an example of that

7flash commented 7 months ago

I mean with vector embeddings? @baptisteArno

7flash commented 7 months ago

It's also called semantic router can be done like this

    message_to_intent = {
        'how much money i have?': 'show balance',
        'hi there': 'show greetings'
    }

    for message in message_to_intent.keys():
        similarity = cosine_similarity(user_msg_embedding, generate_embedding(message))
        intent = message_to_intent[message]
        if intent in similarities:
            similarities[intent]['sum'] += similarity
            similarities[intent]['count'] += 1
        else:
            similarities[intent] = {'sum': similarity, 'count': 1}

    average_similarities = [(intent, similarities[intent]['sum'] / similarities[intent]['count']) for intent in similarities]

    top_intent = sorted(average_similarities, key=lambda x: x[1], reverse=True)[0]

    if top_intent == 'show balance':
        # ...
    else:
        # ...
baptisteArno commented 7 months ago

That's not supported for now!