-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
huggingface "github.com/hupe1980/go-huggingface" | ||
) | ||
|
||
func main() { | ||
ic := huggingface.NewInferenceClient(os.Getenv("HUGGINGFACEHUB_API_TOKEN")) | ||
|
||
res, err := ic.SentenceSimilarity(context.Background(), &huggingface.SentenceSimilarityRequest{ | ||
Inputs: huggingface.SentenceSimilarityInputs{ | ||
SourceSentence: "That is a happy person", | ||
Sentences: []string{"That is a happy dog", "That is a very happy person", "Today is a sunny day"}, | ||
}, | ||
Model: "sentence-transformers/all-MiniLM-L6-v2", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package gohuggingface | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
) | ||
|
||
// SentenceSimilarityInputs represents the inputs for sentence similarity computation. | ||
type SentenceSimilarityInputs struct { | ||
SourceSentence string `json:"source_sentence"` | ||
Sentences []string `json:"sentences"` | ||
} | ||
|
||
// SentenceSimilarityRequest represents a request for sentence similarity computation. | ||
type SentenceSimilarityRequest struct { | ||
Inputs SentenceSimilarityInputs `json:"inputs"` | ||
Options Options `json:"options,omitempty"` | ||
Model string `json:"-"` | ||
} | ||
|
||
// SentenceSimilarityResponse represents the response for a sentence similarity computation request. | ||
type SentenceSimilarityResponse []float32 | ||
|
||
// SentenceSimilarity sends a sentence similarity computation request to the InferenceClient | ||
// and returns the sentence similarity response. | ||
func (ic *InferenceClient) SentenceSimilarity(ctx context.Context, req *SentenceSimilarityRequest) (SentenceSimilarityResponse, error) { | ||
if len(req.Inputs.SourceSentence) == 0 || len(req.Inputs.Sentences) == 0 { | ||
return nil, errors.New("sourceSentence and sentences are required") | ||
} | ||
|
||
body, err := ic.post(ctx, req.Model, "sentence-similarity", req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sentenceSimilarityResponse := SentenceSimilarityResponse{} | ||
|
||
if err := json.Unmarshal(body, &sentenceSimilarityResponse); err != nil { | ||
return nil, err | ||
} | ||
|
||
return sentenceSimilarityResponse, nil | ||
} |