Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKQA over structured dataSQLRetrieval-augmented generation (RAG)Interacting with APIsChatbotsExtractionSummarizationTaggingWeb scrapingSynthetic data generationGraph queryingExtractionOn this pageExtractionUse case‚ÄãGetting structured output from raw LLM generations is hard.For example, suppose you need the model output formatted with a specific schema for:Extracting a structured row to insert into a database Extracting API parametersExtracting different parts of a user query (e.g., for semantic vs keyword search)Overview‚ÄãThere are two primary approaches for this:Functions: Some LLMs can call functions to extract arbitrary entities from LLM responses.Parsing: Output parsers are classes that structure LLM responses. Only some LLMs support functions (e.g., OpenAI), and they are more general than parsers. Parsers extract precisely what is enumerated in a provided schema (e.g., specific attributes of a person).Functions can infer things beyond of a provided schema (e.g., attributes about a person that you did not ask for).Quickstart‚ÄãOpenAI functions are one way to get started with extraction.Define a schema that specifies the properties we want to extract from the LLM output.Then, we can use create_extraction_chain to extract our desired schema using an OpenAI function call.pip install langchain openai # Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()from langchain.chat_models import ChatOpenAIfrom langchain.chains import create_extraction_chain# Schemaschema = {    "properties": {        "name": {"type": "string"},        "height": {"type": "integer"},        "hair_color": {"type": "string"},    },    "required": ["name", "height"],}# Input inp = """Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde."""# Run chainllm =
Open In Collab
Open In Collab ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKQA over structured dataSQLRetrieval-augmented generation (RAG)Interacting with APIsChatbotsExtractionSummarizationTaggingWeb scrapingSynthetic data generationGraph queryingExtractionOn this pageExtractionUse case‚ÄãGetting structured output from raw LLM generations is hard.For example, suppose you need the model output formatted with a specific schema for:Extracting a structured row to insert into a database Extracting API parametersExtracting different parts of a user query (e.g., for semantic vs keyword search)Overview‚ÄãThere are two primary approaches for this:Functions: Some LLMs can call functions to extract arbitrary entities from LLM responses.Parsing: Output parsers are classes that structure LLM responses. Only some LLMs support functions (e.g., OpenAI), and they are more general than parsers. Parsers extract precisely what is enumerated in a provided schema (e.g., specific attributes of a person).Functions can infer things beyond of a provided schema (e.g., attributes about a person that you did not ask for).Quickstart‚ÄãOpenAI functions are one way to get started with extraction.Define a schema that specifies the properties we want to extract from the LLM output.Then, we can use create_extraction_chain to extract our desired schema using an OpenAI function call.pip install langchain openai # Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()from langchain.chat_models import ChatOpenAIfrom langchain.chains import create_extraction_chain# Schemaschema = {    "properties": {        "name": {"type": "string"},        "height": {"type": "integer"},        "hair_color": {"type": "string"},    },    "required": ["name", "height"],}# Input inp = """Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde."""# Run chainllm =