typebot.io icon indicating copy to clipboard operation
typebot.io copied to clipboard

FR: semantic similarity condition

Open 7flash opened this issue 1 year ago • 1 comments

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

7flash avatar Mar 05 '24 10:03 7flash

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

SweetJenna avatar Mar 06 '24 13:03 SweetJenna

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

baptisteArno avatar Mar 07 '24 08:03 baptisteArno

I mean with vector embeddings? @baptisteArno

7flash avatar Mar 07 '24 09:03 7flash

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:
		# ...

7flash avatar Mar 07 '24 09:03 7flash

That's not supported for now!

baptisteArno avatar Mar 11 '24 08:03 baptisteArno