Add DotProduct Method and README Example for Embedding Similarity Search (#492)
* Add DotProduct Method and README Example for Embedding Similarity Search - Implement a DotProduct() method for the Embedding struct to calculate the dot product between two embeddings. - Add a custom error type for vector length mismatch. - Update README.md with a complete example demonstrating how to perform an embedding similarity search for user queries. - Add unit tests to validate the new DotProduct() method and error handling. * Update README to focus on Embedding Semantic Similarity
This commit is contained in:
56
README.md
56
README.md
@@ -483,6 +483,62 @@ func main() {
|
||||
```
|
||||
</details>
|
||||
|
||||
<detail>
|
||||
<summary>Embedding Semantic Similarity</summary>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := openai.NewClient("your-token")
|
||||
|
||||
// Create an EmbeddingRequest for the user query
|
||||
queryReq := openai.EmbeddingRequest{
|
||||
Input: []string{"How many chucks would a woodchuck chuck"},
|
||||
Model: openai.AdaEmbeddingv2,
|
||||
}
|
||||
|
||||
// Create an embedding for the user query
|
||||
queryResponse, err := client.CreateEmbeddings(context.Background(), queryReq)
|
||||
if err != nil {
|
||||
log.Fatal("Error creating query embedding:", err)
|
||||
}
|
||||
|
||||
// Create an EmbeddingRequest for the target text
|
||||
targetReq := openai.EmbeddingRequest{
|
||||
Input: []string{"How many chucks would a woodchuck chuck if the woodchuck could chuck wood"},
|
||||
Model: openai.AdaEmbeddingv2,
|
||||
}
|
||||
|
||||
// Create an embedding for the target text
|
||||
targetResponse, err := client.CreateEmbeddings(context.Background(), targetReq)
|
||||
if err != nil {
|
||||
log.Fatal("Error creating target embedding:", err)
|
||||
}
|
||||
|
||||
// Now that we have the embeddings for the user query and the target text, we
|
||||
// can calculate their similarity.
|
||||
queryEmbedding := queryResponse.Data[0]
|
||||
targetEmbedding := targetResponse.Data[0]
|
||||
|
||||
similarity, err := queryEmbedding.DotProduct(&targetEmbedding)
|
||||
if err != nil {
|
||||
log.Fatal("Error calculating dot product:", err)
|
||||
}
|
||||
|
||||
log.Printf("The similarity score between the query and the target is %f", similarity)
|
||||
}
|
||||
|
||||
```
|
||||
</detail>
|
||||
|
||||
<details>
|
||||
<summary>Azure OpenAI Embeddings</summary>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user