langchain-gridgain 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_gridgain-1.0.0/MANIFEST.in +2 -0
- langchain_gridgain-1.0.0/PKG-INFO +308 -0
- langchain_gridgain-1.0.0/README.md +289 -0
- langchain_gridgain-1.0.0/pyproject.toml +50 -0
- langchain_gridgain-1.0.0/setup.cfg +4 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/__init__.py +7 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/__pycache__/__init__.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/__pycache__/chat_message_histories.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/__pycache__/document_loaders.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/__pycache__/llm_cache.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/__pycache__/storage.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/__pycache__/vectorstores.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/chat_message_histories.py +115 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/document_loaders.py +166 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/llm_cache.py +357 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/storage.py +133 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain/vectorstores.py +317 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain.egg-info/PKG-INFO +308 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain.egg-info/SOURCES.txt +49 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain.egg-info/dependency_links.txt +1 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain.egg-info/requires.txt +13 -0
- langchain_gridgain-1.0.0/src/langchain_gridgain.egg-info/top_level.txt +1 -0
- langchain_gridgain-1.0.0/tests/README.md +176 -0
- langchain_gridgain-1.0.0/tests/__init__.py +0 -0
- langchain_gridgain-1.0.0/tests/__pycache__/__init__.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/__init__.py +0 -0
- langchain_gridgain-1.0.0/tests/integration/__pycache__/__init__.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/__pycache__/test_chat_message_histories.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/__pycache__/test_document_loaders.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/__pycache__/test_llm_cache.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/__pycache__/test_semantic_llm_cache.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/__pycache__/test_storage.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/__pycache__/test_vectorstores.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/integration/test_chat_message_histories.py +76 -0
- langchain_gridgain-1.0.0/tests/integration/test_document_loaders.py +105 -0
- langchain_gridgain-1.0.0/tests/integration/test_llm_cache.py +156 -0
- langchain_gridgain-1.0.0/tests/integration/test_semantic_llm_cache.py +147 -0
- langchain_gridgain-1.0.0/tests/integration/test_storage.py +63 -0
- langchain_gridgain-1.0.0/tests/integration/test_vectorstores.py +178 -0
- langchain_gridgain-1.0.0/tests/unit/__init__.py +0 -0
- langchain_gridgain-1.0.0/tests/unit/__pycache__/__init__.cpython-311.pyc +0 -0
- langchain_gridgain-1.0.0/tests/unit/__pycache__/test_chat_message_histories.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/unit/__pycache__/test_document_loaders.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/unit/__pycache__/test_llm_cache.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/unit/__pycache__/test_storage.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/unit/__pycache__/test_vectorstores.cpython-311-pytest-8.3.4.pyc +0 -0
- langchain_gridgain-1.0.0/tests/unit/test_chat_message_histories.py +91 -0
- langchain_gridgain-1.0.0/tests/unit/test_document_loaders.py +132 -0
- langchain_gridgain-1.0.0/tests/unit/test_llm_cache.py +120 -0
- langchain_gridgain-1.0.0/tests/unit/test_storage.py +164 -0
- langchain_gridgain-1.0.0/tests/unit/test_vectorstores.py +112 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: langchain-gridgain
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: This package makes it possible to use Gridgain as a Vector Store, Document Loader, LLM Cache, Key Value Store, Chat Memory within langchain
|
|
5
|
+
Author-email: Manini Puranik <manini.puranik@gridgain.com>, Aditi Sharma <aditi.sharma@gridgain.com>
|
|
6
|
+
Requires-Python: >=3.11.7
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: langchain==0.3.10
|
|
9
|
+
Requires-Dist: langchain-community~=0.3.10
|
|
10
|
+
Requires-Dist: pandas==2.2.2
|
|
11
|
+
Requires-Dist: numpy==1.26.4
|
|
12
|
+
Requires-Dist: pyignite==0.6.1
|
|
13
|
+
Requires-Dist: pygridgain==1.5.0
|
|
14
|
+
Provides-Extra: test
|
|
15
|
+
Requires-Dist: pytest; extra == "test"
|
|
16
|
+
Requires-Dist: pytest-asyncio; extra == "test"
|
|
17
|
+
Provides-Extra: coverage
|
|
18
|
+
Requires-Dist: pytest-cov; extra == "coverage"
|
|
19
|
+
|
|
20
|
+
# langchain-gridgain
|
|
21
|
+
|
|
22
|
+
langchain-gridgain is a Python library that provides seamless integration between GridGain/Apache Ignite and LangChain. This library offers a set of storage adapters that allow LangChain components to efficiently use GridGain as a backend for various data storage needs.
|
|
23
|
+
|
|
24
|
+
## Table of Contents
|
|
25
|
+
1. [Features](#features)
|
|
26
|
+
2. [Prerequisites](#prerequisites)
|
|
27
|
+
3. [Installation](#installation)
|
|
28
|
+
4. [GridGain Setup](#gridgain-setup)
|
|
29
|
+
- [Connecting to GridGain](#1-connecting-to-gridgain)
|
|
30
|
+
5. [Detailed Component Explanations](#detailed-component-explanations)
|
|
31
|
+
- [GridGainStore](#1-gridgainstore)
|
|
32
|
+
- [GridGainDocumentLoader](#2-gridgaindocumentloader)
|
|
33
|
+
- [GridGainChatMessageHistory](#3-gridgainchatmessagehistory)
|
|
34
|
+
- [GridGainCache](#4-gridgaincache)
|
|
35
|
+
- [GridGainSemanticCache](#5-gridgainsemanticcache)
|
|
36
|
+
- [GridGainVectorStore](#6-gridgainvectorstore)
|
|
37
|
+
5. [Documentation](#documentation)
|
|
38
|
+
6. [Example](#example)
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
This library implements five key LangChain interfaces for GridGain:
|
|
43
|
+
|
|
44
|
+
1. **GridGainStore**: A key-value store implementation.
|
|
45
|
+
2. **GridGainDocumentLoader**: A document loader for retrieving documents from GridGain caches.
|
|
46
|
+
3. **GridGainChatMessageHistory**: A chat message history store using GridGain.
|
|
47
|
+
4. **GridGainCache**: A caching mechanism for Language Models using GridGain.
|
|
48
|
+
5. **GridGainSemanticCache**: A semantic caching mechanism for Language Models using GridGain.
|
|
49
|
+
6. **GridGainVectorStore**: A vector store implementation using GridGain for storing and querying embeddings.
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
## Prerequisites
|
|
53
|
+
|
|
54
|
+
1. Python 3.11.7
|
|
55
|
+
* You can use `pyenv` to manage multiple Python versions (optional):
|
|
56
|
+
1. Install `pyenv`: `brew install pyenv` (or your system's package manager)
|
|
57
|
+
2. Create and activate the environment:
|
|
58
|
+
```bash
|
|
59
|
+
pyenv virtualenv 3.11.7 langchain-env
|
|
60
|
+
source $HOME/.pyenv/versions/langchain-env/bin/activate
|
|
61
|
+
```
|
|
62
|
+
* Alternatively, ensure Python 3.11.7 is installed directly.
|
|
63
|
+
|
|
64
|
+
2. A running GridGain Enterprise or Ultimate Edition, at least 8.9.17 ([release notes](https://www.gridgain.com/docs/latest/release-notes/8.9.17/release-notes_8.9.17))
|
|
65
|
+
- Make sure your license includes access to the vector search feature.
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
Install the package using pip:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install langchain-gridgain
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## GridGain Setup
|
|
76
|
+
|
|
77
|
+
In order to use [GridGain](https://www.gridgain.com/) powered langchain components, you need to have a running GridGain cluster with vector search enabled.
|
|
78
|
+
|
|
79
|
+
### 1. Connecting to Gridgain
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from pygridgain import Client
|
|
83
|
+
|
|
84
|
+
def connect_to_gridgain(host: str, port: int) -> Client:
|
|
85
|
+
try:
|
|
86
|
+
client = Client()
|
|
87
|
+
client.connect(host, port)
|
|
88
|
+
print("Connected to Ignite successfully.")
|
|
89
|
+
return client
|
|
90
|
+
except Exception as e:
|
|
91
|
+
print(f"Failed to connect to Ignite: {e}")
|
|
92
|
+
raise
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Usage:
|
|
96
|
+
```python
|
|
97
|
+
client = connect_to_gridgain("localhost", 10800)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Detailed Component Explanations
|
|
101
|
+
|
|
102
|
+
### 1. GridGainStore
|
|
103
|
+
|
|
104
|
+
GridGainStore is a key-value store implementation that uses GridGain as its backend. It provides a simple and efficient way to store and retrieve data using key-value pairs.
|
|
105
|
+
|
|
106
|
+
Usage example:
|
|
107
|
+
```python
|
|
108
|
+
from langchain_gridgain.storage import GridGainStore
|
|
109
|
+
|
|
110
|
+
def initialize_keyvalue_store(client) -> GridGainStore:
|
|
111
|
+
try:
|
|
112
|
+
key_value_store = GridGainStore(
|
|
113
|
+
cache_name="laptop_specs",
|
|
114
|
+
client=client
|
|
115
|
+
)
|
|
116
|
+
print("GridGainStore initialized successfully.")
|
|
117
|
+
return key_value_store
|
|
118
|
+
except Exception as e:
|
|
119
|
+
print(f"Failed to initialize GridGainStore: {e}")
|
|
120
|
+
raise
|
|
121
|
+
|
|
122
|
+
# Usage
|
|
123
|
+
client = connect_to_ignite("localhost", 10800)
|
|
124
|
+
key_value_store = initialize_keyvalue_store(client)
|
|
125
|
+
|
|
126
|
+
# Store a value
|
|
127
|
+
key_value_store.mset([("laptop1", "16GB RAM, NVIDIA RTX 3060, Intel i7 11th Gen")])
|
|
128
|
+
|
|
129
|
+
# Retrieve a value
|
|
130
|
+
specs = key_value_store.mget(["laptop1"])[0]
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 2. GridGainDocumentLoader
|
|
134
|
+
|
|
135
|
+
GridGainDocumentLoader is designed to load documents from GridGain caches. It's particularly useful for scenarios where you need to retrieve and process large amounts of textual data stored in GridGain.
|
|
136
|
+
|
|
137
|
+
Usage example:
|
|
138
|
+
```python
|
|
139
|
+
from langchain_gridgain.document_loaders import GridGainDocumentLoader
|
|
140
|
+
|
|
141
|
+
def initialize_doc_loader(client) -> GridGainDocumentLoader:
|
|
142
|
+
try:
|
|
143
|
+
doc_loader = GridGainDocumentLoader(
|
|
144
|
+
cache_name="review_cache",
|
|
145
|
+
client=client,
|
|
146
|
+
create_cache_if_not_exists=True
|
|
147
|
+
)
|
|
148
|
+
print("GridGainDocumentLoader initialized successfully.")
|
|
149
|
+
return doc_loader
|
|
150
|
+
except Exception as e:
|
|
151
|
+
print(f"Failed to initialize GridGainDocumentLoader: {e}")
|
|
152
|
+
raise
|
|
153
|
+
|
|
154
|
+
# Usage
|
|
155
|
+
client = connect_to_ignite("localhost", 10800)
|
|
156
|
+
doc_loader = initialize_doc_loader(client)
|
|
157
|
+
|
|
158
|
+
# Populate the cache
|
|
159
|
+
reviews = {
|
|
160
|
+
"laptop1": "Great performance for coding and video editing. The 16GB RAM and dedicated GPU make multitasking a breeze."
|
|
161
|
+
}
|
|
162
|
+
doc_loader.populate_cache(reviews)
|
|
163
|
+
|
|
164
|
+
# Load documents
|
|
165
|
+
documents = doc_loader.load()
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### 3. GridGainChatMessageHistory
|
|
169
|
+
|
|
170
|
+
GridGainChatMessageHistory provides a way to store and retrieve chat message history using GridGain. This is crucial for maintaining context in conversational AI applications.
|
|
171
|
+
|
|
172
|
+
Usage example:
|
|
173
|
+
```python
|
|
174
|
+
from langchain_gridgain.chat_message_histories import GridGainChatMessageHistory
|
|
175
|
+
|
|
176
|
+
def initialize_chathistory_store(client) -> GridGainChatMessageHistory:
|
|
177
|
+
try:
|
|
178
|
+
chat_history = GridGainChatMessageHistory(
|
|
179
|
+
session_id="user_session",
|
|
180
|
+
cache_name="chat_history",
|
|
181
|
+
client=client
|
|
182
|
+
)
|
|
183
|
+
print("GridGainChatMessageHistory initialized successfully.")
|
|
184
|
+
return chat_history
|
|
185
|
+
except Exception as e:
|
|
186
|
+
print(f"Failed to initialize GridGainChatMessageHistory: {e}")
|
|
187
|
+
raise
|
|
188
|
+
|
|
189
|
+
# Usage
|
|
190
|
+
client = connect_to_ignite("localhost", 10800)
|
|
191
|
+
chat_history = initialize_chathistory_store(client)
|
|
192
|
+
|
|
193
|
+
# Add a message to the history
|
|
194
|
+
chat_history.add_user_message("Hello, I need help choosing a laptop.")
|
|
195
|
+
|
|
196
|
+
# Retrieve the conversation history
|
|
197
|
+
messages = chat_history.messages
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### 4. GridGainCache
|
|
201
|
+
|
|
202
|
+
GridGainCache provides a caching mechanism for the responses received from LLMs using GridGain. This can significantly improve response times for **exact** queries by storing and retrieving pre-computed results.
|
|
203
|
+
|
|
204
|
+
Usage example:
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
from langchain_gridgain.llm_cache import GridGainCache
|
|
208
|
+
|
|
209
|
+
def initialize_llm_cache(client)-> GridGainCache:
|
|
210
|
+
try:
|
|
211
|
+
llm_cache = GridGainCache(
|
|
212
|
+
cache_name="llm_cache",
|
|
213
|
+
client=client
|
|
214
|
+
)
|
|
215
|
+
logger.info("GridGainCache initialized successfully.")
|
|
216
|
+
return llm_cache
|
|
217
|
+
except Exception as e:
|
|
218
|
+
logger.error(f"Failed to initialize GridGainCache: {e}")
|
|
219
|
+
raise
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### 5. GridGainSemanticCache
|
|
223
|
+
|
|
224
|
+
GridGainSemanticCache provides a semantic caching mechanism for the responses received from LLMs using GridGain. This can significantly improve response times for **similar** queries by storing and retrieving pre-computed results.
|
|
225
|
+
|
|
226
|
+
Usage example:
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
from langchain_gridgain.llm_cache import GridGainCache
|
|
230
|
+
from langchain_gridgain.llm_cache import GridGainSemanticCache
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def initialize_semantic_llm_cache(client, embedding)-> GridGainSemanticCache:
|
|
234
|
+
try:
|
|
235
|
+
llm_cache = GridGainCache(
|
|
236
|
+
cache_name="llm_cache",
|
|
237
|
+
client=client
|
|
238
|
+
)
|
|
239
|
+
semantic_cache = GridGainSemanticCache(
|
|
240
|
+
llm_cache=llm_cache,
|
|
241
|
+
cache_name="semantic_llm_cache",
|
|
242
|
+
client=client,
|
|
243
|
+
embedding=embedding,
|
|
244
|
+
similarity_threshold=0.85
|
|
245
|
+
)
|
|
246
|
+
logger.info("GridGainSemanticCache initialized successfully.")
|
|
247
|
+
return semantic_cache
|
|
248
|
+
except Exception as e:
|
|
249
|
+
logger.error(f"Failed to initialize GridGainSemanticCache: {e}")
|
|
250
|
+
raise
|
|
251
|
+
|
|
252
|
+
### 6. GridGainVectorStore
|
|
253
|
+
|
|
254
|
+
GridGainVectorStore is a vector store implementation using GridGain for storing and querying embeddings. It allows efficient similarity search operations on high-dimensional vector data.
|
|
255
|
+
|
|
256
|
+
Usage example:
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
from langchain_gridgain.vectorstores import GridGainVectorStore
|
|
260
|
+
|
|
261
|
+
# Initialize GridGainVectorStore
|
|
262
|
+
def initialize_vector_store(client, embedding_model)-> GridGainVectorStore:
|
|
263
|
+
try:
|
|
264
|
+
vector_store = GridGainVectorStore(
|
|
265
|
+
cache_name="vector_cache",
|
|
266
|
+
embedding=embedding_model,
|
|
267
|
+
client=client
|
|
268
|
+
)
|
|
269
|
+
logger.info("GridGainVectorStore initialized successfully.")
|
|
270
|
+
return vector_store
|
|
271
|
+
except Exception as e:
|
|
272
|
+
logger.error(f"Failed to initialize GridGainVectorStore: {e}")
|
|
273
|
+
raise
|
|
274
|
+
|
|
275
|
+
# Add texts to the vector store
|
|
276
|
+
texts = [
|
|
277
|
+
"The latest MacBook Pro offers exceptional performance for video editing.",
|
|
278
|
+
"Dell XPS 15 is a powerful Windows laptop suitable for creative professionals.",
|
|
279
|
+
"ASUS ROG Zephyrus G14 provides a balance of portability and gaming performance."
|
|
280
|
+
]
|
|
281
|
+
metadatas = [{"id": "tech_review_1"}, {"id": "tech_review_2"}, {"id": "tech_review_3"}]
|
|
282
|
+
|
|
283
|
+
vector_store.add_texts(texts=texts, metadatas=metadatas)
|
|
284
|
+
|
|
285
|
+
# Perform similarity search
|
|
286
|
+
query = "What's a good laptop for video editing?"
|
|
287
|
+
results = vector_store.similarity_search(query, k=2)
|
|
288
|
+
|
|
289
|
+
for doc in results:
|
|
290
|
+
print(f"Content: {doc.page_content}")
|
|
291
|
+
print(f"Metadata: {doc.metadata}")
|
|
292
|
+
print("---")
|
|
293
|
+
|
|
294
|
+
# Clear the vector store
|
|
295
|
+
vector_store.clear()
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## Documentation
|
|
299
|
+
|
|
300
|
+
For an up-to-date documentation, see the [GridGain Docs](https://www.gridgain.com/docs/extensions/vector/langchain).
|
|
301
|
+
|
|
302
|
+
## Example
|
|
303
|
+
|
|
304
|
+
For a comprehensive, real-world example of how to use this package, please refer to the following GitHub repository:
|
|
305
|
+
|
|
306
|
+
[GG Langchain Demo](https://github.com/GridGain-Demos/gg8_langchain_demo)
|
|
307
|
+
|
|
308
|
+
gg8_langchain_demo is a demonstration project that showcases the integration of GridGain/Apache Ignite with LangChain, using the custom langchain-gridgain package. This project provides examples of how to use GridGain as a backend for various LangChain components, focusing on a laptop recommendation system.
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# langchain-gridgain
|
|
2
|
+
|
|
3
|
+
langchain-gridgain is a Python library that provides seamless integration between GridGain/Apache Ignite and LangChain. This library offers a set of storage adapters that allow LangChain components to efficiently use GridGain as a backend for various data storage needs.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
1. [Features](#features)
|
|
7
|
+
2. [Prerequisites](#prerequisites)
|
|
8
|
+
3. [Installation](#installation)
|
|
9
|
+
4. [GridGain Setup](#gridgain-setup)
|
|
10
|
+
- [Connecting to GridGain](#1-connecting-to-gridgain)
|
|
11
|
+
5. [Detailed Component Explanations](#detailed-component-explanations)
|
|
12
|
+
- [GridGainStore](#1-gridgainstore)
|
|
13
|
+
- [GridGainDocumentLoader](#2-gridgaindocumentloader)
|
|
14
|
+
- [GridGainChatMessageHistory](#3-gridgainchatmessagehistory)
|
|
15
|
+
- [GridGainCache](#4-gridgaincache)
|
|
16
|
+
- [GridGainSemanticCache](#5-gridgainsemanticcache)
|
|
17
|
+
- [GridGainVectorStore](#6-gridgainvectorstore)
|
|
18
|
+
5. [Documentation](#documentation)
|
|
19
|
+
6. [Example](#example)
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
This library implements five key LangChain interfaces for GridGain:
|
|
24
|
+
|
|
25
|
+
1. **GridGainStore**: A key-value store implementation.
|
|
26
|
+
2. **GridGainDocumentLoader**: A document loader for retrieving documents from GridGain caches.
|
|
27
|
+
3. **GridGainChatMessageHistory**: A chat message history store using GridGain.
|
|
28
|
+
4. **GridGainCache**: A caching mechanism for Language Models using GridGain.
|
|
29
|
+
5. **GridGainSemanticCache**: A semantic caching mechanism for Language Models using GridGain.
|
|
30
|
+
6. **GridGainVectorStore**: A vector store implementation using GridGain for storing and querying embeddings.
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
## Prerequisites
|
|
34
|
+
|
|
35
|
+
1. Python 3.11.7
|
|
36
|
+
* You can use `pyenv` to manage multiple Python versions (optional):
|
|
37
|
+
1. Install `pyenv`: `brew install pyenv` (or your system's package manager)
|
|
38
|
+
2. Create and activate the environment:
|
|
39
|
+
```bash
|
|
40
|
+
pyenv virtualenv 3.11.7 langchain-env
|
|
41
|
+
source $HOME/.pyenv/versions/langchain-env/bin/activate
|
|
42
|
+
```
|
|
43
|
+
* Alternatively, ensure Python 3.11.7 is installed directly.
|
|
44
|
+
|
|
45
|
+
2. A running GridGain Enterprise or Ultimate Edition, at least 8.9.17 ([release notes](https://www.gridgain.com/docs/latest/release-notes/8.9.17/release-notes_8.9.17))
|
|
46
|
+
- Make sure your license includes access to the vector search feature.
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
Install the package using pip:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install langchain-gridgain
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## GridGain Setup
|
|
57
|
+
|
|
58
|
+
In order to use [GridGain](https://www.gridgain.com/) powered langchain components, you need to have a running GridGain cluster with vector search enabled.
|
|
59
|
+
|
|
60
|
+
### 1. Connecting to Gridgain
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from pygridgain import Client
|
|
64
|
+
|
|
65
|
+
def connect_to_gridgain(host: str, port: int) -> Client:
|
|
66
|
+
try:
|
|
67
|
+
client = Client()
|
|
68
|
+
client.connect(host, port)
|
|
69
|
+
print("Connected to Ignite successfully.")
|
|
70
|
+
return client
|
|
71
|
+
except Exception as e:
|
|
72
|
+
print(f"Failed to connect to Ignite: {e}")
|
|
73
|
+
raise
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Usage:
|
|
77
|
+
```python
|
|
78
|
+
client = connect_to_gridgain("localhost", 10800)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Detailed Component Explanations
|
|
82
|
+
|
|
83
|
+
### 1. GridGainStore
|
|
84
|
+
|
|
85
|
+
GridGainStore is a key-value store implementation that uses GridGain as its backend. It provides a simple and efficient way to store and retrieve data using key-value pairs.
|
|
86
|
+
|
|
87
|
+
Usage example:
|
|
88
|
+
```python
|
|
89
|
+
from langchain_gridgain.storage import GridGainStore
|
|
90
|
+
|
|
91
|
+
def initialize_keyvalue_store(client) -> GridGainStore:
|
|
92
|
+
try:
|
|
93
|
+
key_value_store = GridGainStore(
|
|
94
|
+
cache_name="laptop_specs",
|
|
95
|
+
client=client
|
|
96
|
+
)
|
|
97
|
+
print("GridGainStore initialized successfully.")
|
|
98
|
+
return key_value_store
|
|
99
|
+
except Exception as e:
|
|
100
|
+
print(f"Failed to initialize GridGainStore: {e}")
|
|
101
|
+
raise
|
|
102
|
+
|
|
103
|
+
# Usage
|
|
104
|
+
client = connect_to_ignite("localhost", 10800)
|
|
105
|
+
key_value_store = initialize_keyvalue_store(client)
|
|
106
|
+
|
|
107
|
+
# Store a value
|
|
108
|
+
key_value_store.mset([("laptop1", "16GB RAM, NVIDIA RTX 3060, Intel i7 11th Gen")])
|
|
109
|
+
|
|
110
|
+
# Retrieve a value
|
|
111
|
+
specs = key_value_store.mget(["laptop1"])[0]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 2. GridGainDocumentLoader
|
|
115
|
+
|
|
116
|
+
GridGainDocumentLoader is designed to load documents from GridGain caches. It's particularly useful for scenarios where you need to retrieve and process large amounts of textual data stored in GridGain.
|
|
117
|
+
|
|
118
|
+
Usage example:
|
|
119
|
+
```python
|
|
120
|
+
from langchain_gridgain.document_loaders import GridGainDocumentLoader
|
|
121
|
+
|
|
122
|
+
def initialize_doc_loader(client) -> GridGainDocumentLoader:
|
|
123
|
+
try:
|
|
124
|
+
doc_loader = GridGainDocumentLoader(
|
|
125
|
+
cache_name="review_cache",
|
|
126
|
+
client=client,
|
|
127
|
+
create_cache_if_not_exists=True
|
|
128
|
+
)
|
|
129
|
+
print("GridGainDocumentLoader initialized successfully.")
|
|
130
|
+
return doc_loader
|
|
131
|
+
except Exception as e:
|
|
132
|
+
print(f"Failed to initialize GridGainDocumentLoader: {e}")
|
|
133
|
+
raise
|
|
134
|
+
|
|
135
|
+
# Usage
|
|
136
|
+
client = connect_to_ignite("localhost", 10800)
|
|
137
|
+
doc_loader = initialize_doc_loader(client)
|
|
138
|
+
|
|
139
|
+
# Populate the cache
|
|
140
|
+
reviews = {
|
|
141
|
+
"laptop1": "Great performance for coding and video editing. The 16GB RAM and dedicated GPU make multitasking a breeze."
|
|
142
|
+
}
|
|
143
|
+
doc_loader.populate_cache(reviews)
|
|
144
|
+
|
|
145
|
+
# Load documents
|
|
146
|
+
documents = doc_loader.load()
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 3. GridGainChatMessageHistory
|
|
150
|
+
|
|
151
|
+
GridGainChatMessageHistory provides a way to store and retrieve chat message history using GridGain. This is crucial for maintaining context in conversational AI applications.
|
|
152
|
+
|
|
153
|
+
Usage example:
|
|
154
|
+
```python
|
|
155
|
+
from langchain_gridgain.chat_message_histories import GridGainChatMessageHistory
|
|
156
|
+
|
|
157
|
+
def initialize_chathistory_store(client) -> GridGainChatMessageHistory:
|
|
158
|
+
try:
|
|
159
|
+
chat_history = GridGainChatMessageHistory(
|
|
160
|
+
session_id="user_session",
|
|
161
|
+
cache_name="chat_history",
|
|
162
|
+
client=client
|
|
163
|
+
)
|
|
164
|
+
print("GridGainChatMessageHistory initialized successfully.")
|
|
165
|
+
return chat_history
|
|
166
|
+
except Exception as e:
|
|
167
|
+
print(f"Failed to initialize GridGainChatMessageHistory: {e}")
|
|
168
|
+
raise
|
|
169
|
+
|
|
170
|
+
# Usage
|
|
171
|
+
client = connect_to_ignite("localhost", 10800)
|
|
172
|
+
chat_history = initialize_chathistory_store(client)
|
|
173
|
+
|
|
174
|
+
# Add a message to the history
|
|
175
|
+
chat_history.add_user_message("Hello, I need help choosing a laptop.")
|
|
176
|
+
|
|
177
|
+
# Retrieve the conversation history
|
|
178
|
+
messages = chat_history.messages
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 4. GridGainCache
|
|
182
|
+
|
|
183
|
+
GridGainCache provides a caching mechanism for the responses received from LLMs using GridGain. This can significantly improve response times for **exact** queries by storing and retrieving pre-computed results.
|
|
184
|
+
|
|
185
|
+
Usage example:
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from langchain_gridgain.llm_cache import GridGainCache
|
|
189
|
+
|
|
190
|
+
def initialize_llm_cache(client)-> GridGainCache:
|
|
191
|
+
try:
|
|
192
|
+
llm_cache = GridGainCache(
|
|
193
|
+
cache_name="llm_cache",
|
|
194
|
+
client=client
|
|
195
|
+
)
|
|
196
|
+
logger.info("GridGainCache initialized successfully.")
|
|
197
|
+
return llm_cache
|
|
198
|
+
except Exception as e:
|
|
199
|
+
logger.error(f"Failed to initialize GridGainCache: {e}")
|
|
200
|
+
raise
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### 5. GridGainSemanticCache
|
|
204
|
+
|
|
205
|
+
GridGainSemanticCache provides a semantic caching mechanism for the responses received from LLMs using GridGain. This can significantly improve response times for **similar** queries by storing and retrieving pre-computed results.
|
|
206
|
+
|
|
207
|
+
Usage example:
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
from langchain_gridgain.llm_cache import GridGainCache
|
|
211
|
+
from langchain_gridgain.llm_cache import GridGainSemanticCache
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def initialize_semantic_llm_cache(client, embedding)-> GridGainSemanticCache:
|
|
215
|
+
try:
|
|
216
|
+
llm_cache = GridGainCache(
|
|
217
|
+
cache_name="llm_cache",
|
|
218
|
+
client=client
|
|
219
|
+
)
|
|
220
|
+
semantic_cache = GridGainSemanticCache(
|
|
221
|
+
llm_cache=llm_cache,
|
|
222
|
+
cache_name="semantic_llm_cache",
|
|
223
|
+
client=client,
|
|
224
|
+
embedding=embedding,
|
|
225
|
+
similarity_threshold=0.85
|
|
226
|
+
)
|
|
227
|
+
logger.info("GridGainSemanticCache initialized successfully.")
|
|
228
|
+
return semantic_cache
|
|
229
|
+
except Exception as e:
|
|
230
|
+
logger.error(f"Failed to initialize GridGainSemanticCache: {e}")
|
|
231
|
+
raise
|
|
232
|
+
|
|
233
|
+
### 6. GridGainVectorStore
|
|
234
|
+
|
|
235
|
+
GridGainVectorStore is a vector store implementation using GridGain for storing and querying embeddings. It allows efficient similarity search operations on high-dimensional vector data.
|
|
236
|
+
|
|
237
|
+
Usage example:
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from langchain_gridgain.vectorstores import GridGainVectorStore
|
|
241
|
+
|
|
242
|
+
# Initialize GridGainVectorStore
|
|
243
|
+
def initialize_vector_store(client, embedding_model)-> GridGainVectorStore:
|
|
244
|
+
try:
|
|
245
|
+
vector_store = GridGainVectorStore(
|
|
246
|
+
cache_name="vector_cache",
|
|
247
|
+
embedding=embedding_model,
|
|
248
|
+
client=client
|
|
249
|
+
)
|
|
250
|
+
logger.info("GridGainVectorStore initialized successfully.")
|
|
251
|
+
return vector_store
|
|
252
|
+
except Exception as e:
|
|
253
|
+
logger.error(f"Failed to initialize GridGainVectorStore: {e}")
|
|
254
|
+
raise
|
|
255
|
+
|
|
256
|
+
# Add texts to the vector store
|
|
257
|
+
texts = [
|
|
258
|
+
"The latest MacBook Pro offers exceptional performance for video editing.",
|
|
259
|
+
"Dell XPS 15 is a powerful Windows laptop suitable for creative professionals.",
|
|
260
|
+
"ASUS ROG Zephyrus G14 provides a balance of portability and gaming performance."
|
|
261
|
+
]
|
|
262
|
+
metadatas = [{"id": "tech_review_1"}, {"id": "tech_review_2"}, {"id": "tech_review_3"}]
|
|
263
|
+
|
|
264
|
+
vector_store.add_texts(texts=texts, metadatas=metadatas)
|
|
265
|
+
|
|
266
|
+
# Perform similarity search
|
|
267
|
+
query = "What's a good laptop for video editing?"
|
|
268
|
+
results = vector_store.similarity_search(query, k=2)
|
|
269
|
+
|
|
270
|
+
for doc in results:
|
|
271
|
+
print(f"Content: {doc.page_content}")
|
|
272
|
+
print(f"Metadata: {doc.metadata}")
|
|
273
|
+
print("---")
|
|
274
|
+
|
|
275
|
+
# Clear the vector store
|
|
276
|
+
vector_store.clear()
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## Documentation
|
|
280
|
+
|
|
281
|
+
For an up-to-date documentation, see the [GridGain Docs](https://www.gridgain.com/docs/extensions/vector/langchain).
|
|
282
|
+
|
|
283
|
+
## Example
|
|
284
|
+
|
|
285
|
+
For a comprehensive, real-world example of how to use this package, please refer to the following GitHub repository:
|
|
286
|
+
|
|
287
|
+
[GG Langchain Demo](https://github.com/GridGain-Demos/gg8_langchain_demo)
|
|
288
|
+
|
|
289
|
+
gg8_langchain_demo is a demonstration project that showcases the integration of GridGain/Apache Ignite with LangChain, using the custom langchain-gridgain package. This project provides examples of how to use GridGain as a backend for various LangChain components, focusing on a laptop recommendation system.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "langchain-gridgain"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
authors = [
|
|
9
|
+
{name = "Manini Puranik", email = "manini.puranik@gridgain.com"},
|
|
10
|
+
{name = "Aditi Sharma", email = "aditi.sharma@gridgain.com"}
|
|
11
|
+
]
|
|
12
|
+
description = "This package makes it possible to use Gridgain as a Vector Store, Document Loader, LLM Cache, Key Value Store, Chat Memory within langchain "
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
requires-python = ">=3.11.7"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"langchain == 0.3.10",
|
|
17
|
+
"langchain-community~=0.3.10",
|
|
18
|
+
"pandas == 2.2.2",
|
|
19
|
+
"numpy == 1.26.4",
|
|
20
|
+
"pyignite == 0.6.1",
|
|
21
|
+
"pygridgain == 1.5.0"
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
test = [
|
|
26
|
+
"pytest",
|
|
27
|
+
"pytest-asyncio",
|
|
28
|
+
]
|
|
29
|
+
coverage = [
|
|
30
|
+
"pytest-cov",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[tool.setuptools]
|
|
34
|
+
package-dir = {"" = "src"}
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["src"]
|
|
38
|
+
include = ["langchain_gridgain*"]
|
|
39
|
+
namespaces = true
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
testpaths = ["tests"]
|
|
43
|
+
python_files = "test_*.py"
|
|
44
|
+
asyncio_mode = "strict"
|
|
45
|
+
markers = [
|
|
46
|
+
"integration: marks tests as integration tests (deselect with '-m \"not integration\"')",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.asyncio]
|
|
50
|
+
default_fixture_loop_scope = "function"
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from langchain_gridgain.chat_message_histories import GridGainChatMessageHistory
|
|
2
|
+
from langchain_gridgain.document_loaders import GridGainDocumentLoader
|
|
3
|
+
from langchain_gridgain.llm_cache import GridGainCache
|
|
4
|
+
from langchain_gridgain.storage import GridGainStore
|
|
5
|
+
from langchain_gridgain.vectorstores import GridGainVectorStore
|
|
6
|
+
|
|
7
|
+
__version__ = '0.6.4'
|
|
Binary file
|
langchain_gridgain-1.0.0/src/langchain_gridgain/__pycache__/chat_message_histories.cpython-311.pyc
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|