■ SemanticSimilarityExampleSelector 클래스의 select_examples 메소드를 사용해 의미적으로 유사한 선택 예제 리스트를 구하는 방법을 보여준다.
※ OPENAI_API_KEY 환경 변수 값은 .env 파일에 정의한다.
▶ 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
from dotenv import load_dotenv from langchain_openai import OpenAIEmbeddings from langchain_chroma import Chroma from langchain_core.example_selectors import SemanticSimilarityExampleSelector load_dotenv() exampleList = [ { "question" : "Who lived longer, Muhammad Ali or Alan Turing?", "answer" : """Are follow up questions needed here : Yes. Follow up : How old was Muhammad Ali when he died? Intermediate answer : Muhammad Ali was 74 years old when he died. Follow up : How old was Alan Turing when he died? Intermediate answer : Alan Turing was 41 years old when he died. So the final answer is: Muhammad Ali """ }, { "question" : "When was the founder of craigslist born?", "answer" : """Are follow up questions needed here : Yes. Follow up : Who was the founder of craigslist? Intermediate answer : Craigslist was founded by Craig Newmark. Follow up : When was Craig Newmark born? Intermediate answer : Craig Newmark was born on December 6, 1952. So the final answer is: December 6, 1952 """ }, { "question": "Who was the maternal grandfather of George Washington?", "answer" : """Are follow up questions needed here : Yes. Follow up : Who was the mother of George Washington? Intermediate answer : The mother of George Washington was Mary Ball Washington. Follow up : Who was the father of Mary Ball Washington? Intermediate answer : The father of Mary Ball Washington was Joseph Ball. So the final answer is : Joseph Ball """ }, { "question" : "Are both the directors of Jaws and Casino Royale from the same country?", "answer" : """Are follow up questions needed here : Yes. Follow up : Who is the director of Jaws? Intermediate Answer : The director of Jaws is Steven Spielberg. Follow up : Where is Steven Spielberg from? Intermediate Answer : The United States. Follow up : Who is the director of Casino Royale? Intermediate Answer : The director of Casino Royale is Martin Campbell. Follow up : Where is Martin Campbell from? Intermediate Answer : New Zealand. So the final answer is : No """, } ] semanticSimilarityExampleSelector = SemanticSimilarityExampleSelector.from_examples( # 선택할 수 있는 예시 목록이다. exampleList, # 의미론적 유사성을 측정하는 데 사용되는 임베딩을 생성하는 데 사용되는 임베딩 클래스이다. OpenAIEmbeddings(), # 이는 임베딩을 저장하고 유사성 검색을 수행하는 데 사용되는 VectorStore 클래스이다. Chroma, # 이는 생성할 예제의 수이다. k = 1 ) question = "Who was the father of Mary Ball Washington?" selectedExampleList = semanticSimilarityExampleSelector.select_examples({"question" : question}) print(f"Examples most similar to the input : {question}") for example in selectedExampleList: print("\n") for key, value in example.items(): print(f"{key}: {value}") """ Examples most similar to the input : Who was the father of Mary Ball Washington? answer: Are follow up questions needed here : Yes. Follow up : Who was the mother of George Washington? Intermediate answer : The mother of George Washington was Mary Ball Washington. Follow up : Who was the father of Mary Ball Washington? Intermediate answer : The father of Mary Ball Washington was Joseph Ball. So the final answer is : Joseph Ball question : Who was the maternal grandfather of George Washington? """ |
▶ 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.3 click==8.1.7 coloredlogs==15.0.1 Deprecated==1.2.14 distro==1.9.0 dnspython==2.6.1 email_validator==2.2.0 exceptiongroup==1.2.1 fastapi==0.111.0 fastapi-cli==0.0.4 filelock==3.15.4 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.4 humanfriendly==10.0 idna==3.7 importlib_metadata==7.1.0 importlib_resources==6.4.0 Jinja2==3.1.4 jsonpatch==1.33 jsonpointer==3.0.0 kubernetes==30.1.0 langchain==0.2.5 langchain-chroma==0.1.1 langchain-core==0.2.9 langchain-openai==0.1.9 langchain-text-splitters==0.2.1 langsmith==0.1.81 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.35.3 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.5 overrides==7.7.0 packaging==24.1 posthog==3.5.0 protobuf==4.25.3 pyasn1==0.6.0 pyasn1_modules==0.4.0 pydantic==2.7.4 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.31 starlette==0.37.2 sympy==1.12.1 tenacity==8.4.1 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.2 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 install python-dotenv langchain langchain_chroma langchain-openai 명령을 실행했다.