.ipynb
.pdf
Extraction
 Contents 
Extracting entities
Pydantic example
Extraction#
The extraction chain uses the OpenAI functions parameter to specify a schema to extract entities from a document. This helps us make sure that the model outputs exactly the schema of entities and properties that we want, with their appropriate types.
The extraction chain is to be used when we want to extract several entities with their properties from the same passage (i.e. what people were mentioned in this passage?)
from langchain.chat_models import ChatOpenAI
from langchain.chains import create_extraction_chain, create_extraction_chain_pydantic
from langchain.prompts import ChatPromptTemplate
llm = ChatOpenAI(temperature=0, 
                   model="gpt-3.5-turbo-0613")
Extracting entities#
To extract entities, we need to create a schema like the following, were we specify all the properties we want to find and the type we expect them to have. We can also specify which of these properties are required and which are optional.
schema = {
      "properties": {
          "person_name": {"type": "string"}, 
          "person_height":{"type": "integer"},
          "person_hair_color": {"type": "string"},
          "dog_name": {"type": "string"},
          "dog_breed": {"type": "string"}
              },
        "required": ["person_name", "person_height"]
    }
inp = """
Alex is 5 feet tall. Claudia is 4 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.
Alex's dog Frosty is a labrador and likes to play hide and seek.
        """
chain = create_extraction_chain(schema, llm)
rtdocs_stable/api.python.langchain.com/en/stable/modules/chains/examples/extraction.html