Skip to content

Commit

Permalink
ragserver: start adding new example of a RAG server in Go
Browse files Browse the repository at this point in the history
Change-Id: I256449c9cd97ef53251e326d943858c237b4ea16
Reviewed-on: https://go-review.googlesource.com/c/example/+/608055
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Eli Bendersky <[email protected]>
TryBot-Bypass: Eli Bendersky <[email protected]>
Reviewed-by: Eli Bendersky <[email protected]>
  • Loading branch information
eliben authored and gopherbot committed Aug 26, 2024
1 parent 39e772f commit f942a68
Show file tree
Hide file tree
Showing 11 changed files with 884 additions and 0 deletions.
54 changes: 54 additions & 0 deletions ragserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# ragserver

*RAG stands for Retrieval Augmented Generation*

Demos of implementing a "RAG Server" in Go, using [Google
AI](https://ai.google.dev/) for embeddings and language models and
[Weaviate](https://weaviate.io/) as a vector database.


## How it works

The server we're developing is a standard Go HTTP server, listening on a local
port. See the next section for the request schema for this server. It supports
adding new documents to its context, and getting queries that would use this
context.

Weaviate has the be installed locally; the easiest way to do so is by using
`docker-compose` as described in the Usage section.

## Server request schema

```
/add/: POST {"documents": [{"text": "..."}, {"text": "..."}, ...]}
response: OK (no body)
/query/: GET {"content": "..."}
response: model response as a string
```

## Server variants

* `ragserver`: uses the Google AI Go SDK directly for LLM calls and embeddings,
and the Weaviate Go client library directly for interacting with Weaviate.

## Usage

* In terminal window 1, `cd tests` and run `docker-compose up`;
This will start the weaviate service in the foreground.
* In terminal window 2, run `GEMINI_API_KEY=... go run .` in the tested
`ragserver` directory.
* In terminal window 3, we can now run scripts to clear/populate the
weaviate DB and interact with `ragserver`. The following instructions are
for terminal window 3.

Run `cd tests`; then we can clear out the weaviate DB with
`./weaviate-delete-objects.sh`. To add documents to the DB through `ragserver`,
run `./add-documents.sh`. For a sample query, run `./query.sh`
Adjust the contents of these scripts as needed.

## Environment variables

* `SERVERPORT`: the port this server is listening on (default 9020)
* `WVPORT`: the port Weaviate is listening on (default 9035)
* `GEMINI_API_KEY`: API key for the Gemini service at https://ai.google.dev
63 changes: 63 additions & 0 deletions ragserver/ragserver/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module golang.org/x/example/ragserver/ragserver

go 1.23.0

require (
github.com/google/generative-ai-go v0.17.0
github.com/weaviate/weaviate v1.26.1
github.com/weaviate/weaviate-go-client/v4 v4.15.1
google.golang.org/api v0.194.0
)

require (
cloud.google.com/go v0.115.1 // indirect
cloud.google.com/go/ai v0.8.0 // indirect
cloud.google.com/go/auth v0.9.1 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/longrunning v0.5.7 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.21.2 // indirect
github.com/go-openapi/errors v0.22.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/loads v0.21.1 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/strfmt v0.23.0 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/validate v0.21.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit f942a68

Please sign in to comment.