forked from toon-format/toon-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializer.py
More file actions
39 lines (28 loc) · 1.05 KB
/
serializer.py
File metadata and controls
39 lines (28 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from __future__ import annotations
from typing import Any, Sequence
from langchain_core.documents import Document
from langchain_core.output_parsers import BaseOutputParser
from .. import encode, decode
class ToonSerializer:
"""Convert LangChain Documents to TOON format (30–60% fewer tokens)."""
def transform_documents(
self, documents: Sequence[Document], **kwargs: Any
) -> list[Document]:
return [
Document(
page_content=encode(doc.page_content),
metadata={**doc.metadata, "format": "toon"}
)
for doc in documents
]
async def atransform_documents(
self, documents: Sequence[Document], **kwargs: Any
) -> list[Document]:
return self.transform_documents(documents, **kwargs)
class ToonOutputParser(BaseOutputParser):
"""Parse TOON responses from LLMs back to Python objects."""
def parse(self, text: str) -> Any:
return decode(text.strip())
@property
def _type(self) -> str:
return "toon"