diff --git a/examples/README.md b/examples/README.md index b03a432..fdacd89 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,9 +1,13 @@ # Examples -1. [RAG Wikipedia Ollama](rag-wikipedia-ollama) +1. [Minimal example](minimal) + - A minimal example with the least amount of code and no comments + - Uses OpenAI for creating the embeddings +2. [RAG Wikipedia Ollama](rag-wikipedia-ollama) - This example shows a retrieval augmented generation (RAG) application, using `chromem-go` as knowledge base for finding relevant info for a question. More specifically the app is doing *question answering*. - The underlying data is 200 Wikipedia articles (or rather their lead section / introduction). - - We run the embeddings model and LLM in [Ollama](https://github.com/ollama/ollama), to showcase how a RAG application can run entirely offline, without relying on OpenAI or other third party APIs. -2. [Semantic search arXiv OpenAI](semantic-search-arxiv-openai) + - Runs the embeddings model and LLM in [Ollama](https://github.com/ollama/ollama), to showcase how a RAG application can run entirely offline, without relying on OpenAI or other third party APIs. +3. [Semantic search arXiv OpenAI](semantic-search-arxiv-openai) - This example shows a semantic search application, using `chromem-go` as vector database for finding semantically relevant search results. - - We load and search across ~5,000 arXiv papers in the "Computer Science - Computation and Language" category, which is the relevant one for Natural Language Processing (NLP) related papers. + - Loads and searches across ~5,000 arXiv papers in the "Computer Science - Computation and Language" category, which is the relevant one for Natural Language Processing (NLP) related papers. + - Uses OpenAI for creating the embeddings diff --git a/examples/minimal/README.md b/examples/minimal/README.md new file mode 100644 index 0000000..86996be --- /dev/null +++ b/examples/minimal/README.md @@ -0,0 +1,16 @@ +# Minimal example + +This is a minimal example that shows how `chromem-go` works. For more sophisticated examples that use the persistent DB, locally running embedding models, a basic RAG pipeline, with more explanations in the README as well as code comments, check the other examples! + +## How to run + +1. Set the OpenAI API key in your env as `OPENAI_API_KEY` +2. Run the example: `go run .` + +## Output + +```text +ID: 1 +Similarity: 0.6833369 +Content: The sky is blue because of Rayleigh scattering. +``` diff --git a/examples/minimal/go.mod b/examples/minimal/go.mod new file mode 100644 index 0000000..7bccb45 --- /dev/null +++ b/examples/minimal/go.mod @@ -0,0 +1,7 @@ +module github.com/philippgille/chromem-go/examples/minimal + +go 1.21 + +require github.com/philippgille/chromem-go v0.0.0 + +replace github.com/philippgille/chromem-go => ./../.. diff --git a/examples/minimal/main.go b/examples/minimal/main.go new file mode 100644 index 0000000..6c0efbf --- /dev/null +++ b/examples/minimal/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "context" + "fmt" + "runtime" + + "github.com/philippgille/chromem-go" +) + +func main() { + ctx := context.Background() + + db := chromem.NewDB() + + c, err := db.CreateCollection("knowledge-base", nil, nil) + if err != nil { + panic(err) + } + + c.AddDocuments(ctx, []chromem.Document{ + { + ID: "1", + Content: "The sky is blue because of Rayleigh scattering.", + }, + { + ID: "2", + Content: "Leaves are green because chlorophyll absorbs red and blue light.", + }, + }, runtime.NumCPU()) + + res, err := c.Query(ctx, "Why is the sky blue?", 1, nil, nil) + if err != nil { + panic(err) + } + + fmt.Printf("ID: %v\nSimilarity: %v\nContent: %v\n", res[0].ID, res[0].Similarity, res[0].Content) + + /* Output: + ID: 1 + Similarity: 0.6833369 + Content: The sky is blue because of Rayleigh scattering. + */ +}