Skip to content

Commit 77b2d7b

Browse files
authored
memory: add interfaces for Google AlloyDB and Cloud SQL (tmc#1205)
* memory: add interfaces for Google AlloyDB and Cloud SQL
1 parent 0174097 commit 77b2d7b

15 files changed

Lines changed: 1505 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Google AlloyDB Chat Message History Example
2+
3+
This example demonstrates how to use [AlloyDB for Postgres](https://cloud.google.com/products/alloydb) as a backend for the ChatMessageHistory for LangChain in Go.
4+
5+
## What This Example Does
6+
7+
1. **Creates an AlloyDB Chat Message History:**
8+
- Initializes the `alloydb.PostgresEngine` object to establish a connection to the AlloyDB database.
9+
- Creates a new table to store the chat message history if it doesn't exist.
10+
- Initializes an `alloydb.ChatMessageHistory` object, which provides methods to store, retrieve, and clear message contents with a specific session ID.
11+
12+
2. **Add Single Messages:**
13+
- Creates individual AI and Human messages and stores them in the chat message history.
14+
- Prints the stored messages to the console.
15+
16+
3. **Add Multiple Messages:**
17+
- Creates multiple messages of different types (AI and Human) and stores them all at once in the chat message history.
18+
- Prints the stored messages to the console.
19+
20+
4. **Overwrite Messages:**
21+
- Clears the existing messages and then stores a new set of messages.
22+
- Prints the updated list of stored messages.
23+
24+
5. **Clear All Messages:**
25+
- Clears all messages stored for the current session.
26+
27+
## How to Run the Example
28+
29+
1. Set the following environment variables. Your AlloyDB values can be found in the [Google Cloud Console](https://console.cloud.google.com/alloydb/clusters):
30+
```
31+
export PROJECT_ID=<your project Id>
32+
export ALLOYDB_USERNAME=<your user>
33+
export ALLOYDB_PASSWORD=<your password>
34+
export ALLOYDB_REGION=<your region>
35+
export ALLOYDB_CLUSTER=<your cluster>
36+
export ALLOYDB_INSTANCE=<your instance>
37+
export ALLOYDB_DATABASE=<your database>
38+
export ALLOYDB_TABLE=<your tablename>
39+
export ALLOYDB_SESSION_ID=<your sessionID>
40+
```
41+
42+
2. Run the Go example:
43+
```
44+
go run google_alloydb_chat_message_history_example.go
45+
```
46+
47+
## Key Features
48+
- **AlloyDB Integration**: Connects to an AlloyDB instance for storing and managing chat messages.
49+
- **Message Storage**: Provides methods for storing chat messages with different operations like add, overwrite, and clear.
50+
- **Session-based Message Management**: Messages are stored and retrieved using a unique session ID, making it easy to manage separate chat sessions.
51+
- **Clear and Overwrite Capabilities**: Allows overwriting and clearing messages to maintain the chat history as needed.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/tmc/langchaingo/llms"
10+
"github.com/tmc/langchaingo/memory/alloydb"
11+
"github.com/tmc/langchaingo/util/alloydbutil"
12+
)
13+
14+
// getEnvVariables loads the necessary environment variables for the AlloyDB connection
15+
// and the chat message history creation.
16+
func getEnvVariables() (string, string, string, string, string, string, string, string, string) {
17+
// Requires environment variable ALLOYDB_USERNAME to be set.
18+
username := os.Getenv("ALLOYDB_USERNAME")
19+
if username == "" {
20+
log.Fatal("environment variable ALLOYDB_USERNAME is empty")
21+
}
22+
// Requires environment variable ALLOYDB_PASSWORD to be set.
23+
password := os.Getenv("ALLOYDB_PASSWORD")
24+
if password == "" {
25+
log.Fatal("environment variable ALLOYDB_PASSWORD is empty")
26+
}
27+
// Requires environment variable ALLOYDB_DATABASE to be set.
28+
database := os.Getenv("ALLOYDB_DATABASE")
29+
if database == "" {
30+
log.Fatal("environment variable ALLOYDB_DATABASE is empty")
31+
}
32+
// Requires environment variable PROJECT_ID to be set.
33+
projectID := os.Getenv("PROJECT_ID")
34+
if projectID == "" {
35+
log.Fatal("environment variable PROJECT_ID is empty")
36+
}
37+
// Requires environment variable ALLOYDB_REGION to be set.
38+
region := os.Getenv("ALLOYDB_REGION")
39+
if region == "" {
40+
log.Fatal("environment variable ALLOYDB_REGION is empty")
41+
}
42+
// Requires environment variable ALLOYDB_INSTANCE to be set.
43+
instance := os.Getenv("ALLOYDB_INSTANCE")
44+
if instance == "" {
45+
log.Fatal("environment variable ALLOYDB_INSTANCE is empty")
46+
}
47+
// Requires environment variable ALLOYDB_CLUSTER to be set.
48+
cluster := os.Getenv("ALLOYDB_CLUSTER")
49+
if cluster == "" {
50+
log.Fatal("environment variable ALLOYDB_CLUSTER is empty")
51+
}
52+
// Requires environment variable ALLOYDB_TABLE to be set.
53+
tableName := os.Getenv("ALLOYDB_TABLE")
54+
if tableName == "" {
55+
log.Fatal("environment variable ALLOYDB_TABLE is empty")
56+
}
57+
// Requires environment variable ALLOYDB_SESSION_ID to be set.
58+
sessionID := os.Getenv("ALLOYDB_SESSION_ID")
59+
if sessionID == "" {
60+
log.Fatal("environment variable ALLOYDB_SESSION_ID is empty")
61+
}
62+
63+
return username, password, database, projectID, region, instance, cluster, tableName, sessionID
64+
}
65+
66+
func printMessages(ctx context.Context, cmh alloydb.ChatMessageHistory) {
67+
msgs, err := cmh.Messages(ctx)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
for _, msg := range msgs {
72+
fmt.Println("Message:", msg)
73+
}
74+
}
75+
76+
func main() {
77+
// Requires that the Environment variables to be set as indicated in the getEnvVariables function.
78+
username, password, database, projectID, region, instance, cluster, tableName, sessionID := getEnvVariables()
79+
ctx := context.Background()
80+
81+
pgEngine, err := alloydbutil.NewPostgresEngine(ctx,
82+
alloydbutil.WithUser(username),
83+
alloydbutil.WithPassword(password),
84+
alloydbutil.WithDatabase(database),
85+
alloydbutil.WithAlloyDBInstance(projectID, region, cluster, instance),
86+
)
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
91+
// Creates a new table in the Postgres database, which will be used for storing Chat History.
92+
err = pgEngine.InitChatHistoryTable(ctx, tableName)
93+
if err != nil {
94+
log.Fatal(err)
95+
}
96+
97+
// Creates a new Chat Message History
98+
cmh, err := alloydb.NewChatMessageHistory(ctx, pgEngine, tableName, sessionID)
99+
if err != nil {
100+
log.Fatal(err)
101+
}
102+
103+
// Creates individual messages and adds them to the chat message history.
104+
aiMessage := llms.AIChatMessage{Content: "test AI message"}
105+
humanMessage := llms.HumanChatMessage{Content: "test HUMAN message"}
106+
// Adds a user message to the chat message history.
107+
err = cmh.AddUserMessage(ctx, aiMessage.GetContent())
108+
if err != nil {
109+
log.Fatal(err)
110+
}
111+
// Adds a user message to the chat message history.
112+
err = cmh.AddUserMessage(ctx, humanMessage.GetContent())
113+
if err != nil {
114+
log.Fatal(err)
115+
}
116+
117+
printMessages(ctx, cmh)
118+
119+
// Create multiple messages and store them in the chat message history at the same time.
120+
multipleMessages := []llms.ChatMessage{
121+
llms.AIChatMessage{Content: "first AI test message from AddMessages"},
122+
llms.AIChatMessage{Content: "second AI test message from AddMessages"},
123+
llms.HumanChatMessage{Content: "first HUMAN test message from AddMessages"},
124+
}
125+
126+
// Adds multiple messages to the chat message history.
127+
err = cmh.AddMessages(ctx, multipleMessages)
128+
if err != nil {
129+
log.Fatal(err)
130+
}
131+
132+
printMessages(ctx, cmh)
133+
134+
// Create messages that will overwrite the existing ones
135+
overWrittingMessages := []llms.ChatMessage{
136+
llms.AIChatMessage{Content: "overwritten AI test message"},
137+
llms.HumanChatMessage{Content: "overwritten HUMAN test message"},
138+
}
139+
// Overwrites the existing messages with new ones.
140+
err = cmh.SetMessages(ctx, overWrittingMessages)
141+
if err != nil {
142+
log.Fatal(err)
143+
}
144+
145+
printMessages(ctx, cmh)
146+
147+
// Clear all the messages from the current session.
148+
err = cmh.Clear(ctx)
149+
if err != nil {
150+
log.Fatal(err)
151+
}
152+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Google CloudSQL Chat Message History Example
2+
3+
This example demonstrates how to use [CloudSQL for Postgres](https://cloud.google.com/sql) as a backend for the ChatMessageHistory for LangChain in Go.
4+
5+
## What This Example Does
6+
7+
1. **Creates a CloudSQL Chat Message History:**
8+
- Initializes the `cloudsql.PostgresEngine` object to establish a connection to the CloudSQL database.
9+
- Creates a new table to store the chat message history if it doesn't exist.
10+
- Initializes a `cloudsql.ChatMessageHistory` object, which provides methods to store, retrieve, and clear message contents with a specific session ID.
11+
12+
2. **Add Single Messages:**
13+
- Creates individual AI and Human messages and stores them in the chat message history.
14+
- Prints the stored messages to the console.
15+
16+
3. **Add Multiple Messages:**
17+
- Creates multiple messages of different types (AI and Human) and stores them all at once in the chat message history.
18+
- Prints the stored messages to the console.
19+
20+
4. **Overwrite Messages:**
21+
- Clears the existing messages and then stores a new set of messages.
22+
- Prints the updated list of stored messages.
23+
24+
5. **Clear All Messages:**
25+
- Clears all messages stored for the current session.
26+
27+
## How to Run the Example
28+
29+
1. Set the following environment variables. Your CloudSQL values can be found in the [Google Cloud Console](https://console.cloud.google.com/sql/instances):
30+
```
31+
export PROJECT_ID=<your project Id>
32+
export POSTGRES_USERNAME=<your user>
33+
export POSTGRES_PASSWORD=<your password>
34+
export POSTGRES_REGION=<your region>
35+
export POSTGRES_INSTANCE=<your instance>
36+
export POSTGRES_DATABASE=<your database>
37+
export POSTGRES_TABLE=<your tablename>
38+
export POSTGRES_SESSION_ID=<your sessionID>
39+
```
40+
41+
2. Run the Go example:
42+
```
43+
go run google_cloudsql_chat_message_history_example.go
44+
```
45+
46+
## Key Features
47+
- **CloudSQL Integration**: Connects to a CloudSQL instance for storing and managing chat messages.
48+
- **Message Storage**: Provides methods for storing chat messages with different operations like add, overwrite, and clear.
49+
- **Session-based Message Management**: Messages are stored and retrieved using a unique session ID, making it easy to manage separate chat sessions.
50+
- **Clear and Overwrite Capabilities**: Allows overwriting and clearing messages to maintain the chat history as needed.

0 commit comments

Comments
 (0)