A semantic and legal text chunker based on LLM analysis
Project description
๐ช LLM Chunker
LLM ๊ธฐ๋ฐ ์๋ฏธ๋ก ์ ํ ์คํธ ๋ถํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
๊ธ์ ์๊ฐ ์๋ ์๋ฏธ ๋จ์๋ก ๋ฌธ์๋ฅผ ๋ถํ ํฉ๋๋ค.
์ค์น โข ๋น ๋ฅธ ์์ โข ์์ โข API ๋ ํผ๋ฐ์ค โข English
โจ ์ LLM Chunker์ธ๊ฐ?
๊ธฐ์กด ์ฒญ์ปค๋ ๊ธ์ ์๋ ์ ๊ท์์ผ๋ก ํ ์คํธ๋ฅผ ๋ถํ ํด์, ๋ฌธ์ฅ ์ค๊ฐ์์ ์๋ฆฌ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. LLM Chunker๋ ๋งฅ๋ฝ์ ์ดํดํฉ๋๋คโ์์ค์ ๊ฐ์ ๋ณํ, ๋ฒ๋ฅ ๋ฌธ์์ ์กฐํญ ๊ฒฝ๊ณ, ํ์บ์คํธ์ ์ฃผ์ ์ ํ์ ๊ฐ์งํฉ๋๋ค.
| ๊ธฐ์กด ์ฒญํน | LLM Chunker |
|---|---|
| ๊ธ์ ์๋ก ๋ถํ | ์๋ฏธ ๋จ์๋ก ๋ถํ |
| ๋ฌธ์ฅ ์ค๊ฐ์์ ์๋ฆผ | ์์ ํ ๋ฌธ๋งฅ ๋ณด์กด |
| ์ผ๋ฅ ์ ์ธ ๋ฐฉ์ | ๋๋ฉ์ธ ๋ง์ถค ํ๋กฌํํธ |
๐ฆ ์ค์น
pip install llm-chunker
์๊ตฌ์ฌํญ:
- Python 3.8+
- OpenAI API ํค (๋๋ ๋ก์ปฌ LLM์ฉ Ollama)
๐ ๋น ๋ฅธ ์์
from llm_chunker import GenericChunker
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
chunker = GenericChunker()
chunks = chunker.split_text(your_text)
for i, chunk in enumerate(chunks):
print(f"[์ฒญํฌ {i+1}] {chunk[:100]}...")
๐ ์์
๋ชจ๋ธ ์ ํํ๊ธฐ
from llm_chunker import GenericChunker
from llm_chunker.analyzer import TransitionAnalyzer, create_openai_caller
from llm_chunker.prompts import get_default_prompt
# ๋ฐฉ๋ฒ 1: model ํ๋ผ๋ฏธํฐ๋ก ์ง์ ์ง์
analyzer = TransitionAnalyzer(
prompt_generator=get_default_prompt,
model="gpt-4o" # ๋๋ "gpt-5-nano", "gpt-3.5-turbo"
)
# ๋ฐฉ๋ฒ 2: ํฉํ ๋ฆฌ ํจ์ ์ฌ์ฉ
analyzer = TransitionAnalyzer(
prompt_generator=get_default_prompt,
llm_caller=create_openai_caller("gpt-4o-mini")
)
chunker = GenericChunker(analyzer=analyzer)
๋ฒ๋ฅ ๋ฌธ์ ์ฒญํน
from llm_chunker import GenericChunker
from llm_chunker.analyzer import TransitionAnalyzer
from llm_chunker.prompts import get_legal_prompt
analyzer = TransitionAnalyzer(
prompt_generator=get_legal_prompt,
model="gpt-4o"
)
chunker = GenericChunker(
analyzer=analyzer,
significance_threshold=6, # ๋ฎ์์๋ก ๋ ๋ง์ด ๋ถํ
min_chunk_gap=500 # ์ฒญํฌ ๊ฐ ์ต์ ๊ฑฐ๋ฆฌ (๊ธ์์)
)
chunks = chunker.split_text(legal_document)
๋ก์ปฌ LLM ์ฌ์ฉ (Ollama)
from llm_chunker import GenericChunker
from llm_chunker.analyzer import TransitionAnalyzer, create_ollama_caller
analyzer = TransitionAnalyzer(
prompt_generator=get_default_prompt,
llm_caller=create_ollama_caller("llama3") # ๋๋ "mistral", "codellama"
)
chunker = GenericChunker(analyzer=analyzer)
์ปค์คํ ํ๋กฌํํธ (PromptBuilder)
PromptBuilder๋ฅผ ์ฌ์ฉํ๋ฉด ํจ์๋ฅผ ์ง์ ์์ฑํ์ง ์๊ณ ๋ ์ปค์คํ
ํ๋กฌํํธ๋ฅผ ์ฝ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค:
from llm_chunker import GenericChunker, TransitionAnalyzer, PromptBuilder
# ๋ฐฉ๋ฒ 1: ๋ฏธ๋ฆฌ ๋ง๋ค์ด์ง ํ๋ฆฌ์
์ฌ์ฉ
prompt = PromptBuilder.podcast(language="ko")
chunker = GenericChunker(analyzer=TransitionAnalyzer(prompt_generator=prompt))
# ๋ฐฉ๋ฒ 2: ์ปค์คํ
์ต์
์ผ๋ก ์์ฑ
prompt = PromptBuilder.create(
domain="novel", # podcast, novel, legal, news, meeting etc..
find="speaker changes", # topic changes, emotional shifts, scene changes
language="ko",
extra_fields=["speaker_name"]
)
์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ฆฌ์ :
| ๋ฉ์๋ | ์ฉ๋ |
|---|---|
PromptBuilder.podcast() |
ํ์บ์คํธ ์ฃผ์ ๋ณ๊ฒฝ |
PromptBuilder.novel_speaker() |
์์ค ํ์ ๋ณ๊ฒฝ |
PromptBuilder.novel_scene() |
์์ค ์ฅ๋ฉด ์ ํ |
PromptBuilder.meeting() |
ํ์๋ก ์๊ฑด ๋ณ๊ฒฝ |
๐ API ๋ ํผ๋ฐ์ค
GenericChunker
| ํ๋ผ๋ฏธํฐ | ํ์ | ๊ธฐ๋ณธ๊ฐ | ์ค๋ช |
|---|---|---|---|
analyzer |
TransitionAnalyzer |
None |
์ปค์คํ ๋ถ์๊ธฐ |
significance_threshold |
int |
7 |
์ต์ ์ค์๋ ์ ์ (1-10) |
min_chunk_gap |
int |
200 |
๋ถํ ์ง์ ๊ฐ ์ต์ ๊ฑฐ๋ฆฌ |
max_chunk_size |
int |
5000 |
ํด๋ฐฑ ์ฒญํฌ ํฌ๊ธฐ |
verbose |
bool |
False |
์์ธ ๋ก๊ทธ ์ถ๋ ฅ |
TransitionAnalyzer
| ํ๋ผ๋ฏธํฐ | ํ์ | ๊ธฐ๋ณธ๊ฐ | ์ค๋ช |
|---|---|---|---|
prompt_generator |
Callable |
ํ์ | LLM ํ๋กฌํํธ ์์ฑ ํจ์ |
model |
str |
None |
OpenAI ๋ชจ๋ธ๋ช |
llm_caller |
Callable |
None |
์ปค์คํ LLM ํธ์ถ ํจ์ |
ํฉํ ๋ฆฌ ํจ์
# OpenAI
create_openai_caller(model="gpt-4o") -> Callable
# Ollama (๋ก์ปฌ)
create_ollama_caller(model="llama3") -> Callable
๐๏ธ ์๋ ์๋ฆฌ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๊ธด ํ
์คํธ ์
๋ ฅ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. ๋ถํ LLM ์ปจํ
์คํธ ํฌ๊ธฐ์ ๋ง๊ฒ ์๋์ฐ ๋ถํ (~2600์) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. ๋ถ์ LLM์ด ์ ํ์ ๊ฐ์ง โ
โ "์ฌ๊ธฐ์ ๊ธฐ์จ์์ ์ฌํ์ผ๋ก ๊ฐ์ ์ด ๋ฐ๋" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. ํํฐ๋ง ๋ฎ์ ์ค์๋ & ์ค๋ณต ํฌ์ธํธ ์ ๊ฑฐ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. ์ฌ๋ผ์ด์ฑ ๊ฒ์ฆ๋ ์ ํ์ ์์ ํ
์คํธ ๋ถํ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
[์ฒญํฌ 1] [์ฒญํฌ 2] [์ฒญํฌ 3] ...
๐ ๋ผ์ด์ ์ค
MIT License - LICENSE ์ฐธ์กฐ
โญ Star History
๋ ๋์ RAG ํ์ดํ๋ผ์ธ์ ์ํด โค๏ธ
์ ์ฉํ์ จ๋ค๋ฉด โญ ์คํ๋ฅผ ๋๋ฌ์ฃผ์ธ์!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_chunker-0.1.2.tar.gz.
File metadata
- Download URL: llm_chunker-0.1.2.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
453a193e937d7db6415a04427726211449bff46428bfea5c4eaa75471f3da866
|
|
| MD5 |
3fe347be6c30cefccb8d15f89cd1abeb
|
|
| BLAKE2b-256 |
c8af1579db6b891ffb3b35440974336c06d7fc15fb8e5d22265f26debdbf68c6
|
File details
Details for the file llm_chunker-0.1.2-py3-none-any.whl.
File metadata
- Download URL: llm_chunker-0.1.2-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44df0a591b375d89030ecf9c4a4747705a73b93ef2a06ac7171cc1c61771cb86
|
|
| MD5 |
0a2fb3e7ed526738f0b31eebae4479e0
|
|
| BLAKE2b-256 |
5f57cf691e3851f66df2fb89d90532f8a149b75d7309902af7832232125c27b3
|