|
| 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 | +} |
0 commit comments