■ ChatMistralAI 클래스의 with_structured_output 메소드를 사용해 비정형 텍스트에서 객체를 구하는 방법을 보여준다.
▶ 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 |
import os from typing import Optional from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.prompts import ChatPromptTemplate from langchain_mistralai import ChatMistralAI os.environ["MISTRAL_API_KEY"] = "<MISTRAL_API_KEY>" # 구조화된 모델 클래스를 정의한다. class Person(BaseModel): """Information about a person.""" # ^ Doc-string for the entity Person. # This doc-string is sent to the LLM as the description of the schema Person, # and it can help to improve extraction results. # Note that: # 1. Each field is an `optional` -- this allows the model to decline to extract it! # 2. Each field has a `description` -- this description is used by the LLM. # Having a good description can help improve extraction results. name : Optional[str] = Field(default = None, description = "The name of the person" ) hair_color : Optional[str] = Field(default = None, description = "The color of the person's hair if known") height_in_meters : Optional[str] = Field(default = None, description = "Height measured in meters" ) # 채팅 프롬프트 템플리트를 설정한다. chatPromptTemplate = ChatPromptTemplate.from_messages( [ ( "system", "You are an expert extraction algorithm. " "Only extract relevant information from the text. " "If you do not know the value of an attribute asked to extract, " "return null for the attribute's value.", ), ("human", "{text}"), ] ) # LLM 모델을 설정한다. chatMistralAI = ChatMistralAI(model = "mistral-large-latest", temperature = 0) # 추출 체인을 설정한다. extractionRunnableSequence = chatMistralAI.with_structured_output(schema = Person) # 실행 체인을 설정한다. executeRunnableSequence = chatPromptTemplate | extractionRunnableSequence # 질의 응답을 실행한다. person = executeRunnableSequence.invoke({"text" : "Alan Smith is 6 feet tall and has blond hair."}) print(person) """ name='Alan Smith' hair_color='blond' height_in_meters='1.8288' """ |
▶ 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 |
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 exceptiongroup==1.2.1 filelock==3.15.1 frozenlist==1.4.1 fsspec==2024.6.0 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 httpx-sse==0.4.0 huggingface-hub==0.23.3 idna==3.7 jsonpatch==1.33 jsonpointer==3.0.0 langchain==0.2.4 langchain-core==0.2.6 langchain-mistralai==0.1.8 langchain-text-splitters==0.2.1 langsmith==0.1.77 multidict==6.0.5 numpy==1.26.4 orjson==3.10.5 packaging==24.1 pydantic==2.7.4 pydantic_core==2.18.4 PyYAML==6.0.1 requests==2.32.3 sniffio==1.3.1 SQLAlchemy==2.0.30 tenacity==8.3.0 tokenizers==0.19.1 tqdm==4.66.4 typing_extensions==4.12.2 urllib3==2.2.1 yarl==1.9.4 |
※ pip install langchain langchain_mistralai 명령을 실행했다.