harmonydata / harmonydata/app

Evaluate and add Tensorflow JS sentence embedding model

Open
#15 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
0
Forks
11
Avg merge
4h 9m
Merged PRs (30d)
1

Description

## Description

The user should be able to request the TensorFlow sentence embedding model. But first we should evaluate it (https://github.com/harmonydata/matching).

This has the huge advantage that the embedding model runs on client side so we don't use up server resources.

Linked to: https://github.com/harmonydata/app/issues/14

## Rationale

We have had requests to improve the model's matching and add options for more LLMs. TFJS should be easy to add because it's client side. But first we need to evaluate it!

## Code snippet

Here is an example HTML file using the https://github.com/tensorflow/tfjs-models Universal Sentence Encoder

```

// Load the model.
use.loadQnA().then(model => {
// Embed a dictionary of a query and responses. The input to the embed method
// needs to be in following format:
// {
// queries: string[];
// responses: Response[];
// }
// queries is an array of question strings
// responses is an array of following structure:
// {
// response: string;
// context?: string;
// }
// context is optional, it provides the context string of the answer.

const input = {
queries: ['I feel depressed'],
responses: [
'I feel sad',
'I feel happy',
]
};
var scores = [];
const embeddings = model.embed(input);
/*
* The output of the embed method is an object with two keys:
* {
* queryEmbedding: tf.Tensor;
* responseEmbedding: tf.Tensor;
* }
* queryEmbedding is a tensor containing embeddings for all queries.
* responseEmbedding is a tensor containing embeddings for all answers.
* You can call `arraySync()` to retrieve the values of the tensor.
* In this example, embed_query[0] is the embedding for the query
* 'How are you feeling today?'
* And embed_responses[0] is the embedding for the answer
* 'I\'m not feeling very well.'
*/
scores = tf.matMul(embeddings['queryEmbedding'],
embeddings['responseEmbedding'], false, true).dataSync();
console.log(scores);
});

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.