Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions examples/googleai-milvus-rag-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Google Gemini + Milvus RAG Example

Welcome to the **Google Gemini + Milvus RAG Example**!
This Go example demonstrates how to build a simple **Retrieval-Augmented Generation (RAG)** pipeline using the [`langchaingo`](https://github.com/tmc/langchaingo) library, **Google Gemini** for embeddings and text generation, and **Milvus** as a vector database.

This example shows how you can combine vector search with large language models to answer questions based on your own documents — a foundation for building intelligent knowledge assistants, search tools, and chatbots.

---

## 🧩 What This Example Does

This example program:

1. Connects to the **Google Gemini API** using your API key.
2. Connects to a **Milvus** instance (local or cloud).
3. Embeds and inserts a few sample documents into Milvus.
4. Performs a **semantic similarity search** for a given query.
5. Sends the retrieved information to Gemini for a natural-language answer.

---

## ⚙️ How It Works

1. **Environment Setup**
- Loads the following environment variables:
- `GEMINI_API_KEY` → Required. Your Google Gemini API key.
- `MILVUS_URL` → Required. Address of your Milvus instance (`localhost:19530` for local Milvus, or your cloud endpoint).
- `MILVUS_API_KEY` → Optional. Only required for Milvus cloud instances.

2. **Embedding & Storage**
- The program uses Gemini’s embedding model (`gemini-embedding-001`) to convert text into vectors.
- These vectors are stored in Milvus under the collection name `"docs"`.

3. **Retrieval**
- A similarity search is performed for the query `"What is machine learning?"`.
- Milvus returns the top documents with the highest similarity scores.

4. **Answer Generation**
- The relevant text is provided to Gemini (`gemini-2.0-flash`) to generate a clear, concise English answer.

---

## 🪄 Example Documents

The following example notes are stored in Milvus:

- “The capital of France is Paris. It is known for its culture, art, and cuisine.”
- “Machine learning is a branch of artificial intelligence that enables systems to learn from data and improve over time without being explicitly programmed.”
- “Photosynthesis is the process by which green plants use sunlight to synthesize nutrients from carbon dioxide and water.”

---

## 🚀 Running the Example

### 1. Requirements

Make sure you have:

- **Go 1.25+** installed
- Access to a **Google Gemini API key**
- Access to a **Milvus instance** (local or cloud)

Verify your Go installation:

```bash
go version
```
If you don’t have Milvus running locally, you can start it quickly with Docker:
```bash
docker run -p 19530:19530 milvusdb/milvus:latest
```
### 2. Clone and Navigate
```bash
git clone https://github.com/tmc/langchaingo.git
cd langchaingo/examples/googleai-milvus-rag-example
```
### 3. Set Environment Variables
Linux / macOS
```bash
export GEMINI_API_KEY=your_gemini_api_key_here
export MILVUS_URL=localhost:19530
# Optional for Milvus Cloud
export MILVUS_API_KEY=your_milvus_api_key_here
```
Window
```bash
$env:GEMINI_API_KEY="your_gemini_api_key_here"
$env:MILVUS_URL="localhost:19530"
$env:MILVUS_API_KEY="your_milvus_api_key_here"
```
### 4. Run the Program
```bash
go run main.go
```
### 5. Example Output
```bash
Answer: Machine learning is a branch of artificial intelligence that allows systems to learn from data and improve automatically through experience.
```
137 changes: 137 additions & 0 deletions examples/googleai-milvus-rag-example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
module github.com/tmc/langchaingo/examples/googleai-milvus-rag-example

go 1.25.4

require (
cloud.google.com/go v0.118.3 // indirect
cloud.google.com/go/ai v0.7.0 // indirect
cloud.google.com/go/aiplatform v1.74.0 // indirect
cloud.google.com/go/auth v0.15.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/iam v1.4.1 // indirect
cloud.google.com/go/longrunning v0.6.4 // indirect
cloud.google.com/go/vertexai v0.12.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cilium/ebpf v0.11.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/getsentry/sentry-go v0.30.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/generative-ai-go v0.15.1 // indirect
github.com/google/s2a-go v0.1.9 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.5 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect
github.com/milvus-io/milvus-proto/go-api/v2 v2.6.3 // indirect
github.com/milvus-io/milvus-sdk-go/v2 v2.4.0 // indirect
github.com/milvus-io/milvus/client/v2 v2.6.1 // indirect
github.com/milvus-io/milvus/pkg/v2 v2.6.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
github.com/panjf2000/ants/v2 v2.11.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkoukk/tiktoken-go v0.1.6 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/samber/lo v1.27.0 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.11.1 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.10.0 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/tmc/langchaingo v0.1.14 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.etcd.io/bbolt v1.3.11 // indirect
go.etcd.io/etcd/api/v3 v3.5.5 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.5 // indirect
go.etcd.io/etcd/client/v2 v2.305.5 // indirect
go.etcd.io/etcd/client/v3 v3.5.5 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.5 // indirect
go.etcd.io/etcd/raft/v3 v3.5.5 // indirect
go.etcd.io/etcd/server/v3 v3.5.5 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 // indirect
go.opentelemetry.io/otel v1.36.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0 // indirect
go.opentelemetry.io/otel/metric v1.36.0 // indirect
go.opentelemetry.io/otel/sdk v1.36.0 // indirect
go.opentelemetry.io/otel/trace v1.36.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/oauth2 v0.30.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/time v0.10.0 // indirect
google.golang.org/api v0.224.0 // indirect
google.golang.org/genproto v0.0.0-20250303144028-a0af3efb3deb // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250227231956-55c901821b1e // indirect
google.golang.org/grpc v1.71.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.32.3 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading