Files
langchaingo/examples/google-cloudsql-chat-message-history-example
Dmitry Ng 064f09155c chore(deps): bump Go toolchain to 1.26.5 and fix vulnerable dependencies
- Raised the `go` directive from 1.24.1 to 1.26.5 to satisfy pgx/v5 and ollama's minimum Go version requirements and to pick up 15 patched standard library CVEs.
- Updated `pgx/v5`, `otel`/`otel/sdk`/`otlptracehttp`, `x/net`, and the AWS `eventstream` protocol package to their latest requested versions.
- Remediated govulncheck-flagged CVEs in `grpc`, `x/text`, `x/crypto`, `antchfx/xpath`, AWS SDK `bedrock*`/`s3` services, `etcd/server`, and `mongo-driver` v1/v2.
- Fixed ollama's `MainGPU` API type change (`int` -> `*int`) in `llms/ollama/options.go` without breaking the public `WithRunnerMainGPU` signature.
2026-08-04 13:57:26 +03:00
..

Google CloudSQL Chat Message History Example

This example demonstrates how to use CloudSQL for Postgres as a backend for the ChatMessageHistory for LangChain in Go.

What This Example Does

  1. Creates a CloudSQL Chat Message History:

    • Initializes the cloudsql.PostgresEngine object to establish a connection to the CloudSQL database.
    • Creates a new table to store the chat message history if it doesn't exist.
    • Initializes a cloudsql.ChatMessageHistory object, which provides methods to store, retrieve, and clear message contents with a specific session ID.
  2. Add Single Messages:

    • Creates individual AI and Human messages and stores them in the chat message history.
    • Prints the stored messages to the console.
  3. Add Multiple Messages:

    • Creates multiple messages of different types (AI and Human) and stores them all at once in the chat message history.
    • Prints the stored messages to the console.
  4. Overwrite Messages:

    • Clears the existing messages and then stores a new set of messages.
    • Prints the updated list of stored messages.
  5. Clear All Messages:

    • Clears all messages stored for the current session.

How to Run the Example

  1. Set the following environment variables. Your CloudSQL values can be found in the Google Cloud Console:

    export PROJECT_ID=<your project Id>
    export POSTGRES_USERNAME=<your user>
    export POSTGRES_PASSWORD=<your password>
    export POSTGRES_REGION=<your region>
    export POSTGRES_INSTANCE=<your instance>
    export POSTGRES_DATABASE=<your database>
    export POSTGRES_TABLE=<your tablename>
    export POSTGRES_SESSION_ID=<your sessionID>
    
  2. Run the Go example:

    go run google_cloudsql_chat_message_history_example.go
    

Key Features

  • CloudSQL Integration: Connects to a CloudSQL instance for storing and managing chat messages.
  • Message Storage: Provides methods for storing chat messages with different operations like add, overwrite, and clear.
  • Session-based Message Management: Messages are stored and retrieved using a unique session ID, making it easy to manage separate chat sessions.
  • Clear and Overwrite Capabilities: Allows overwriting and clearing messages to maintain the chat history as needed.