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
133 changes: 133 additions & 0 deletions src/oss/python/integrations/caches/singlestore.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: "SingleStore semantic cache"
description: "Integrate with the SingleStoreSemanticCache using LangChain Python."
---

`SingleStoreSemanticCache` implements semantic caching for LLM responses using [SingleStore](https://singlestore.com/) vector capabilities. Instead of exact string matching, it uses embeddings to find semantically similar cached queries, reducing API costs and improving response times.

Internally, `SingleStoreSemanticCache` creates a [`SingleStoreVectorStore`](/oss/integrations/vectorstores/singlestore) instance per LLM configuration, storing prompts as embedded vectors and returning cached responses when a semantically similar query is found.

| Class | Package | JS support |
| :--- | :--- | :---: |
| `SingleStoreSemanticCache` | `langchain_singlestore` | ❌ |

## Setup

```bash
pip install -qU langchain-singlestore
```

## Initialization

### Required parameters

- **embedding** (`Embeddings`): A text embedding model for computing query similarity.

### Cache parameters

- **cache_table_prefix** (`str`): Prefix for cache table names. Each LLM configuration gets its own table named `{prefix}{hash}`. Defaults to `"cache_"`.
- **search_threshold** (`float`): The minimum similarity score for a cached result to be considered a match. Defaults to `0.2`. When using `DOT_PRODUCT`, results with a score above this threshold are returned. When using `EUCLIDEAN_DISTANCE`, results with a score below this threshold are returned.

### SingleStoreVectorStore parameters

All additional keyword arguments are passed directly to the underlying [`SingleStoreVectorStore`](/oss/integrations/vectorstores/singlestore). This includes:

- **distance_strategy** (`DistanceStrategy`): Strategy for calculating vector distances. Defaults to `DOT_PRODUCT`. Options: `DOT_PRODUCT`, `EUCLIDEAN_DISTANCE`.
- **content_field** (`str`): Field to store prompt content. Defaults to `"content"`.
- **metadata_field** (`str`): Field to store metadata. Defaults to `"metadata"`.
- **vector_field** (`str`): Field to store vectors. Defaults to `"vector"`.
- **id_field** (`str`): Field to store IDs. Defaults to `"id"`.
- **use_vector_index** (`bool`): Enable vector indexing (requires SingleStore 8.5+). Defaults to `False`.
- **vector_index_name** (`str`): Name of the vector index. Ignored if `use_vector_index` is `False`.
- **vector_index_options** (`dict`): Options for the vector index. Ignored if `use_vector_index` is `False`.
- **vector_size** (`int`): Size of the vector. Defaults to `1536`. Required if `use_vector_index` is `True`.

For the full list of vector store, connection pool, database connection, and driver options, see the [`SingleStoreVectorStore` integration page](/oss/integrations/vectorstores/singlestore).

### Environment variables

Connection parameters and credentials can be provided through environment variables instead of passing them directly. The underlying `singlestoredb` driver recognizes the following variables:

| Environment variable | Description |
| :--- | :--- |
| `SINGLESTOREDB_URL` | Connection URL in the format `user:password@host:port/db_name` |
| `SINGLESTOREDB_HOST` | Database hostname or IP address |
| `SINGLESTOREDB_PORT` | Database port number |
| `SINGLESTOREDB_USER` | Database username |
| `SINGLESTOREDB_PASSWORD` | Database password |
| `SINGLESTOREDB_DATABASE` | Database name |

For a complete list of supported connection parameters, see the [`singlestoredb.connect` API reference](https://singlestoredb-python.labs.singlestore.com/generated/singlestoredb.connect.html).

## Basic usage

```python
from langchain.globals import set_llm_cache
from langchain_openai import OpenAI, OpenAIEmbeddings
from langchain_singlestore import SingleStoreSemanticCache

set_llm_cache(
SingleStoreSemanticCache(
embedding=OpenAIEmbeddings(),
host="127.0.0.1:3306/db",
)
)

llm = OpenAI()

# First call: hits the LLM API
response = llm.invoke("What is the capital of France?")

# Semantically similar query: returns cached result
response = llm.invoke("Tell me the capital city of France")
```

## Configure with environment variables

```python
import os

from langchain.globals import set_llm_cache
from langchain_openai import OpenAI, OpenAIEmbeddings
from langchain_singlestore import SingleStoreSemanticCache

os.environ["SINGLESTOREDB_URL"] = "user:password@host:3306/db_name"

set_llm_cache(
SingleStoreSemanticCache(
embedding=OpenAIEmbeddings(),
)
)
```

## Configure with vector index

```python
from langchain.globals import set_llm_cache
from langchain_openai import OpenAIEmbeddings
from langchain_singlestore import SingleStoreSemanticCache

set_llm_cache(
SingleStoreSemanticCache(
embedding=OpenAIEmbeddings(),
host="127.0.0.1:3306/db",
search_threshold=0.3,
use_vector_index=True,
vector_size=1536,
)
)
```

## Clear the cache

Clear the cache for a specific LLM configuration:

```python
from langchain.globals import get_llm_cache

get_llm_cache().clear(llm_string="my_llm_string")
```

## API reference

For detailed documentation of all `SingleStoreSemanticCache` features and configurations, see the [langchain-singlestore GitHub repository](https://github.com/singlestore-labs/langchain-singlestore/).
159 changes: 159 additions & 0 deletions src/oss/python/integrations/chat_message_histories/singlestore.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
---
title: "SingleStore chat message history"
description: "Store chat message history in SingleStore using LangChain Python."
---

`SingleStoreChatMessageHistory` provides persistent storage for chat message history in [SingleStore](https://singlestore.com/). It integrates with LangChain chat models and chains to maintain conversation context across sessions.

| Class | Package | JS support |
| :--- | :--- | :---: |
| `SingleStoreChatMessageHistory` | `langchain_singlestore` | ❌ |

## Setup

```bash
pip install -qU langchain-singlestore
```

## Initialization

### Required parameters

- **session_id** (`str`): Unique identifier for the conversation session.

### Table parameters

- **table_name** (`str`): Name of the table for storing messages. Defaults to `"message_store"`.
- **id_field** (`str`): Name of the auto-increment primary key column. Defaults to `"id"`.
- **session_id_field** (`str`): Name of the column storing session IDs. Defaults to `"session_id"`.
- **message_field** (`str`): Name of the column storing message JSON. Defaults to `"message"`.

### Connection pool parameters

- **pool_size** (`int`): Number of active connections in the pool. Defaults to `5`.
- **max_overflow** (`int`): Maximum connections beyond `pool_size`. Defaults to `10`.
- **timeout** (`float`): Connection timeout in seconds. Defaults to `30`.

### Database connection parameters

All remaining keyword arguments are passed directly to the underlying [`singlestoredb` Python driver](https://singlestoredb-python.labs.singlestore.com/generated/singlestoredb.connect.html):

- **host** (`str`): Hostname, IP address, or URL for the database. Supports the URL format `[scheme://][user:password@]host[:port]/db_name`.
- **user** (`str`): Database username.
- **password** (`str`): Database password.
- **port** (`int`): Database port. Defaults to `3306` for non-HTTP connections, `80` for HTTP, and `443` for HTTPS.
- **database** (`str`): Database name.
- **pure_python** (`bool`): Use the connector in pure Python mode.
- **local_infile** (`bool`): Allow local file uploads.
- **charset** (`str`): Character set for string values.
- **ssl_key**, **ssl_cert**, **ssl_ca** (`str`): Paths to SSL key, certificate, and CA files.
- **ssl_cipher** (`str`): Sets the SSL cipher list.
- **ssl_disabled** (`bool`): Disable SSL usage.
- **ssl_verify_cert** (`bool`): Verify the server's certificate. Automatically enabled if `ssl_ca` is specified.
- **ssl_verify_identity** (`bool`): Verify the server's identity.
- **credential_type** (`str`): Type of authentication: `PASSWORD`, `JWT`, or `BROWSER_SSO`.
- **autocommit** (`bool`): Enable autocommits.
- **results_type** (`str`): Structure of query results (e.g., `tuples`, `namedtuples`, `dicts`).

For a complete list of supported parameters, see the [`singlestoredb.connect` API reference](https://singlestoredb-python.labs.singlestore.com/generated/singlestoredb.connect.html).

### Environment variables

Connection parameters and credentials can be provided through environment variables instead of passing them directly:

| Environment variable | Description |
| :--- | :--- |
| `SINGLESTOREDB_URL` | Connection URL in the format `user:password@host:port/db_name` |
| `SINGLESTOREDB_HOST` | Database hostname or IP address |
| `SINGLESTOREDB_PORT` | Database port number |
| `SINGLESTOREDB_USER` | Database username |
| `SINGLESTOREDB_PASSWORD` | Database password |
| `SINGLESTOREDB_DATABASE` | Database name |

## Basic usage

```python
from langchain.messages import AIMessage, HumanMessage
from langchain_singlestore import SingleStoreChatMessageHistory

chat_history = SingleStoreChatMessageHistory(
session_id="user-123-session-1",
host="127.0.0.1:3306/db",
)

# Add messages
chat_history.add_message(HumanMessage(content="What is SingleStore?"))
chat_history.add_message(
AIMessage(content="SingleStore is a high-performance distributed SQL database.")
)

# Retrieve messages
for msg in chat_history.messages:
print(f"{msg.type}: {msg.content}")
```

## Configure with environment variables

```python
import os

from langchain_singlestore import SingleStoreChatMessageHistory

os.environ["SINGLESTOREDB_URL"] = "user:password@host:3306/db_name"

chat_history = SingleStoreChatMessageHistory(
session_id="user-123-session-1",
)
```

## Configure with explicit connection parameters

```python
from langchain_singlestore import SingleStoreChatMessageHistory

chat_history = SingleStoreChatMessageHistory(
session_id="user-123-session-1",
host="127.0.0.1",
port=3306,
user="user",
password="password",
database="db",
table_name="my_custom_table",
pool_size=10,
timeout=60,
)
```

## Clear conversation history

```python
chat_history.clear()
```

## Use with a chat model

```python
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
from langchain_singlestore import SingleStoreChatMessageHistory

chat_history = SingleStoreChatMessageHistory(
session_id="user-123-session-2",
host="127.0.0.1:3306/db",
)

prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant."),
MessagesPlaceholder(variable_name="history"),
("human", "{input}"),
]
)

llm = ChatOpenAI()
chain = prompt | llm
```

## API reference

For detailed documentation of all `SingleStoreChatMessageHistory` features and configurations, see the [langchain-singlestore GitHub repository](https://github.com/singlestore-labs/langchain-singlestore/).
Loading
Loading