-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_stores_demo.py
More file actions
374 lines (290 loc) · 10.7 KB
/
vector_stores_demo.py
File metadata and controls
374 lines (290 loc) · 10.7 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
Vector Stores Demo - Fluent API
beanllm 방식: 쉽고 강력한 벡터 스토어
"""
import asyncio
from beanllm import (
Document,
DocumentLoader,
Embedding,
TextSplitter,
VectorStore,
VectorStoreBuilder,
create_vector_store,
from_documents,
)
def demo_basic_usage():
"""기본 사용법"""
print("\n" + "=" * 60)
print("📦 기본 사용법")
print("=" * 60)
# 임베딩 함수 준비
try:
emb = Embedding(model="text-embedding-3-small")
embed_func = emb.embed_sync
print("\n✓ Using OpenAI embeddings")
except:
# API 키 없으면 더미
import random
embed_func = lambda texts: [[random.random() for _ in range(384)] for _ in texts]
print("\n⚠️ Using dummy embeddings (no API key)")
# 1. 가장 간단한 방법
print("\n1. 가장 간단한 방법:")
try:
store = VectorStore.chroma(embedding_function=embed_func)
docs = [
Document(content="AI is transforming the world"),
Document(content="Machine learning learns from data"),
Document(content="Deep learning uses neural networks"),
]
store.add_documents(docs)
results = store.similarity_search("artificial intelligence", k=2)
print(f" ✓ Found {len(results)} documents")
for i, result in enumerate(results[:2], 1):
print(f" {i}. {result.document.content[:50]}... (score: {result.score:.3f})")
except Exception as e:
print(f" ⚠️ {e}")
print("\n✓ 기본 사용법 완료!")
def demo_factory_methods():
"""팩토리 메서드"""
print("\n" + "=" * 60)
print("🏭 팩토리 메서드")
print("=" * 60)
import random
embed_func = lambda texts: [[random.random() for _ in range(384)] for _ in texts]
# Chroma
print("\n1. Chroma (로컬, 쉬움):")
try:
store = VectorStore.chroma(collection_name="demo_chroma", embedding_function=embed_func)
print(" ✓ Chroma store created")
except Exception as e:
print(f" ⚠️ {e}")
# FAISS
print("\n2. FAISS (로컬, 빠름):")
try:
store = VectorStore.faiss(dimension=384, embedding_function=embed_func)
print(" ✓ FAISS store created")
except Exception as e:
print(f" ⚠️ {e}")
print("\n✓ 팩토리 메서드 완료!")
def demo_fluent_api():
"""Fluent API"""
print("\n" + "=" * 60)
print("✨ Fluent API (Builder Pattern)")
print("=" * 60)
import random
embed_func = lambda texts: [[random.random() for _ in range(384)] for _ in texts]
print("\n1. Builder 패턴:")
try:
store = (
VectorStoreBuilder()
.use_chroma()
.with_embedding(embed_func)
.with_collection("fluent_demo")
.build()
)
print(" ✓ Store built with fluent API")
# 사용
docs = [Document(content="Fluent API is elegant")]
store.add_documents(docs)
results = store.similarity_search("elegant", k=1)
print(f" ✓ Found: {results[0].document.content}")
except Exception as e:
print(f" ⚠️ {e}")
print("\n✓ Fluent API 완료!")
def demo_convenience_functions():
"""편의 함수"""
print("\n" + "=" * 60)
print("⚡ 편의 함수")
print("=" * 60)
import random
embed_func = lambda texts: [[random.random() for _ in range(384)] for _ in texts]
# create_vector_store()
print("\n1. create_vector_store():")
try:
store = create_vector_store(
provider="chroma", embedding_function=embed_func, collection_name="convenience_demo"
)
print(" ✓ Store created")
except Exception as e:
print(f" ⚠️ {e}")
# from_documents()
print("\n2. from_documents() - 가장 편리!")
try:
docs = [
Document(content="Quick document 1"),
Document(content="Quick document 2"),
Document(content="Quick document 3"),
]
store = from_documents(
docs, embedding_function=embed_func, provider="chroma", collection_name="from_docs_demo"
)
print(f" ✓ Store created with {len(docs)} documents")
results = store.similarity_search("quick", k=2)
print(f" ✓ Search: {len(results)} results")
except Exception as e:
print(f" ⚠️ {e}")
print("\n✓ 편의 함수 완료!")
async def demo_full_rag_pipeline():
"""전체 RAG 파이프라인"""
print("\n" + "=" * 60)
print("🚀 전체 RAG 파이프라인")
print("=" * 60)
from pathlib import Path
# 테스트 파일 생성
test_file = Path("rag_demo.txt")
test_file.write_text(
"""
Artificial Intelligence is revolutionizing technology.
Machine learning algorithms learn patterns from data.
Deep learning uses multi-layer neural networks.
Natural language processing understands human language.
Computer vision enables machines to see and interpret images.
""".strip(),
encoding="utf-8",
)
try:
# 1. 문서 로딩
print("\n1. 문서 로딩:")
docs = DocumentLoader.load(test_file)
print(f" ✓ Loaded {len(docs)} document(s)")
# 2. 텍스트 분할
print("\n2. 텍스트 분할:")
chunks = TextSplitter.split(docs, chunk_size=100)
print(f" ✓ Split into {len(chunks)} chunks")
# 3. 임베딩 준비
print("\n3. 임베딩 준비:")
try:
from beanllm import embed_sync
embed_func = lambda texts: embed_sync(texts)
print(" ✓ Using OpenAI embeddings")
except:
import random
embed_func = lambda texts: [[random.random() for _ in range(384)] for _ in texts]
print(" ⚠️ Using dummy embeddings")
# 4. Vector Store 생성 및 문서 추가
print("\n4. Vector Store 생성:")
store = from_documents(
chunks, embedding_function=embed_func, provider="chroma", collection_name="rag_pipeline"
)
print(f" ✓ Created vector store with {len(chunks)} chunks")
# 5. 검색
print("\n5. 질의 검색:")
query = "What is machine learning?"
results = store.similarity_search(query, k=3)
print(f' Query: "{query}"')
print(f" Found {len(results)} relevant chunks:")
for i, result in enumerate(results, 1):
print(f" {i}. {result.document.content[:60]}...")
print(f" Score: {result.score:.3f}")
print("\n✓ 전체 RAG 파이프라인 완료!")
except Exception as e:
print(f" ⚠️ {e}")
import traceback
traceback.print_exc()
finally:
# 정리
if test_file.exists():
test_file.unlink()
async def demo_async_operations():
"""비동기 작업"""
print("\n" + "=" * 60)
print("⚡ 비동기 작업")
print("=" * 60)
import random
embed_func = lambda texts: [[random.random() for _ in range(384)] for _ in texts]
try:
# Store 생성
store = VectorStore.chroma(collection_name="async_demo", embedding_function=embed_func)
# 문서 추가 (동기)
docs = [
Document(content="Async document 1"),
Document(content="Async document 2"),
Document(content="Async document 3"),
]
store.add_documents(docs)
print("\n✓ Documents added")
# 비동기 검색
print("\n비동기 검색:")
results = await store.asimilarity_search("async", k=2)
print(f" ✓ Found {len(results)} results")
print("\n✓ 비동기 작업 완료!")
except Exception as e:
print(f" ⚠️ {e}")
def demo_provider_selection():
"""Provider 선택"""
print("\n" + "=" * 60)
print("🔍 Provider 선택")
print("=" * 60)
# 사용 가능한 provider 확인
print("\n1. 사용 가능한 providers:")
available = VectorStore.list_available_providers()
print(f" Available: {available}")
# 기본 provider
default = VectorStore.get_default_provider()
print(f" Default: {default}")
# 각 provider 특징
print("\n2. Provider 특징:")
print(" • Chroma: 로컬, 사용하기 쉬움, 빠른 시작")
print(" • FAISS: 로컬, 매우 빠름, 대용량 데이터")
print(" • Pinecone: 클라우드, 확장 가능, 프로덕션")
print(" • Qdrant: 클라우드/로컬, 모던, 필터링 강력")
print(" • Weaviate: 엔터프라이즈, GraphQL, 복잡한 쿼리")
print("\n✓ Provider 선택 완료!")
def demo_comparison():
"""LangChain vs beanllm 비교"""
print("\n" + "=" * 60)
print("📊 LangChain vs beanllm 비교")
print("=" * 60)
print("\n【 LangChain 방식 】")
print("""
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
# 여러 import 필요
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(
documents=docs,
embedding=embeddings,
persist_directory="./db"
)
""")
print("\n【 beanllm 방식 】")
print("""
from beanllm import from_documents, Embedding
# 간단하고 직관적
embed_func = Embedding.openai().embed_sync
store = from_documents(docs, embed_func, provider="chroma")
""")
print("\n✅ beanllm: 더 간단하고 직관적!")
print("✅ 통합 인터페이스로 provider 전환 쉬움")
print("✅ Fluent API로 가독성 향상")
async def main():
"""모든 데모 실행"""
print("=" * 60)
print("🎯 Vector Stores 데모")
print("=" * 60)
print("\nbeanllm의 철학:")
print(" 1. 통합 인터페이스 (모든 vector store 동일한 API)")
print(" 2. Fluent API (Builder 패턴)")
print(" 3. 편의 함수 (from_documents)")
print(" 4. RAG 파이프라인 완전 지원")
demo_basic_usage()
demo_factory_methods()
demo_fluent_api()
demo_convenience_functions()
await demo_full_rag_pipeline()
await demo_async_operations()
demo_provider_selection()
demo_comparison()
print("\n" + "=" * 60)
print("🎉 Vector Stores 완료!")
print("=" * 60)
print("\n✨ 주요 기능:")
print(" 1. VectorStore.chroma() # 팩토리 메서드")
print(" 2. from_documents(docs, embed_func) # 가장 편리")
print(" 3. VectorStoreBuilder().use_chroma() # Fluent API")
print(" 4. 완전한 RAG 파이프라인")
print(" 5. 5개 주요 provider 지원")
print("\n💡 Document → Chunks → Embeddings → Vector Store → Search!")
if __name__ == "__main__":
asyncio.run(main())