Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate and add Tensorflow JS sentence embedding model #15

Open
woodthom2 opened this issue Apr 15, 2024 · 0 comments
Open

Evaluate and add Tensorflow JS sentence embedding model #15

woodthom2 opened this issue Apr 15, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@woodthom2
Copy link
Contributor

woodthom2 commented Apr 15, 2024

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


<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder"></script>
</head>

<body>

<script>
// 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);
});

</script>

</body>
</html>

@woodthom2 woodthom2 added the enhancement New feature or request label Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant