Skip to content

Commit

Permalink
embeddings: add example of proper usage, and tweak doc.go accordingly
Browse files Browse the repository at this point in the history
For #379
  • Loading branch information
eliben committed Dec 1, 2023
1 parent bef6d7e commit ce6962e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
15 changes: 7 additions & 8 deletions embeddings/doc.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/*
Package embeddings contains the implementation for creating vector
embeddings from text using different APIs, like OpenAI and Google PaLM (VertexAI).
Package embeddings contains helpers for creating vector embeddings from text
using different providers.
The main components of this package are:
- Embedder interface: a common interface for creating vector embeddings from texts.
- OpenAI: an Embedder implementation using the OpenAI API.
- VertexAIPaLM: an Embedder implementation using Google PaLM (VertexAI) API.
- Helper functions: utility functions for embedding, such as `batchTexts` and `maybeRemoveNewLines`.
- [Embedder] interface: a common interface for creating vector embeddings
from texts, with optional batching.
- [NewEmbedder] creates implementations of [Embedder] from provider LLM
(or Chat) clients.
The package provides a flexible way to handle different APIs for generating
embeddings by using the Embedder interface as an abstraction.
See the package example below.
*/
package embeddings
30 changes: 30 additions & 0 deletions embeddings/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package embeddings_test

import (
"context"
"log"

"github.com/tmc/langchaingo/embeddings"
"github.com/tmc/langchaingo/llms/openai"
)

func Example() {

Check failure on line 11 in embeddings/example_test.go

View workflow job for this annotation

GitHub Actions / lint

missing output for example, go test can't validate it (testableexamples)

Check failure on line 11 in embeddings/example_test.go

View workflow job for this annotation

GitHub Actions / lint

missing output for example, go test can't validate it (testableexamples)
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}

embedder, err := embeddings.NewEmbedder(llm)
if err != nil {
log.Fatal(err)
}

docs := []string{"doc 1", "another doc"}
embs, err := embedder.EmbedDocuments(context.Background(), docs)
if err != nil {
log.Fatal(err)
}

// Consume embs
_ = embs
}

0 comments on commit ce6962e

Please sign in to comment.