■ Chroma 클래스의 similarity_search_with_score 메소드를 사용해 검색 문자열로 검색 결과 문서 리스트를 구하는 방법을 보여준다. (점수 포함) ▶ 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
import os from langchain_core.documents import Document from langchain_chroma import Chroma from langchain_openai import OpenAIEmbeddings os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" documentList = [ Document( page_content = "Dogs are great companions, known for their loyalty and friendliness.", metadata = {"source" : "mammal-pets-doc"} ), Document( page_content = "Cats are independent pets that often enjoy their own space.", metadata = {"source" : "mammal-pets-doc"}, ), Document( page_content = "Goldfish are popular pets for beginners, requiring relatively simple care.", metadata = {"source" : "fish-pets-doc"}, ), Document( page_content = "Parrots are intelligent birds capable of mimicking human speech.", metadata = {"source" : "bird-pets-doc"}, ), Document( page_content = "Rabbits are social animals that need plenty of space to hop around.", metadata = {"source" : "mammal-pets-doc"}, ), ] chroma = Chroma.from_documents( documentList, embedding = OpenAIEmbeddings(), ) searchResultList = chroma.similarity_search_with_score("cat") print(searchResultList) """ [ ( Document( page_content = 'Cats are independent pets that often enjoy their own space.', metadata = {'source' : 'mammal-pets-doc'} ), 0.375326931476593 ), ( Document( page_content = 'Dogs are great companions, known for their loyalty and friendliness.', metadata = {'source' : 'mammal-pets-doc'} ), 0.4833090305328369 ), ( Document( page_content = 'Rabbits are social animals that need plenty of space to hop around.', metadata = {'source' : 'mammal-pets-doc'} ), 0.4958883225917816 ), ( Document( page_content = 'Parrots are intelligent birds capable of mimicking human speech.', metadata = {'source' : 'bird-pets-doc'} ), 0.4974174499511719 ) ] """ |
▶ requirements.txt
더 읽기
■ Chroma 클래스의 asimilarity_search 메소드를 사용해 검색 문자열로 검색 결과 문서 리스트를 구하는 방법을 보여준다. (비동기) ▶ 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 61 62 63 64 65 66
|
import asyncio import os from langchain_core.documents import Document from langchain_chroma import Chroma from langchain_openai import OpenAIEmbeddings async def main(): os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" documentList = [ Document( page_content = "Dogs are great companions, known for their loyalty and friendliness.", metadata = {"source" : "mammal-pets-doc"} ), Document( page_content = "Cats are independent pets that often enjoy their own space.", metadata = {"source" : "mammal-pets-doc"}, ), Document( page_content = "Goldfish are popular pets for beginners, requiring relatively simple care.", metadata = {"source" : "fish-pets-doc"}, ), Document( page_content = "Parrots are intelligent birds capable of mimicking human speech.", metadata = {"source" : "bird-pets-doc"}, ), Document( page_content = "Rabbits are social animals that need plenty of space to hop around.", metadata = {"source" : "mammal-pets-doc"}, ), ] chroma = Chroma.from_documents( documentList, embedding = OpenAIEmbeddings(), ) searchResultList = await chroma.asimilarity_search("cat") print(searchResultList) asyncio.run(main()) """ [ Document( page_content = 'Cats are independent pets that often enjoy their own space.', metadata = {'source' : 'mammal-pets-doc'} ), Document( page_content = 'Dogs are great companions, known for their loyalty and friendliness.', metadata = {'source' : 'mammal-pets-doc'} ), Document( page_content = 'Rabbits are social animals that need plenty of space to hop around.', metadata = {'source' : 'mammal-pets-doc'} ), Document( page_content = 'Parrots are intelligent birds capable of mimicking human speech.', metadata = {'source' : 'bird-pets-doc'} ) ] """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 asgiref==3.8.1 async-timeout==4.0.3 attrs==23.2.0 backoff==2.2.1 bcrypt==4.1.3 build==1.2.1 cachetools==5.3.3 certifi==2024.6.2 charset-normalizer==3.3.2 chroma-hnswlib==0.7.3 chromadb==0.5.0 click==8.1.7 coloredlogs==15.0.1 Deprecated==1.2.14 distro==1.9.0 dnspython==2.6.1 email_validator==2.1.1 exceptiongroup==1.2.1 fastapi==0.111.0 fastapi-cli==0.0.4 filelock==3.14.0 flatbuffers==24.3.25 frozenlist==1.4.1 fsspec==2024.6.0 google-auth==2.30.0 googleapis-common-protos==1.63.1 greenlet==3.0.3 grpcio==1.64.1 h11==0.14.0 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 huggingface-hub==0.23.3 humanfriendly==10.0 idna==3.7 importlib_metadata==7.1.0 importlib_resources==6.4.0 Jinja2==3.1.4 jsonpatch==1.33 jsonpointer==2.4 kubernetes==30.1.0 langchain==0.2.3 langchain-chroma==0.1.1 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 mmh3==4.1.0 monotonic==1.6 mpmath==1.3.0 multidict==6.0.5 numpy==1.26.4 oauthlib==3.2.2 onnxruntime==1.18.0 openai==1.33.0 opentelemetry-api==1.25.0 opentelemetry-exporter-otlp-proto-common==1.25.0 opentelemetry-exporter-otlp-proto-grpc==1.25.0 opentelemetry-instrumentation==0.46b0 opentelemetry-instrumentation-asgi==0.46b0 opentelemetry-instrumentation-fastapi==0.46b0 opentelemetry-proto==1.25.0 opentelemetry-sdk==1.25.0 opentelemetry-semantic-conventions==0.46b0 opentelemetry-util-http==0.46b0 orjson==3.10.3 overrides==7.7.0 packaging==23.2 posthog==3.5.0 protobuf==4.25.3 pyasn1==0.6.0 pyasn1_modules==0.4.0 pydantic==2.7.3 pydantic_core==2.18.4 Pygments==2.18.0 PyPika==0.48.9 pyproject_hooks==1.1.0 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 python-multipart==0.0.9 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 requests-oauthlib==2.0.0 rich==13.7.1 rsa==4.9 shellingham==1.5.4 six==1.16.0 sniffio==1.3.1 SQLAlchemy==2.0.30 starlette==0.37.2 sympy==1.12.1 tenacity==8.3.0 tiktoken==0.7.0 tokenizers==0.19.1 tomli==2.0.1 tqdm==4.66.4 typer==0.12.3 typing_extensions==4.12.2 ujson==5.10.0 urllib3==2.2.1 uvicorn==0.30.1 uvloop==0.19.0 watchfiles==0.22.0 websocket-client==1.8.0 websockets==12.0 wrapt==1.16.0 yarl==1.9.4 zipp==3.19.2 |
더 읽기
■ Chroma 클래스의 similarity_search 메소드를 사용해 검색 문자열로 검색 결과 문서 리스트를 구하는 방법을 보여준다. ▶ 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 61 62
|
import os from langchain_core.documents import Document from langchain_chroma import Chroma from langchain_openai import OpenAIEmbeddings os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" documentList = [ Document( page_content = "Dogs are great companions, known for their loyalty and friendliness.", metadata = {"source" : "mammal-pets-doc"} ), Document( page_content = "Cats are independent pets that often enjoy their own space.", metadata = {"source" : "mammal-pets-doc"}, ), Document( page_content = "Goldfish are popular pets for beginners, requiring relatively simple care.", metadata = {"source" : "fish-pets-doc"}, ), Document( page_content = "Parrots are intelligent birds capable of mimicking human speech.", metadata = {"source" : "bird-pets-doc"}, ), Document( page_content = "Rabbits are social animals that need plenty of space to hop around.", metadata = {"source" : "mammal-pets-doc"}, ), ] chroma = Chroma.from_documents( ) documentList, embedding = OpenAIEmbeddings(), searchResultList = chroma.similarity_search("cat") print(searchResultList) """ [ Document( page_content = 'Cats are independent pets that often enjoy their own space.', metadata = {'source' : 'mammal-pets-doc'} ), Document( page_content = 'Dogs are great companions, known for their loyalty and friendliness.', metadata = {'source' : 'mammal-pets-doc'} ), Document( page_content = 'Rabbits are social animals that need plenty of space to hop around.', metadata = {'source' : 'mammal-pets-doc'} ), Document( page_content = 'Parrots are intelligent birds capable of mimicking human speech.', metadata = {'source' : 'bird-pets-doc'} ) ] """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 asgiref==3.8.1 async-timeout==4.0.3 attrs==23.2.0 backoff==2.2.1 bcrypt==4.1.3 build==1.2.1 cachetools==5.3.3 certifi==2024.6.2 charset-normalizer==3.3.2 chroma-hnswlib==0.7.3 chromadb==0.5.0 click==8.1.7 coloredlogs==15.0.1 Deprecated==1.2.14 distro==1.9.0 dnspython==2.6.1 email_validator==2.1.1 exceptiongroup==1.2.1 fastapi==0.111.0 fastapi-cli==0.0.4 filelock==3.14.0 flatbuffers==24.3.25 frozenlist==1.4.1 fsspec==2024.6.0 google-auth==2.30.0 googleapis-common-protos==1.63.1 greenlet==3.0.3 grpcio==1.64.1 h11==0.14.0 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 huggingface-hub==0.23.3 humanfriendly==10.0 idna==3.7 importlib_metadata==7.1.0 importlib_resources==6.4.0 Jinja2==3.1.4 jsonpatch==1.33 jsonpointer==2.4 kubernetes==30.1.0 langchain==0.2.3 langchain-chroma==0.1.1 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 mmh3==4.1.0 monotonic==1.6 mpmath==1.3.0 multidict==6.0.5 numpy==1.26.4 oauthlib==3.2.2 onnxruntime==1.18.0 openai==1.33.0 opentelemetry-api==1.25.0 opentelemetry-exporter-otlp-proto-common==1.25.0 opentelemetry-exporter-otlp-proto-grpc==1.25.0 opentelemetry-instrumentation==0.46b0 opentelemetry-instrumentation-asgi==0.46b0 opentelemetry-instrumentation-fastapi==0.46b0 opentelemetry-proto==1.25.0 opentelemetry-sdk==1.25.0 opentelemetry-semantic-conventions==0.46b0 opentelemetry-util-http==0.46b0 orjson==3.10.3 overrides==7.7.0 packaging==23.2 posthog==3.5.0 protobuf==4.25.3 pyasn1==0.6.0 pyasn1_modules==0.4.0 pydantic==2.7.3 pydantic_core==2.18.4 Pygments==2.18.0 PyPika==0.48.9 pyproject_hooks==1.1.0 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 python-multipart==0.0.9 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 requests-oauthlib==2.0.0 rich==13.7.1 rsa==4.9 shellingham==1.5.4 six==1.16.0 sniffio==1.3.1 SQLAlchemy==2.0.30 starlette==0.37.2 sympy==1.12.1 tenacity==8.3.0 tiktoken==0.7.0 tokenizers==0.19.1 tomli==2.0.1 tqdm==4.66.4 typer==0.12.3 typing_extensions==4.12.2 ujson==5.10.0 urllib3==2.2.1 uvicorn==0.30.1 uvloop==0.19.0 watchfiles==0.22.0 websocket-client==1.8.0 websockets==12.0 wrapt==1.16.0 yarl==1.9.4 zipp==3.19.2 |
※
더 읽기
■ from_documents 함수를 사용해 메모리 내 벡터 저장소를 위한 Chroma 객체를 만드는 방법을 보여준다. ▶ 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
|
import os from langchain_core.documents import Document from langchain_chroma import Chroma from langchain_openai import OpenAIEmbeddings os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" documentList = [ Document( page_content = "Dogs are great companions, known for their loyalty and friendliness.", metadata = {"source" : "mammal-pets-doc"} ), Document( page_content = "Cats are independent pets that often enjoy their own space.", metadata = {"source" : "mammal-pets-doc"}, ), Document( page_content = "Goldfish are popular pets for beginners, requiring relatively simple care.", metadata = {"source" : "fish-pets-doc"}, ), Document( page_content = "Parrots are intelligent birds capable of mimicking human speech.", metadata = {"source" : "bird-pets-doc"}, ), Document( page_content = "Rabbits are social animals that need plenty of space to hop around.", metadata = {"source" : "mammal-pets-doc"}, ), ] chroma = Chroma.from_documents( documentList, embedding = OpenAIEmbeddings(), ) |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 asgiref==3.8.1 async-timeout==4.0.3 attrs==23.2.0 backoff==2.2.1 bcrypt==4.1.3 build==1.2.1 cachetools==5.3.3 certifi==2024.6.2 charset-normalizer==3.3.2 chroma-hnswlib==0.7.3 chromadb==0.5.0 click==8.1.7 coloredlogs==15.0.1 Deprecated==1.2.14 distro==1.9.0 dnspython==2.6.1 email_validator==2.1.1 exceptiongroup==1.2.1 fastapi==0.111.0 fastapi-cli==0.0.4 filelock==3.14.0 flatbuffers==24.3.25 frozenlist==1.4.1 fsspec==2024.6.0 google-auth==2.30.0 googleapis-common-protos==1.63.1 greenlet==3.0.3 grpcio==1.64.1 h11==0.14.0 httpcore==1.0.5 httptools==0.6.1 httpx==0.27.0 huggingface-hub==0.23.3 humanfriendly==10.0 idna==3.7 importlib_metadata==7.1.0 importlib_resources==6.4.0 Jinja2==3.1.4 jsonpatch==1.33 jsonpointer==2.4 kubernetes==30.1.0 langchain==0.2.3 langchain-chroma==0.1.1 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 markdown-it-py==3.0.0 MarkupSafe==2.1.5 mdurl==0.1.2 mmh3==4.1.0 monotonic==1.6 mpmath==1.3.0 multidict==6.0.5 numpy==1.26.4 oauthlib==3.2.2 onnxruntime==1.18.0 openai==1.33.0 opentelemetry-api==1.25.0 opentelemetry-exporter-otlp-proto-common==1.25.0 opentelemetry-exporter-otlp-proto-grpc==1.25.0 opentelemetry-instrumentation==0.46b0 opentelemetry-instrumentation-asgi==0.46b0 opentelemetry-instrumentation-fastapi==0.46b0 opentelemetry-proto==1.25.0 opentelemetry-sdk==1.25.0 opentelemetry-semantic-conventions==0.46b0 opentelemetry-util-http==0.46b0 orjson==3.10.3 overrides==7.7.0 packaging==23.2 posthog==3.5.0 protobuf==4.25.3 pyasn1==0.6.0 pyasn1_modules==0.4.0 pydantic==2.7.3 pydantic_core==2.18.4 Pygments==2.18.0 PyPika==0.48.9 pyproject_hooks==1.1.0 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 python-multipart==0.0.9 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 requests-oauthlib==2.0.0 rich==13.7.1 rsa==4.9 shellingham==1.5.4 six==1.16.0 sniffio==1.3.1 SQLAlchemy==2.0.30 starlette==0.37.2 sympy==1.12.1 tenacity==8.3.0 tiktoken==0.7.0 tokenizers==0.19.1 tomli==2.0.1 tqdm==4.66.4 typer==0.12.3 typing_extensions==4.12.2 ujson==5.10.0 urllib3==2.2.1 uvicorn==0.30.1 uvloop==0.19.0 watchfiles==0.22.0 websocket-client==1.8.0 websockets==12.0 wrapt==1.16.0 yarl==1.9.4 zipp==3.19.2 |
※ pip
더 읽기
■ langchain-chroma 패키지를 설치하는 방법을 보여준다. 1. 명령 프롬프트를 실행한다. 2. 명령 프롬프트에서 아래 명령을 실행한다. ▶ 실행 명령
|
pip install langchain-chroma |
■ RunnableWithMessageHistory 클래스의 stream 메소드를 사용해 응답 스트리밍을 처리하는 방법을 보여준다. • LLM이 응답하는 데 시간이 걸릴 수 있으므로 사용자 경험을 개선하기
더 읽기
■ RunnablePassthrough 클래스의 assign 메소드를 사용해 대화 기록을 관리하는 방법을 보여준다. • 챗봇을 구축할 때 중요한 점은 대화 기록을 관리하는 방법이다. •
더 읽기
■ RunnableWithMessageHistory 클래스를 사용해 챗봇을 만드는 방법을 보여준다. ▶ 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 61 62 63
|
import os from langchain_core.prompts import ChatPromptTemplate from langchain_core.prompts import MessagesPlaceholder from langchain_openai import ChatOpenAI from langchain_core.chat_history import BaseChatMessageHistory from langchain_community.chat_message_histories import ChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_core.messages import HumanMessage os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" chatPromptTemplate = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant. Answer all questions to the best of your ability in {language}."), MessagesPlaceholder(variable_name = "messages"), ] ) chatOpenAI = ChatOpenAI(model = "gpt-3.5-turbo") runnableSequence = chatPromptTemplate | chatOpenAI storeDictionary = {} def GetChatMessageHistory(sessionID : str) -> BaseChatMessageHistory: if sessionID not in storeDictionary: storeDictionary[sessionID] = ChatMessageHistory() return storeDictionary[sessionID] runnableWithMessageHistory = RunnableWithMessageHistory( runnableSequence, GetChatMessageHistory, input_messages_key = "messages", ) configuratonDictionary = {"configurable" : {"session_id" : "session1"}} aiMessage1 = runnableWithMessageHistory.invoke( { "messages" : [HumanMessage(content = "hi! I'm todd")], "language" : "Korean" }, config = configuratonDictionary, ) aiMessage2 = runnableWithMessageHistory.invoke( { "messages" : [HumanMessage(content = "whats my name?")], "language" : "Korean" }, config = configuratonDictionary, ) print(aiMessage1.content) print(aiMessage2.content) """ 안녕하세요, 토드님! 어떻게 도와드릴까요? 당신의 이름은 토드입니다. """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 dataclasses-json==0.6.6 distro==1.9.0 exceptiongroup==1.2.1 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-community==0.2.4 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 marshmallow==3.21.3 multidict==6.0.5 mypy-extensions==1.0.0 numpy==1.26.4 openai==1.33.0 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 sniffio==1.3.1 SQLAlchemy==2.0.30 tenacity==8.3.0 tiktoken==0.7.0 tqdm==4.66.4 typing-inspect==0.9.0 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip install langchain langchain-openai langchain_community 명령을 실행했다.
■ langchain_community 패키지를 설치하는 방법을 보여준다. 1. 명령 프롬프트를 실행한다. 2. 명령 프롬프트에서 아래 명령을 실행한다. ▶ 실행 명령
|
pip install langchain_community |
■ | 연산자를 사용해 프롬프트 템플리트, LLM 모델과 출력 파서의 체인을 만드는 방법을 보여준다. ▶ 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
|
import os from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI from langchain_core.output_parsers import StrOutputParser os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" promptTemplate = ChatPromptTemplate.from_messages( [ ("system", "Translate the following into {language} : "), ("user" , "{text}" ) ] ) chatOpenAI = ChatOpenAI(model = "gpt-4") strOutputParser = StrOutputParser() runnableSequence = promptTemplate | chatOpenAI | strOutputParser resultString = runnableSequence.invoke({"language" : "korean", "text" : "hi"}) print(resultString) """ 안녕하세요 """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 distro==1.9.0 exceptiongroup==1.2.1 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 openai==1.33.0 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 sniffio==1.3.1 SQLAlchemy==2.0.30 tenacity==8.3.0 tiktoken==0.7.0 tqdm==4.66.4 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip
더 읽기
■ langserve 모듈을 사용해 langchain 모듈의 체인을 REST API 서버로 배포하는 방법을 보여준다. ▶ server.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
|
import os import uvicorn from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_openai import ChatOpenAI from fastapi import FastAPI from langserve import add_routes os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" promptTemplate = ChatPromptTemplate.from_messages( [ ("system", "Translate the following into {language} : "), ("user" , "{text}" ) ] ) chatOpenAI = ChatOpenAI(model = "gpt-4") strOutputParser = StrOutputParser() runnableSequence = promptTemplate | chatOpenAI | strOutputParser fastAPI = FastAPI( title = "LangChain Server", version = "1.0", description = "A simple API server using LangChain's Runnable interfaces", ) add_routes( fastAPI, runnableSequence, path = "/chain", ) if __name__ == "__main__": uvicorn.run(fastAPI, host = "0.0.0.0", port = 8000) |
▶ client.py
|
from langserve import RemoteRunnable remoteRunnable = RemoteRunnable("http://localhost:8000/chain/") resultString = remoteRunnable.invoke({"language" : "korean", "text" : "hi"}) print(resultString) |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 distro==1.9.0 exceptiongroup==1.2.1 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 openai==1.33.0 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 sniffio==1.3.1 SQLAlchemy==2.0.30 tenacity==8.3.0 tiktoken==0.7.0 tqdm==4.66.4 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
더 읽기
■ langserve 패키지를 설치하는 방법을 보여준다. 1. 명령 프롬프트를 실행한다. 2. 명령 프롬프트에서 아래 명령을 실행한다. ▶ 실행 명령
|
pip install "langserve[all]" |
■ ChatPromptValue 클래스의 to_messages 메소드를 사용해 ChatPromptTemplate 객체에서 메시지 리스트를 구하는 방법을 배운다. ▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
from langchain_core.prompts import ChatPromptTemplate chatPromptTemplate = ChatPromptTemplate.from_messages( [ ("system", "Translate the following into {language} : "), ("user" , "{text}" ) ] ) chatPromptValue = chatPromptTemplate.invoke({"language" : "Korean", "text" : "Good moring!"}) resultMessageList = chatPromptValue.to_messages() print(resultMessageList) """ [SystemMessage(content='Translate the following into Korean : '), HumanMessage(content='Good moring!')] """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 frozenlist==1.4.1 greenlet==3.0.3 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 requests==2.32.3 SQLAlchemy==2.0.30 tenacity==8.3.0 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip install
더 읽기
■ ChatPromptTemplate 클래스의 from_messages 정적 메소드를 사용해 채팅 프롬프트 템플리트를 만드는 방법을 보여준다. (ChatPromptTemplate 객체) ▶ main.py
|
from langchain_core.prompts import ChatPromptTemplate chatPromptTemplate = ChatPromptTemplate.from_messages([("system", "Translate the following into {language} : "), ("user", "{text}")]) chatPromptValue = chatPromptTemplate.invoke({"language" : "korean", "text" : "Good morning!"}) print(chatPromptValue) """ messages=[SystemMessage(content='Translate the following into korean : '), HumanMessage(content='Good morning')] """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 frozenlist==1.4.1 greenlet==3.0.3 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 requests==2.32.3 SQLAlchemy==2.0.30 tenacity==8.3.0 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※
더 읽기
■ | 연산자를 사용해 LLM 모델과 출력 파서의 체인을 만드는 방법을 보여준다. ▶ 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
|
import os from langchain_core.messages import HumanMessage, SystemMessage from langchain_core.output_parsers import StrOutputParser from langchain_openai import ChatOpenAI os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" chatOpenAI = ChatOpenAI(model = "gpt-4") strOutputParser = StrOutputParser() runnableSequence = chatOpenAI | strOutputParser messageList = [ SystemMessage(content = "Translate the following from English into Korean"), HumanMessage (content = "hi!") ] resultString = runnableSequence.invoke(messageList) print(resultString) """ 안녕! """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 distro==1.9.0 exceptiongroup==1.2.1 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 openai==1.33.0 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 sniffio==1.3.1 SQLAlchemy==2.0.30 tenacity==8.3.0 tiktoken==0.7.0 tqdm==4.66.4 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip install langchain
더 읽기
■ StrOutputParser 클래스의 invoke 메소드를 사용해 출력 결과를 파싱하는 방법을 보여준다. ※ StrOutputParser 클래스는 AIMessageChunk에서 content 필드를 추출하여 모델이 반환한 토큰을 제공하는
더 읽기
■ ChatOpenAI 클래스의 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
|
import os from langchain_core.messages import HumanMessage, SystemMessage from langchain_openai import ChatOpenAI os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" chatOpenAI = ChatOpenAI(model = "gpt-4") messageList = [ SystemMessage(content = "Translate the following from English into Korean"), HumanMessage (content = "hi!") ] aiMessage = chatOpenAI.invoke(messageList) print(aiMessage.content) """ 안녕! """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 distro==1.9.0 exceptiongroup==1.2.1 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 openai==1.33.0 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 sniffio==1.3.1 SQLAlchemy==2.0.30 tenacity==8.3.0 tiktoken==0.7.0 tqdm==4.66.4 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip install langchain langchain-openai 명령을
더 읽기
■ | 연산자를 사용해 프롬프트 템플리트와 LLM 모델의 체인을 만드는 방법을 보여준다. ▶ 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
|
import os from langchain.prompts import PromptTemplate from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" promptTemplate = PromptTemplate( input_variables = ["product"], template = "{product}을 만드는 새로운 한국어 회사명을 하나 제안해 주세요." ) openAI = OpenAI(temperature = 0.9) runnableSequence = promptTemplate | openAI resultString = runnableSequence.invoke("가정용 로봇") print(resultString) """ "홈로봇즈" """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 frozenlist==1.4.1 greenlet==3.0.3 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 requests==2.32.3 SQLAlchemy==2.0.30 tenacity==8.3.0 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip install openai
더 읽기
■ PromptTemplate 클래스의 생성자에서 input_variables/template 인자를 사용해 PromptTemplate 객체를 만드는 방법을 보여준다. • 프롬프트 템플리트는 사용자 입력으로 프롬프트를 생성하기 위한 템플리트이다. •
더 읽기
■ langchain-openai 패키지를 설치하는 방법을 보여준다. 1. 명령 프롬프트를 실행한다. 2. 명령 프롬프트에서 아래 명령을 실행한다. ▶ 실행 명령
|
pip install langchain-openai |
■ OpenAI 클래스를 사용해 질의 응답하는 방법을 보여준다. ▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import os from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = "<OPENA_API_KEY>" openAI = OpenAI(temperature = 0.9) resultString = openAI("컴퓨터 게임을 만드는 새로운 한국어 회사명을 하나 제안해 주세요.") print(resultString) """ "비트월드 게임즈" """ |
▶ 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
|
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 async-timeout==4.0.3 attrs==23.2.0 certifi==2024.6.2 charset-normalizer==3.3.2 distro==1.9.0 exceptiongroup==1.2.1 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 jsonpatch==1.33 jsonpointer==2.4 langchain==0.2.3 langchain-core==0.2.5 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.75 multidict==6.0.5 numpy==1.26.4 openai==1.33.0 orjson==3.10.3 packaging==23.2 pydantic==2.7.3 pydantic_core==2.18.4 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 sniffio==1.3.1 SQLAlchemy==2.0.30 tenacity==8.3.0 tiktoken==0.7.0 tqdm==4.66.4 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip install openai langchain langchain-openai 명령을 실행했다.
■ langchain-huggingface 패키지를 업그레이드하는 방법을 보여준다. 1. 명령 프롬프트를 실행한다. 2. 명령 프롬프트에서 아래 명령을 실행한다. ▶ 실행 명령
|
pip install langchain-huggingface |
■ langchain-community 패키지를 설치하는 방법을 보여준다. 1. 명령 프롬프트를 실행한다. 2. 명령 프롬프트에서 아래 명령을 실행한다. ▶ 실행 명령
|
pip install langchain-community |
■ langchain 패키지를 설치하는 방법을 보여준다. 1. 명령 프롬프트를 실행한다. 2. 명령 프롬프트에서 아래 명령을 실행한다. ▶ 실행 명령