Home / Function/ get_openapi_chain() — langchain Function Reference

get_openapi_chain() — langchain Function Reference

Architecture documentation for the get_openapi_chain() function in openapi.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  66785d1e_5171_ae28_82e6_548fa9d30d28["get_openapi_chain()"]
  a34d3b0d_246d_bf4f_cf5f_c38a4937926e["openapi.py"]
  66785d1e_5171_ae28_82e6_548fa9d30d28 -->|defined in| a34d3b0d_246d_bf4f_cf5f_c38a4937926e
  174ab5ce_1945_87e8_8e93_22af84e01f0a["openapi_spec_to_openai_fn()"]
  66785d1e_5171_ae28_82e6_548fa9d30d28 -->|calls| 174ab5ce_1945_87e8_8e93_22af84e01f0a
  style 66785d1e_5171_ae28_82e6_548fa9d30d28 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/openai_functions/openapi.py lines 261–427

def get_openapi_chain(
    spec: OpenAPISpec | str,
    llm: BaseLanguageModel | None = None,
    prompt: BasePromptTemplate | None = None,
    request_chain: Chain | None = None,
    llm_chain_kwargs: dict | None = None,
    verbose: bool = False,  # noqa: FBT001,FBT002
    headers: dict | None = None,
    params: dict | None = None,
    **kwargs: Any,
) -> SequentialChain:
    r"""Create a chain for querying an API from a OpenAPI spec.

    Note: this class is deprecated. See below for a replacement implementation.
        The benefits of this implementation are:

        - Uses LLM tool calling features to encourage properly-formatted API requests;
        - Includes async support.

        ```python
        from typing import Any

        from langchain_classic.chains.openai_functions.openapi import openapi_spec_to_openai_fn
        from langchain_community.utilities.openapi import OpenAPISpec
        from langchain_core.prompts import ChatPromptTemplate
        from langchain_openai import ChatOpenAI

        # Define API spec. Can be JSON or YAML
        api_spec = \"\"\"
        {
        "openapi": "3.1.0",
        "info": {
            "title": "JSONPlaceholder API",
            "version": "1.0.0"
        },
        "servers": [
            {
            "url": "https://jsonplaceholder.typicode.com"
            }
        ],
        "paths": {
            "/posts": {
            "get": {
                "summary": "Get posts",
                "parameters": [
                {
                    "name": "_limit",
                    "in": "query",
                    "required": false,
                    "schema": {
                    "type": "integer",
                    "example": 2
                    },
                    "description": "Limit the number of results"
                }
                ]
            }
            }
        }
        }
        \"\"\"

        parsed_spec = OpenAPISpec.from_text(api_spec)
        openai_fns, call_api_fn = openapi_spec_to_openai_fn(parsed_spec)
        tools = [
            {"type": "function", "function": fn}
            for fn in openai_fns
        ]

        prompt = ChatPromptTemplate.from_template(
            "Use the provided APIs to respond to this user query:\\n\\n{query}"
        )
        model = ChatOpenAI(model="gpt-4o-mini", temperature=0).bind_tools(tools)

        def _execute_tool(message) -> Any:
            if tool_calls := message.tool_calls:
                tool_call = message.tool_calls[0]
                response = call_api_fn(name=tool_call["name"], fn_args=tool_call["args"])
                response.raise_for_status()
                return response.json()
            else:

Subdomains

Frequently Asked Questions

What does get_openapi_chain() do?
get_openapi_chain() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/openai_functions/openapi.py.
Where is get_openapi_chain() defined?
get_openapi_chain() is defined in libs/langchain/langchain_classic/chains/openai_functions/openapi.py at line 261.
What does get_openapi_chain() call?
get_openapi_chain() calls 1 function(s): openapi_spec_to_openai_fn.

Analyze Your Own Codebase

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

Try Supermodel Free