Skip to content
Merged
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
2 changes: 1 addition & 1 deletion encoders/closedai/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/conneroisu/semanticrouter-go/encoders/closedai

go 1.23.0
go 1.24.0

require github.com/sashabaranov/go-openai v1.29.1
14 changes: 13 additions & 1 deletion encoders/closedai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ import (
openai "github.com/sashabaranov/go-openai"
)

// Client is a minimal interface for the OpenAI client.
type Client interface {
CreateEmbeddings(ctx context.Context, req openai.EmbeddingRequest) (openai.EmbeddingResponse, error)
}

// Encoder encodes a query string into an OpenAI embedding.
type Encoder struct {
// Client is the OpenAI client.
Client *openai.Client
Client Client
// Model is the OpenAI embedding model to use.
Model openai.EmbeddingModel
}

func NewEncoder(client Client, model openai.EmbeddingModel) Encoder {
return Encoder{
Client: client,
Model: model,
}
}

// Encode encodes the given utterance using the OpenAI API.
func (o Encoder) Encode(
ctx context.Context,
Expand Down
39 changes: 39 additions & 0 deletions encoders/closedai/openai_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package closedai

import (
"context"
"testing"

openai "github.com/sashabaranov/go-openai"
"github.com/stretchr/testify/assert"
)

func TestEncoder_Encode(t *testing.T) {
t.Parallel()
client := &mockClient{}
encoder := NewEncoder(client, "text-embedding-ada-002")
utterance := "Hello, world!"
got, err := encoder.Encode(t.Context(), utterance)
expected := []float64{
0.0,
}
assert.Equal(t, expected, got)
assert.NoError(t, err)
}

type mockClient struct{}

func (m *mockClient) CreateEmbeddings(
_ context.Context,
req openai.EmbeddingRequest,
) (openai.EmbeddingResponse, error) {
return openai.EmbeddingResponse{
Data: []openai.Embedding{
{
Embedding: []float32{
0.0,
},
},
},
}, nil
}
4 changes: 2 additions & 2 deletions encoders/google/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package encoders provides encoders for Google language models.
// Package google provides encoders for Google language models.
//
// Google language models are language models that is trained on a large corpus of text data.
package encoders
package google
10 changes: 8 additions & 2 deletions encoders/google/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module github.com/conneroisu/semanticrouter-go/encoders/google

go 1.23.0
go 1.24.0

require github.com/google/generative-ai-go v0.17.0
require (
github.com/google/generative-ai-go v0.17.0
github.com/stretchr/testify v1.9.0
)

require (
cloud.google.com/go v0.115.0 // indirect
Expand All @@ -11,6 +14,7 @@ require (
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/longrunning v0.5.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -20,6 +24,7 @@ require (
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.12.5 // indirect
github.com/pmezard/go-difflib v1.0.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
Expand All @@ -38,4 +43,5 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions encoders/google/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
35 changes: 23 additions & 12 deletions encoders/google/google.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
package encoders
package google

import (
"context"

"github.com/google/generative-ai-go/genai"
)

// GoogleEncoder encodes a query string into a Google search URL.
type GoogleEncoder struct {
client genai.Client
// Client is a minimal client for the Google Generative AI API.
type Client interface {
EmbeddingModel(name string) Model
}

// Model is a minimal model for the Google Generative AI API.
type Model interface {
EmbedContent(ctx context.Context, content genai.Text) (genai.EmbedContentResponse, error)
}

// Encoder encodes a query string into a Google search URL.
type Encoder struct {
client Client
name string
}

// NewGoogleEncoder creates a new GoogleEncoder.
func NewGoogleEncoder(
client genai.Client,
) *GoogleEncoder {
return &GoogleEncoder{
// NewEncoder creates a new GoogleEncoder.
func NewEncoder(
client Client,
) *Encoder {
return &Encoder{
client: client,
}
}

// Encode encodes a query string into a Google search URL.
func (e *GoogleEncoder) Encode(
func (e *Encoder) Encode(
ctx context.Context,
query string,
) ([]float64, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
model := e.client.EmbeddingModel(e.name)
embedding, err := model.EmbedContent(ctx, genai.Text(query))
embedding, err := e.client.EmbeddingModel(
e.name,
).EmbedContent(ctx, genai.Text(query))
if err != nil {
return nil, err
}
Expand Down
34 changes: 34 additions & 0 deletions encoders/google/google_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package google

import (
"context"
"testing"

"github.com/google/generative-ai-go/genai"
"github.com/stretchr/testify/assert"
)

func TestEncoder_Encode(t *testing.T) {
ctx := context.Background()
mockClient := mockClient{}
encoder := NewEncoder(mockClient)
result, err := encoder.Encode(ctx, "query")
assert.NoError(t, err)
assert.Equal(t, []float64{0.0}, result)
}

type mockClient struct{}

func (m mockClient) EmbeddingModel(_ string) Model {
return mockModel{}
}

type mockModel struct{}

func (m mockModel) EmbedContent(_ context.Context, _ genai.Text) (genai.EmbedContentResponse, error) {
return genai.EmbedContentResponse{
Embedding: &genai.ContentEmbedding{
Values: []float32{0.0},
},
}, nil
}
2 changes: 1 addition & 1 deletion encoders/ollama/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/conneroisu/semanticrouter-go/encoders/ollama

go 1.23.0
go 1.24.0

require (
github.com/ollama/ollama v0.3.10
Expand Down
2 changes: 1 addition & 1 deletion encoders/voyageai/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/conneroisu/semanticrouter-go/encoders/voyageai

go 1.23.0
go 1.24.0

require github.com/conneroisu/go-voyageai v0.0.0-20240712192129-77bcd696824e
2 changes: 1 addition & 1 deletion examples/chit-chat/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/conneroisu/semanticrouter-go/examples/chit-chat

go 1.23.0
go 1.24.0

require github.com/sashabaranov/go-openai v1.29.1
2 changes: 1 addition & 1 deletion examples/veterinarian/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/conneroisu/semanticrouter-go/examples/veterinarian

go 1.23.0
go 1.24.0

require github.com/ollama/ollama v0.3.10
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/conneroisu/semanticrouter-go

go 1.23.0
go 1.24.0

require (
github.com/conneroisu/semanticrouter-go/encoders/ollama v0.0.0-20240909025305-0a3db7c99137
Expand Down
4 changes: 2 additions & 2 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
go 1.23.0
go 1.24.0

toolchain go1.23.0
toolchain go1.24.1

use (
.
Expand Down
2 changes: 1 addition & 1 deletion stores/memory/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/conneroisu/semanticrouter-go/stores/memory

go 1.23.0
go 1.24.0

require github.com/stretchr/testify v1.9.0

Expand Down
2 changes: 1 addition & 1 deletion stores/mongo/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/conneroisu/semanticrouter-go/stores/mongo

go 1.23.0
go 1.24.0

require (
github.com/stretchr/testify v1.9.0
Expand Down
35 changes: 26 additions & 9 deletions stores/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,52 @@ package mongo

import (
"context"
"io"

"github.com/conneroisu/semanticrouter-go"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// Cursor is a MongoDB cursor.
//
// It implements an minimal subset of the mongo.Cursor interface.
type Cursor interface {
All(ctx context.Context, result any) error
io.Closer
}

// Collection is a MongoDB collection.
//
// It implements an minimal subset of the mongo.Collection interface.
type Collection interface {
Find(ctx context.Context, filter any, opts ...*options.FindOptions) (cur *mongo.Cursor, err error)
InsertOne(ctx context.Context, doc any, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
}

// Store is a MongoDB store.
//
// It implements the Store interface.
type Store struct {
coll *mongo.Collection
coll Collection
}

// New creates a new MongoDB store.
func New(collection *mongo.Collection) *Store {
return &Store{
coll: collection,
}
func New(collection Collection) *Store {
return &Store{coll: collection}
}

// Get gets a value from the store.
func (s *Store) Get(ctx context.Context, utterance string) ([]float64, error) {
var floats []float64
filter := bson.M{"utterance": utterance}
cur, err := s.coll.Find(ctx, filter)
var (
floats []float64
results []semanticrouter.Utterance
)
cur, err := s.coll.Find(ctx, bson.M{"utterance": utterance})
if err != nil {
return nil, err
}
var results []semanticrouter.Utterance
if err = cur.All(ctx, &results); err != nil {
panic(err)
}
Expand Down
Loading