Skip to content

Commit

Permalink
Merge pull request #16 from ExaDev/sentence-similarity-fix
Browse files Browse the repository at this point in the history
Sentence similarity fix
  • Loading branch information
Mearman authored Jun 20, 2024
2 parents d350d27 + 554976f commit 0327278
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 0 additions & 2 deletions src/examples/sentence-similarity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ For more Details See: https://huggingface.co/docs/api-inference/detailed_paramet

https://breadboard-ai.web.app/?mode=build&board=https%3A%2F%2Fraw.githubusercontent.com%2FExaDev%2Fbreadboard-examples%2FHugging-Face-Clean-History%2Fsrc%2Fexamples%2Fsentence-similarity%2Fboard.json

NOTE: For this board to work we have to set the "sentences" type to array and provide input as : ["string", "string2", "string3"]

API Key can be generated for free: https://huggingface.co/settings/tokens

NOTE: SOMETIMES IT WILL ERROR BECAUSE MODEL IS LOADING, JUST RUN THE BOARD AGAIN.
10 changes: 5 additions & 5 deletions src/examples/sentence-similarity/board.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
"description": "The data to send to the hugging face api sentence similarity endpoint"
},
"sentences": {
"type": "list",
"type": "string",
"title": "sentences",
"default": "[That is a happy dog, That is a very happy person,Today is a sunny day]",
"description": "A list of sentences to compare the source sentence to"
"default": "That is a happy dog, That is a very happy person,Today is a sunny day",
"description": "Comma separated sentences we would like to compare to the source sentence"
},
"apiKey": {
"type": "string",
Expand Down Expand Up @@ -143,7 +143,7 @@
"id": "fn-3-run",
"type": "runJavascript",
"configuration": {
"code": "function fn_3(inputs) {const key=inputs.key;const auth={Authorization:`Bearer ${key}`};return{auth}}",
"code": "function fn_3(inputs) {const key=inputs.key;const auth={Authorization:`Bearer ${key}`,\"content-type\":\"application/json\"};return{auth}}",
"name": "fn_3",
"raw": true
}
Expand Down Expand Up @@ -178,7 +178,7 @@
"id": "fn-4-run",
"type": "runJavascript",
"configuration": {
"code": "function fn_4(input) {const{source_sentence,sentences}=input;const payload={\"inputs\":{source_sentence,sentences}};console.log(\"payload\",payload);return{payload}}",
"code": "function fn_4(input) {const{source_sentence,sentences}=input;const splitSentence=sentences.split(\",\");const payload={inputs:{source_sentence,sentences:splitSentence}};return{payload}}",
"name": "fn_4",
"raw": true
}
Expand Down
24 changes: 14 additions & 10 deletions src/examples/sentence-similarity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const soruceSentenceSchema = {
};

const sentencesSchema = {
type: "list",
type: "string",
title: "sentences",
default: "[That is a happy dog, That is a very happy person,Today is a sunny day]",
description: "A list of sentences to compare the source sentence to"
default: "That is a happy dog, That is a very happy person,Today is a sunny day",
description: "Comma separated sentences we would like to compare to the source sentence"
};

const keySchema = {
Expand All @@ -33,21 +33,25 @@ export type HuggingFaceSentenceSimilarityParams = {

const authenticate = code<{ key: string }>((inputs) => {
const key = inputs.key
const auth = { Authorization: `Bearer ${key}` }
const auth = { Authorization: `Bearer ${key}` , "content-type": "application/json"}

return { auth };
});

const handleParams = code<{ source_sentence: string, sentences: string[] }>((input) => {
const handleParams = code<{ source_sentence: string, sentences: string }>((input) => {
const { source_sentence, sentences } = input

const splitSentence = sentences.split(",")

const payload: HuggingFaceSentenceSimilarityParams = {
"inputs": {
inputs: {
source_sentence: source_sentence,
sentences: sentences
sentences: splitSentence
},
};



return { payload }
});

Expand All @@ -68,10 +72,10 @@ const serialized = await board(() => {
const task = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
const output = base.output({ $id: "main" });

const { auth } = authenticate({ key: inputs.apiKey as unknown as string })
const { auth } = authenticate({ key: inputs.apiKey.isString() })
const { payload } = handleParams({
source_sentence: inputs.source_sentence as unknown as string,
sentences: inputs.sentences as unknown as string[],
source_sentence: inputs.source_sentence.isString(),
sentences: inputs.sentences.isString(),
});

const response = core.fetch({
Expand Down

0 comments on commit 0327278

Please sign in to comment.