Home / File/ openai_functions.py — langchain Source File

openai_functions.py — langchain Source File

Architecture documentation for openai_functions.py, a python file in the langchain codebase. 12 imports, 0 dependents.

File python OutputParsing StreamingParsers 12 imports 5 classes

Entity Profile

Dependency Diagram

graph LR
  d083bb96_f642_0585_b68e_6b3fea430962["openai_functions.py"]
  e874d8a4_cef0_9d0b_d1ee_84999c07cc2c["copy"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> e874d8a4_cef0_9d0b_d1ee_84999c07cc2c
  73622474_3901_c3da_bce7_e7f2cbdfa6c6["json.py"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 73622474_3901_c3da_bce7_e7f2cbdfa6c6
  05bcd40b_a309_8a05_c2c6_59e32e113cf5["types"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 05bcd40b_a309_8a05_c2c6_59e32e113cf5
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  42c4a887_240e_d092_b770_25ae8eb73a14["jsonpatch"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 42c4a887_240e_d092_b770_25ae8eb73a14
  00fb336c_8832_626d_1bf9_f4f07a04d663["pydantic.py"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 00fb336c_8832_626d_1bf9_f4f07a04d663
  63ae09a2_4a67_3eaf_7888_b0fd11776a0e["pydantic.v1"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 63ae09a2_4a67_3eaf_7888_b0fd11776a0e
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 91721f45_4909_e489_8c1f_084f8bd87145
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  d45ff838_1238_0965_d3b7_5b33a0452ec0["langchain_core.output_parsers.json"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> d45ff838_1238_0965_d3b7_5b33a0452ec0
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  d083bb96_f642_0585_b68e_6b3fea430962 --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  style d083bb96_f642_0585_b68e_6b3fea430962 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Parsers for OpenAI functions output."""

import copy
import json
from types import GenericAlias
from typing import Any

import jsonpatch  # type: ignore[import-untyped]
from pydantic import BaseModel, model_validator
from pydantic.v1 import BaseModel as BaseModelV1
from typing_extensions import override

from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import (
    BaseCumulativeTransformOutputParser,
    BaseGenerationOutputParser,
)
from langchain_core.output_parsers.json import parse_partial_json
from langchain_core.outputs import ChatGeneration, Generation


class OutputFunctionsParser(BaseGenerationOutputParser[Any]):
    """Parse an output that is one of sets of values."""

    args_only: bool = True
    """Whether to only return the arguments to the function call."""

    @override
    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        """Parse the result of an LLM call to a JSON object.

        Args:
            result: The result of the LLM call.
            partial: Whether to parse partial JSON objects.

        Returns:
            The parsed JSON object.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        generation = result[0]
        if not isinstance(generation, ChatGeneration):
            msg = "This output parser can only be used with a chat generation."
            raise OutputParserException(msg)
        message = generation.message
        try:
            func_call = copy.deepcopy(message.additional_kwargs["function_call"])
        except KeyError as exc:
            msg = f"Could not parse function call: {exc}"
            raise OutputParserException(msg) from exc

        if self.args_only:
            return func_call["arguments"]
        return func_call


class JsonOutputFunctionsParser(BaseCumulativeTransformOutputParser[Any]):
    """Parse an output as the JSON object."""

// ... (254 more lines)

Domain

Subdomains

Dependencies

  • copy
  • json.py
  • jsonpatch
  • langchain_core.exceptions
  • langchain_core.output_parsers
  • langchain_core.output_parsers.json
  • langchain_core.outputs
  • pydantic.py
  • pydantic.v1
  • types
  • typing
  • typing_extensions

Frequently Asked Questions

What does openai_functions.py do?
openai_functions.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StreamingParsers subdomain.
What does openai_functions.py depend on?
openai_functions.py imports 12 module(s): copy, json.py, jsonpatch, langchain_core.exceptions, langchain_core.output_parsers, langchain_core.output_parsers.json, langchain_core.outputs, pydantic.py, and 4 more.
Where is openai_functions.py in the architecture?
openai_functions.py is located at libs/core/langchain_core/output_parsers/openai_functions.py (domain: OutputParsing, subdomain: StreamingParsers, directory: libs/core/langchain_core/output_parsers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free