latentmesh 0.5.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Yash Ranjith
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,245 @@
1
+ Metadata-Version: 2.4
2
+ Name: latentmesh
3
+ Version: 0.5.0
4
+ Summary: LangGraph-based multi-agent KV-cache communication protocol for LLMs
5
+ Author-email: Yash Ranjith <yranjith@stanford.edu>, Hiroki Kimiwada <kimiwada@stanford.edu>, William Peng <wgpeng@stanford.edu>, Atharva Rao <arao2007@stanford.edu>
6
+ License: MIT
7
+ Classifier: Development Status :: 4 - Beta
8
+ Classifier: Intended Audience :: Science/Research
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Requires-Python: >=3.9
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: torch
20
+ Requires-Dist: transformers
21
+ Requires-Dist: accelerate
22
+ Requires-Dist: langgraph
23
+ Requires-Dist: langchain-core
24
+ Requires-Dist: pygtrie
25
+ Requires-Dist: pydantic
26
+ Provides-Extra: server
27
+ Requires-Dist: fastapi; extra == "server"
28
+ Requires-Dist: uvicorn[standard]; extra == "server"
29
+ Provides-Extra: disk
30
+ Requires-Dist: diskcache; extra == "disk"
31
+ Provides-Extra: dev
32
+ Requires-Dist: pytest; extra == "dev"
33
+ Requires-Dist: datasets; extra == "dev"
34
+ Requires-Dist: matplotlib; extra == "dev"
35
+ Requires-Dist: scipy; extra == "dev"
36
+ Provides-Extra: all
37
+ Requires-Dist: latentmesh[dev,disk,server]; extra == "all"
38
+ Dynamic: license-file
39
+
40
+ <a id="readme-top"></a>
41
+
42
+ <!-- PROJECT SHIELDS -->
43
+ <div align="center">
44
+
45
+ [![Contributors][contributors-shield]][contributors-url]
46
+ [![Stargazers][stars-shield]][stars-url]
47
+ [![License][license-shield]][license-url]
48
+ [![Python][python-shield]][python-url]
49
+
50
+ </div>
51
+
52
+ <!-- PROJECT HEADER -->
53
+ <br />
54
+ <div align="center">
55
+ <h3 align="center">LatentMesh</h3>
56
+
57
+ <p align="center">
58
+ Multi-agent Latent Space Communication for LLMs
59
+ <br />
60
+ <br />
61
+ <a href="https://latentmesh.vercel.app" target="_blank"><strong>Explore the docs »</strong></a>
62
+ <br />
63
+ <br />
64
+ <a href="https://github.com/shayhacker/LatentMesh/issues/new?labels=bug">Report Bug</a>
65
+ ·
66
+ <a href="https://github.com/shayhacker/LatentMesh/issues/new?labels=enhancement">Request Feature</a>
67
+ </p>
68
+ </div>
69
+
70
+ <!-- TABLE OF CONTENTS -->
71
+ <details>
72
+ <summary>Table of Contents</summary>
73
+ <ol>
74
+ <li>
75
+ <a href="#about-the-project">About The Project</a>
76
+ </li>
77
+ <li>
78
+ <a href="#getting-started">Getting Started</a>
79
+ <ul>
80
+ <li><a href="#installation">Installation</a></li>
81
+ </ul>
82
+ </li>
83
+ <li><a href="#usage">Usage</a></li>
84
+ <li><a href="#examples">Examples</a></li>
85
+ <li><a href="#roadmap">Roadmap</a></li>
86
+ <li><a href="#license">License</a></li>
87
+ <li><a href="#contact">Contact</a></li>
88
+ </ol>
89
+ </details>
90
+
91
+ <!-- ABOUT THE PROJECT -->
92
+ ## About The Project
93
+
94
+ LatentMesh wires multiple LLM agents together in a [LangGraph](https://github.com/langchain-ai/langgraph) pipeline by strictly communicating via KV cache.
95
+
96
+ In standard architectures, when Agent B needs to read what Agent A computed, it must process exactly the same text history from scratch. With LatentMesh, Agent B can access the prior agent's KV cache states, cutting costs computation costs from $O(n^2)$ to $O(n)$. Our long term vision is to expand communication between any model regardless of differences in architecture.
97
+
98
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
99
+
100
+
101
+ <!-- GETTING STARTED -->
102
+ ## Getting Started
103
+
104
+ ### Installation
105
+
106
+ Clone the repository and install in editable mode
107
+
108
+ ```sh
109
+ git clone https://github.com/shayhacker/LatentMesh.git
110
+ cd LatentMesh
111
+ pip install -e .
112
+ ```
113
+
114
+ Alternatively, install via PIP
115
+ > not yet available
116
+
117
+ ```sh
118
+ pip install latentmesh
119
+ ```
120
+
121
+ **Optional Dependencies:**
122
+ * For persistent disk-backed caching (recommended for production):
123
+ ```sh
124
+ pip install latentmesh[disk]
125
+ ```
126
+ * For booting a ready-to-go FastAPI endpoint:
127
+ ```sh
128
+ pip install latentmesh[server]
129
+ ```
130
+
131
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
132
+
133
+ <!-- USAGE EXAMPLES -->
134
+ ## Usage
135
+
136
+ A basic multi-agent configuration building a `Plan -> Reason -> Review` text pipeline.
137
+
138
+ ```python
139
+ from langgraph.graph import StateGraph, START, END
140
+ from latentmesh import LatentLLM, LatentState
141
+ from latentmesh.primitives import PlanPrimitive, ReasonPrimitive, ReviewPrimitive
142
+ from latentmesh.persistent_cache import MemoryKVStore, GlobalPrefixCache
143
+
144
+ # 1. Provide the cache to skip re-encoding string prefixes
145
+ store = MemoryKVStore()
146
+ cache = GlobalPrefixCache(store)
147
+
148
+ # 2. Use a standard HF model
149
+ llm = LatentLLM("Qwen/Qwen3-0.6B", device="cuda", global_cache=cache)
150
+
151
+ # 3. Create discrete agent primitives
152
+ planner = PlanPrimitive(llm)
153
+ reasoner = ReasonPrimitive(llm)
154
+ reviewer = ReviewPrimitive(llm)
155
+
156
+ # 4. Connect via LangGraph
157
+ builder = StateGraph(LatentState)
158
+ builder.add_node("planner", planner)
159
+ builder.add_node("reasoner", reasoner)
160
+ builder.add_node("reviewer", reviewer)
161
+
162
+ builder.add_edge(START, "planner")
163
+ builder.add_edge("planner", "reasoner")
164
+ builder.add_edge("reasoner", "reviewer")
165
+ builder.add_edge("reviewer", END)
166
+
167
+ graph = builder.compile()
168
+
169
+ # 5. Run it forward sequentially
170
+ result = graph.invoke({
171
+ "messages": [{"role": "user", "content": "What is the cosine of 45 degrees?"}],
172
+ "tokens_so_far": 0,
173
+ })
174
+
175
+ print(result["latent"].text)
176
+ print(f"Total tokens generated: {result['tokens_so_far']}")
177
+ ```
178
+
179
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
180
+
181
+ <!-- EXAMPLES -->
182
+ ## Examples
183
+
184
+ For more involved architectures like consensus routing and dynamic state inspection, check out `examples/`:
185
+
186
+ | Example | Description |
187
+ |---|---|
188
+ | [`sequential.py`](examples/sequential.py) | Plan → Reason → Review zero-shot pipeline |
189
+ | [`complex.py`](examples/complex.py) | Fast consensus parallel-voting with `VotingPrimitive` |
190
+ | [`hierarchical.py`](examples/hierarchical.py) | Dynamic routing based on model confidence scoring |
191
+
192
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
193
+
194
+ <!-- ROADMAP -->
195
+ ## Roadmap
196
+
197
+ - [x] Implement HuggingFace model backend wrapping
198
+ - [x] Add global prefix matching via `pygtrie`
199
+ - [x] Create Memory and Disk KV Stores
200
+ - [ ] Multi-GPU parallelization for `VotingPrimitive`
201
+ - [ ] Implement LRU/TTL eviction policies for the global prefix cache
202
+ - [ ] Publish v1.0.0 to PyPI
203
+
204
+ See the [open issues](https://github.com/shayhacker/LatentMesh/issues) for a full list of proposed features (and known issues).
205
+
206
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
207
+
208
+ <!-- LICENSE -->
209
+ ## License
210
+
211
+ Distributed under the MIT License. See `LICENSE` for more information.
212
+
213
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
214
+
215
+ <!-- CONTACT -->
216
+ ## Contact
217
+
218
+ * Yash Ranjith - yranjith@stanford.edu
219
+ * Hiroki Kimiwada - kimiwada@stanford.edu
220
+ * William Peng - wgpeng@stanford.edu
221
+ * Atharva Rao - arao2007@stanford.edu
222
+
223
+ Project Link: [https://github.com/shayhacker/LatentMesh](https://github.com/shayhacker/LatentMesh)
224
+
225
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
226
+
227
+ <!-- MARKDOWN LINKS & IMAGES -->
228
+ [contributors-shield]: https://img.shields.io/github/contributors/shayhacker/LatentMesh.svg?style=for-the-badge
229
+ [contributors-url]: https://github.com/shayhacker/LatentMesh/graphs/contributors
230
+ [forks-shield]: https://img.shields.io/github/forks/shayhacker/LatentMesh.svg?style=for-the-badge
231
+ [forks-url]: https://github.com/shayhacker/LatentMesh/network/members
232
+ [stars-shield]: https://img.shields.io/github/stars/shayhacker/LatentMesh.svg?style=for-the-badge
233
+ [stars-url]: https://github.com/shayhacker/LatentMesh/stargazers
234
+ [license-shield]: https://img.shields.io/github/license/shayhacker/LatentMesh.svg?style=for-the-badge
235
+ [license-url]: https://github.com/shayhacker/LatentMesh/blob/main/LICENSE
236
+ [python-shield]: https://img.shields.io/badge/python-3.9+-blue.svg?style=for-the-badge&logo=python&logoColor=white
237
+ [python-url]: https://www.python.org/
238
+ [pytorch-shield]: https://img.shields.io/badge/PyTorch-EE4C2C?style=for-the-badge&logo=pytorch&logoColor=white
239
+ [pytorch-url]: https://pytorch.org/
240
+ [transformers-shield]: https://img.shields.io/badge/Transformers-FFCA28?style=for-the-badge&logo=huggingface&logoColor=black
241
+ [transformers-url]: https://huggingface.co/docs/transformers
242
+ [langgraph-shield]: https://img.shields.io/badge/LangGraph-1C3C3C?style=for-the-badge&logo=langchain&logoColor=white
243
+ [langgraph-url]: https://www.langchain.com/langgraph
244
+ [fastapi-shield]: https://img.shields.io/badge/FastAPI-009688?style=for-the-badge&logo=fastapi&logoColor=white
245
+ [fastapi-url]: https://fastapi.tiangolo.com/
@@ -0,0 +1,206 @@
1
+ <a id="readme-top"></a>
2
+
3
+ <!-- PROJECT SHIELDS -->
4
+ <div align="center">
5
+
6
+ [![Contributors][contributors-shield]][contributors-url]
7
+ [![Stargazers][stars-shield]][stars-url]
8
+ [![License][license-shield]][license-url]
9
+ [![Python][python-shield]][python-url]
10
+
11
+ </div>
12
+
13
+ <!-- PROJECT HEADER -->
14
+ <br />
15
+ <div align="center">
16
+ <h3 align="center">LatentMesh</h3>
17
+
18
+ <p align="center">
19
+ Multi-agent Latent Space Communication for LLMs
20
+ <br />
21
+ <br />
22
+ <a href="https://latentmesh.vercel.app" target="_blank"><strong>Explore the docs »</strong></a>
23
+ <br />
24
+ <br />
25
+ <a href="https://github.com/shayhacker/LatentMesh/issues/new?labels=bug">Report Bug</a>
26
+ ·
27
+ <a href="https://github.com/shayhacker/LatentMesh/issues/new?labels=enhancement">Request Feature</a>
28
+ </p>
29
+ </div>
30
+
31
+ <!-- TABLE OF CONTENTS -->
32
+ <details>
33
+ <summary>Table of Contents</summary>
34
+ <ol>
35
+ <li>
36
+ <a href="#about-the-project">About The Project</a>
37
+ </li>
38
+ <li>
39
+ <a href="#getting-started">Getting Started</a>
40
+ <ul>
41
+ <li><a href="#installation">Installation</a></li>
42
+ </ul>
43
+ </li>
44
+ <li><a href="#usage">Usage</a></li>
45
+ <li><a href="#examples">Examples</a></li>
46
+ <li><a href="#roadmap">Roadmap</a></li>
47
+ <li><a href="#license">License</a></li>
48
+ <li><a href="#contact">Contact</a></li>
49
+ </ol>
50
+ </details>
51
+
52
+ <!-- ABOUT THE PROJECT -->
53
+ ## About The Project
54
+
55
+ LatentMesh wires multiple LLM agents together in a [LangGraph](https://github.com/langchain-ai/langgraph) pipeline by strictly communicating via KV cache.
56
+
57
+ In standard architectures, when Agent B needs to read what Agent A computed, it must process exactly the same text history from scratch. With LatentMesh, Agent B can access the prior agent's KV cache states, cutting costs computation costs from $O(n^2)$ to $O(n)$. Our long term vision is to expand communication between any model regardless of differences in architecture.
58
+
59
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
60
+
61
+
62
+ <!-- GETTING STARTED -->
63
+ ## Getting Started
64
+
65
+ ### Installation
66
+
67
+ Clone the repository and install in editable mode
68
+
69
+ ```sh
70
+ git clone https://github.com/shayhacker/LatentMesh.git
71
+ cd LatentMesh
72
+ pip install -e .
73
+ ```
74
+
75
+ Alternatively, install via PIP
76
+ > not yet available
77
+
78
+ ```sh
79
+ pip install latentmesh
80
+ ```
81
+
82
+ **Optional Dependencies:**
83
+ * For persistent disk-backed caching (recommended for production):
84
+ ```sh
85
+ pip install latentmesh[disk]
86
+ ```
87
+ * For booting a ready-to-go FastAPI endpoint:
88
+ ```sh
89
+ pip install latentmesh[server]
90
+ ```
91
+
92
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
93
+
94
+ <!-- USAGE EXAMPLES -->
95
+ ## Usage
96
+
97
+ A basic multi-agent configuration building a `Plan -> Reason -> Review` text pipeline.
98
+
99
+ ```python
100
+ from langgraph.graph import StateGraph, START, END
101
+ from latentmesh import LatentLLM, LatentState
102
+ from latentmesh.primitives import PlanPrimitive, ReasonPrimitive, ReviewPrimitive
103
+ from latentmesh.persistent_cache import MemoryKVStore, GlobalPrefixCache
104
+
105
+ # 1. Provide the cache to skip re-encoding string prefixes
106
+ store = MemoryKVStore()
107
+ cache = GlobalPrefixCache(store)
108
+
109
+ # 2. Use a standard HF model
110
+ llm = LatentLLM("Qwen/Qwen3-0.6B", device="cuda", global_cache=cache)
111
+
112
+ # 3. Create discrete agent primitives
113
+ planner = PlanPrimitive(llm)
114
+ reasoner = ReasonPrimitive(llm)
115
+ reviewer = ReviewPrimitive(llm)
116
+
117
+ # 4. Connect via LangGraph
118
+ builder = StateGraph(LatentState)
119
+ builder.add_node("planner", planner)
120
+ builder.add_node("reasoner", reasoner)
121
+ builder.add_node("reviewer", reviewer)
122
+
123
+ builder.add_edge(START, "planner")
124
+ builder.add_edge("planner", "reasoner")
125
+ builder.add_edge("reasoner", "reviewer")
126
+ builder.add_edge("reviewer", END)
127
+
128
+ graph = builder.compile()
129
+
130
+ # 5. Run it forward sequentially
131
+ result = graph.invoke({
132
+ "messages": [{"role": "user", "content": "What is the cosine of 45 degrees?"}],
133
+ "tokens_so_far": 0,
134
+ })
135
+
136
+ print(result["latent"].text)
137
+ print(f"Total tokens generated: {result['tokens_so_far']}")
138
+ ```
139
+
140
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
141
+
142
+ <!-- EXAMPLES -->
143
+ ## Examples
144
+
145
+ For more involved architectures like consensus routing and dynamic state inspection, check out `examples/`:
146
+
147
+ | Example | Description |
148
+ |---|---|
149
+ | [`sequential.py`](examples/sequential.py) | Plan → Reason → Review zero-shot pipeline |
150
+ | [`complex.py`](examples/complex.py) | Fast consensus parallel-voting with `VotingPrimitive` |
151
+ | [`hierarchical.py`](examples/hierarchical.py) | Dynamic routing based on model confidence scoring |
152
+
153
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
154
+
155
+ <!-- ROADMAP -->
156
+ ## Roadmap
157
+
158
+ - [x] Implement HuggingFace model backend wrapping
159
+ - [x] Add global prefix matching via `pygtrie`
160
+ - [x] Create Memory and Disk KV Stores
161
+ - [ ] Multi-GPU parallelization for `VotingPrimitive`
162
+ - [ ] Implement LRU/TTL eviction policies for the global prefix cache
163
+ - [ ] Publish v1.0.0 to PyPI
164
+
165
+ See the [open issues](https://github.com/shayhacker/LatentMesh/issues) for a full list of proposed features (and known issues).
166
+
167
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
168
+
169
+ <!-- LICENSE -->
170
+ ## License
171
+
172
+ Distributed under the MIT License. See `LICENSE` for more information.
173
+
174
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
175
+
176
+ <!-- CONTACT -->
177
+ ## Contact
178
+
179
+ * Yash Ranjith - yranjith@stanford.edu
180
+ * Hiroki Kimiwada - kimiwada@stanford.edu
181
+ * William Peng - wgpeng@stanford.edu
182
+ * Atharva Rao - arao2007@stanford.edu
183
+
184
+ Project Link: [https://github.com/shayhacker/LatentMesh](https://github.com/shayhacker/LatentMesh)
185
+
186
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
187
+
188
+ <!-- MARKDOWN LINKS & IMAGES -->
189
+ [contributors-shield]: https://img.shields.io/github/contributors/shayhacker/LatentMesh.svg?style=for-the-badge
190
+ [contributors-url]: https://github.com/shayhacker/LatentMesh/graphs/contributors
191
+ [forks-shield]: https://img.shields.io/github/forks/shayhacker/LatentMesh.svg?style=for-the-badge
192
+ [forks-url]: https://github.com/shayhacker/LatentMesh/network/members
193
+ [stars-shield]: https://img.shields.io/github/stars/shayhacker/LatentMesh.svg?style=for-the-badge
194
+ [stars-url]: https://github.com/shayhacker/LatentMesh/stargazers
195
+ [license-shield]: https://img.shields.io/github/license/shayhacker/LatentMesh.svg?style=for-the-badge
196
+ [license-url]: https://github.com/shayhacker/LatentMesh/blob/main/LICENSE
197
+ [python-shield]: https://img.shields.io/badge/python-3.9+-blue.svg?style=for-the-badge&logo=python&logoColor=white
198
+ [python-url]: https://www.python.org/
199
+ [pytorch-shield]: https://img.shields.io/badge/PyTorch-EE4C2C?style=for-the-badge&logo=pytorch&logoColor=white
200
+ [pytorch-url]: https://pytorch.org/
201
+ [transformers-shield]: https://img.shields.io/badge/Transformers-FFCA28?style=for-the-badge&logo=huggingface&logoColor=black
202
+ [transformers-url]: https://huggingface.co/docs/transformers
203
+ [langgraph-shield]: https://img.shields.io/badge/LangGraph-1C3C3C?style=for-the-badge&logo=langchain&logoColor=white
204
+ [langgraph-url]: https://www.langchain.com/langgraph
205
+ [fastapi-shield]: https://img.shields.io/badge/FastAPI-009688?style=for-the-badge&logo=fastapi&logoColor=white
206
+ [fastapi-url]: https://fastapi.tiangolo.com/
@@ -0,0 +1,40 @@
1
+ """
2
+ LatentMesh — LangGraph-based multi-agent KV-cache communication protocol.
3
+
4
+ Agents share a ``GlobalPrefixCache`` so that downstream agents skip
5
+ re-encoding text that upstream agents already processed.
6
+ """
7
+
8
+ from latentmesh.core import (
9
+ AgentOutput,
10
+ LatentLLM,
11
+ extract_kv,
12
+ )
13
+
14
+ from latentmesh.graph import (
15
+ LatentState,
16
+ latent_reducer,
17
+ )
18
+
19
+ from latentmesh.primitives import (
20
+ AgentPrimitive,
21
+ PlanPrimitive,
22
+ ReasonPrimitive,
23
+ ReviewPrimitive,
24
+ VotingPrimitive,
25
+ )
26
+
27
+ __version__ = "0.5.0"
28
+
29
+ __all__ = [
30
+ "AgentOutput",
31
+ "LatentLLM",
32
+ "extract_kv",
33
+ "LatentState",
34
+ "latent_reducer",
35
+ "AgentPrimitive",
36
+ "PlanPrimitive",
37
+ "ReasonPrimitive",
38
+ "ReviewPrimitive",
39
+ "VotingPrimitive",
40
+ ]