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
8 changes: 4 additions & 4 deletions internal/mongodb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package mongodb
import (
"context"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
)

func NewClient(ctx context.Context, url string) (*mongo.Client, error) {
client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
client, err := mongo.Connect(options.Client().ApplyURI(url))
if err != nil {
return nil, err
}
Expand Down
18 changes: 10 additions & 8 deletions memory/mongo/mongo_chat_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/tmc/langchaingo/internal/mongodb"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/schema"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)

const (
Expand Down Expand Up @@ -41,14 +41,16 @@ func NewMongoDBChatMessageHistory(ctx context.Context, options ...ChatMessageHis
return nil, err
}

client, err := mongodb.NewClient(ctx, h.url)
if err != nil {
return nil, err
}
if h.client == nil {
client, err := mongodb.NewClient(ctx, h.url)
if err != nil {
return nil, err
}

h.client = client
h.client = client
}

h.collection = client.Database(h.databaseName).Collection(h.collectionName)
h.collection = h.client.Database(h.databaseName).Collection(h.collectionName)
// create session id index
if _, err := h.collection.Indexes().CreateOne(ctx, mongo.IndexModel{Keys: bson.D{{Key: mongoSessionIDKey, Value: 1}}}); err != nil {
return nil, err
Expand Down
10 changes: 9 additions & 1 deletion memory/mongo/mongo_chat_history_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mongo

import (
"errors"

"go.mongodb.org/mongo-driver/v2/mongo"
)

const (
Expand All @@ -26,7 +28,7 @@ func applyMongoDBChatOptions(options ...ChatMessageHistoryOption) (*ChatMessageH
option(h)
}

if h.url == "" {
if h.url == "" && h.client == nil {
return nil, errMongoInvalidURL
}
if h.sessionID == "" {
Expand Down Expand Up @@ -64,3 +66,9 @@ func WithDataBaseName(name string) ChatMessageHistoryOption {
p.databaseName = name
}
}

func WithDataBaseClient(client *mongo.Client) ChatMessageHistoryOption {
return func(p *ChatMessageHistory) {
p.client = client
}
}
Loading