■ Chroma 클래스의 invoke 메소드를 사용해 텍스트로 이미지를 찾는 방법을 보여준다.
▶ main.py
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 |
import base64 import os from datetime import datetime from langchain_experimental.open_clip import OpenCLIPEmbeddings from langchain_chroma import Chroma def printMessage(message, *argumentTuple): timeStamp = datetime.now().strftime("[%H:%M:%S]") if argumentTuple: finalMessage = message % argumentTuple else: finalMessage = message print(f"{timeStamp} {finalMessage}") def saveImage(base64String, targetFilePath): if "," in base64String: base64String = base64String.split(',')[1] imageBytes = base64.b64decode(base64String) with open(targetFilePath, "wb") as bufferedWriter: bufferedWriter.write(imageBytes) printMessage("START GET IMAGE FILE PATH LIST") imageFilePathList = sorted([os.path.join("temp", imageFileName) for imageFileName in os.listdir("temp") if imageFileName.endswith(".jpg")]) printMessage("END GET IMAGE FILE PATH LIST") print() printMessage("START CREATE OPENCLIPEMBEDDINGS") openCLIPEmbeddings = OpenCLIPEmbeddings(model_name = "ViT-H-14-378-quickgelu", checkpoint = "dfn5b") printMessage("END CREATE OPENCLIPEMBEDDINGS") print() printMessage("START CREATE CHROMA") chroma = Chroma(collection_name = "multimodal", embedding_function = openCLIPEmbeddings) printMessage("END CREATE CHROMA") print() printMessage("START ADD IMAGE TO CHROMA") chroma.add_images(uris = imageFilePathList) printMessage("END ADD IMAGE TO CHROMA") print() printMessage("START CREATE VECTOR STORE RETRIEVER") vectorStoreRetriever = chroma.as_retriever(search_kwargs = {"k": 3}) printMessage("END CREATE VECTOR STORE RETRIEVER") print() printMessage("START INVOKER 1") documentList1 = vectorStoreRetriever.invoke("A Dog on the street") saveImage(documentList1[0].page_content, "result1.jpg") printMessage("END INVOKER 1") print() printMessage("START INVOKER 2") documentList2 = vectorStoreRetriever.invoke("Motorcycle with a man") saveImage(documentList2[0].page_content, "result2.jpg") printMessage("END INVOKER 2") print() |
▶ requirements.txt
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 |
aiohappyeyeballs==2.4.4 aiohttp==3.11.11 aiosignal==1.3.2 annotated-types==0.7.0 anyio==4.8.0 asgiref==3.8.1 attrs==24.3.0 backoff==2.2.1 bcrypt==4.2.1 build==1.2.2.post1 cachetools==5.5.0 certifi==2024.12.14 charset-normalizer==3.4.1 chroma-hnswlib==0.7.6 chromadb==0.5.23 click==8.1.8 colorama==0.4.6 coloredlogs==15.0.1 dataclasses-json==0.6.7 Deprecated==1.2.15 durationpy==0.9 fastapi==0.115.6 filelock==3.16.1 flatbuffers==24.12.23 frozenlist==1.5.0 fsspec==2024.12.0 ftfy==6.3.1 google-auth==2.37.0 googleapis-common-protos==1.66.0 greenlet==3.1.1 grpcio==1.69.0 h11==0.14.0 httpcore==1.0.7 httptools==0.6.4 httpx==0.28.1 httpx-sse==0.4.0 huggingface-hub==0.27.1 humanfriendly==10.0 idna==3.10 importlib_metadata==8.5.0 importlib_resources==6.5.2 Jinja2==3.1.5 jsonpatch==1.33 jsonpointer==3.0.0 kubernetes==31.0.0 langchain==0.3.14 langchain-chroma==0.2.0 langchain-community==0.3.14 langchain-core==0.3.29 langchain-experimental==0.3.4 langchain-text-splitters==0.3.5 langsmith==0.2.10 markdown-it-py==3.0.0 MarkupSafe==3.0.2 marshmallow==3.25.1 mdurl==0.1.2 mmh3==5.0.1 monotonic==1.6 mpmath==1.3.0 multidict==6.1.0 mypy-extensions==1.0.0 networkx==3.4.2 numpy==1.26.4 oauthlib==3.2.2 onnxruntime==1.20.1 open_clip_torch==2.30.0 opentelemetry-api==1.29.0 opentelemetry-exporter-otlp-proto-common==1.29.0 opentelemetry-exporter-otlp-proto-grpc==1.29.0 opentelemetry-instrumentation==0.50b0 opentelemetry-instrumentation-asgi==0.50b0 opentelemetry-instrumentation-fastapi==0.50b0 opentelemetry-proto==1.29.0 opentelemetry-sdk==1.29.0 opentelemetry-semantic-conventions==0.50b0 opentelemetry-util-http==0.50b0 orjson==3.10.14 overrides==7.7.0 packaging==24.2 pillow==11.1.0 posthog==3.8.3 propcache==0.2.1 protobuf==5.29.3 pyasn1==0.6.1 pyasn1_modules==0.4.1 pydantic==2.10.5 pydantic-settings==2.7.1 pydantic_core==2.27.2 Pygments==2.19.1 PyPika==0.48.9 pyproject_hooks==1.2.0 pyreadline3==3.5.4 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 PyYAML==6.0.2 regex==2024.11.6 requests==2.32.3 requests-oauthlib==2.0.0 requests-toolbelt==1.0.0 rich==13.9.4 rsa==4.9 safetensors==0.5.2 setuptools==75.8.0 shellingham==1.5.4 six==1.17.0 sniffio==1.3.1 SQLAlchemy==2.0.37 starlette==0.41.3 sympy==1.13.1 tenacity==9.0.0 timm==1.0.13 tokenizers==0.20.3 torch==2.5.1 torchvision==0.20.1 tqdm==4.67.1 typer==0.15.1 typing-inspect==0.9.0 typing_extensions==4.12.2 urllib3==2.3.0 uvicorn==0.34.0 watchfiles==1.0.4 wcwidth==0.2.13 websocket-client==1.8.0 websockets==14.1 wrapt==1.17.2 yarl==1.18.3 zipp==3.21.0 |
※ pip install langchain_chroma langchain_experimental open_clip_torch 명령을 실행했다.