-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
ef5c3d0
commit aa91aac
Showing
15 changed files
with
781 additions
and
20 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 @@ | ||
Leaves are green because chlorophyll absorbs red and blue light. |
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 @@ | ||
The sky is blue because of Rayleigh scattering. |
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,53 @@ | ||
# Chromem Example | ||
|
||
This example demonstrates how to use the Chromem vector database with Raggo's SimpleRAG interface. | ||
|
||
## Prerequisites | ||
|
||
1. Go 1.16 or later | ||
2. OpenAI API key (set as environment variable `OPENAI_API_KEY`) | ||
|
||
## Running the Example | ||
|
||
1. Set your OpenAI API key: | ||
```bash | ||
export OPENAI_API_KEY='your-api-key' | ||
``` | ||
|
||
2. Run the example: | ||
```bash | ||
go run main.go | ||
``` | ||
|
||
## What it Does | ||
|
||
1. Creates a new SimpleRAG instance with Chromem as the vector database | ||
2. Creates sample documents about natural phenomena | ||
3. Adds the documents to the database | ||
4. Performs a semantic search using the query "Why is the sky blue?" | ||
5. Prints the response based on the relevant documents found | ||
|
||
## Expected Output | ||
|
||
``` | ||
Question: Why is the sky blue? | ||
Answer: The sky appears blue because of a phenomenon called Rayleigh scattering. When sunlight travels through Earth's atmosphere, it collides with gas molecules. These molecules scatter blue wavelengths of light more strongly than red wavelengths, which is why we see the sky as blue. | ||
``` | ||
|
||
## Configuration | ||
|
||
The example uses the following configuration: | ||
- Vector Database: Chromem (persistent mode) | ||
- Collection Name: knowledge-base | ||
- Embedding Model: text-embedding-3-small | ||
- Chunk Size: 200 characters | ||
- Chunk Overlap: 50 characters | ||
- Top K Results: 1 | ||
- Minimum Score: 0.1 | ||
|
||
## Notes | ||
|
||
- The database is stored in `./data/chromem.db` | ||
- Sample documents are created in the `./data` directory | ||
- The example uses persistent storage mode for Chromem |
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,77 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/teilomillet/raggo" | ||
) | ||
|
||
func main() { | ||
// Enable debug logging | ||
raggo.SetLogLevel(raggo.LogLevelDebug) | ||
|
||
// Create a temporary directory for our documents | ||
tmpDir := "./data" | ||
err := os.MkdirAll(tmpDir, 0755) | ||
if err != nil { | ||
fmt.Printf("Error creating temp directory: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Create sample documents | ||
docs := map[string]string{ | ||
"sky.txt": "The sky is blue because of Rayleigh scattering.", | ||
"leaves.txt": "Leaves are green because chlorophyll absorbs red and blue light.", | ||
} | ||
|
||
for filename, content := range docs { | ||
err := os.WriteFile(filepath.Join(tmpDir, filename), []byte(content), 0644) | ||
if err != nil { | ||
fmt.Printf("Error writing file %s: %v\n", filename, err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// Initialize RAG with Chromem | ||
config := raggo.SimpleRAGConfig{ | ||
Collection: "knowledge-base", | ||
DBType: "chromem", | ||
DBAddress: "./data/chromem.db", | ||
Model: "text-embedding-3-small", // OpenAI embedding model | ||
APIKey: os.Getenv("OPENAI_API_KEY"), | ||
Dimension: 1536, // text-embedding-3-small dimension | ||
// TopK is determined dynamically by the number of documents | ||
} | ||
|
||
raggo.Debug("Creating SimpleRAG with config", "config", config) | ||
|
||
rag, err := raggo.NewSimpleRAG(config) | ||
if err != nil { | ||
fmt.Printf("Error creating SimpleRAG: %v\n", err) | ||
os.Exit(1) | ||
} | ||
defer rag.Close() | ||
|
||
ctx := context.Background() | ||
|
||
// Add documents from the directory | ||
raggo.Debug("Adding documents from directory", "dir", tmpDir) | ||
err = rag.AddDocuments(ctx, tmpDir) | ||
if err != nil { | ||
fmt.Printf("Error adding documents: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Search for documents | ||
raggo.Debug("Searching for documents", "query", "Why is the sky blue?") | ||
response, err := rag.Search(ctx, "Why is the sky blue?") | ||
if err != nil { | ||
fmt.Printf("Error searching: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Printf("Response: %s\n", response) | ||
} |
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
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
Oops, something went wrong.