Skip to main content

A library to convert an OpenAPI spec to LangGraph tools

Project description

openapi2tools

Release Notes PyPI - License

Convert an OpenAPI spec to LangGraph tools 🪄

Concepts

  1. LangChain is a framework for developing apps powered by large language models (LLMs).
  2. LangGraph, introduced by LangChain starting from v0.2, facilitates building Agent-based apps that leverage and extend a LLM to perform tasks outside of its traditional domain. The heart of an agent is Tool calls.
  3. OpenAPI Specification (OAS) is a standard format for defining APIs, allowing both humans and machines to understand the capabilities of a service without accessing its source code, documentation, or network traffic.

Companies worldwide have invested billions 💰💰💰 into developing APIs. In the new AI era, a question begs, how to leverage the power of natural language processing to consume the APIs while minimizing costs and maximizing existing investments.

openapi2tools addresses the challenge by converting and encapsulating an OpenAPI specification JSON into a domain-specific LangGraph agent, with each API represented as a tool.

Installation and Usage

openapi2tools can be installed with pip,

pip install openapi2tools

After installation, you run the tool in a command line to generate an agent source file from a OpenAPI JSON (example json)

openapi2tools <your_openapi_json_file> <output_python_file_name>

Anatomy of an Agent for a Chatbot

The MockServer folder implements a REST API based service and provides an OpenAPI JSON (openapi.json). openapi2tools is supposed to run against the openapi.json to generate a fully encapsulated Agent (moviesApi.py).

The Chatbot folder contains an example implementation of a Chatbot (movies.py). It then utilizes the Agent generated above to call (a.k.a. Tool calls) the Mockserver, when a user chats with an OpenAI model.

Anatomy of the moviesApi Agent

  • an engineered ChatPrompt
prompt = ChatPromptTemplate.from_messages([
    ('system', 
        '''
        generate an overview from the `info.description`

        forEach api in openapi.json:
            generate a tool capability from `api.operationId` and `api.summary`
        '''
        ),
    ('placeholder', '{messages}')
])
  • a list of tools
# for each api in openapi.json, 
# generate a tool in the following format,
    
'''
@tool
def `api.operationId` (`api.parameter list`) -> `api.response(HTTP 200)':
    `api.summary` as tool description

    response = requests call with `api.method` (`api.path`, `api.query`, `api.requestBody`)
    error handling from the listOf(`api.responses`)

...
tools = [
    the api.operationId list
]
'''
  • a class constructor to bind the prompt with a llm
def __init__(self, llm):
    self.runnable = MoviesApi.prompt | llm.bind_tools(MoviesApi.tools)
  • an invoker to process a tool call
def __call__(self, state, config: RunnableConfig):
    ...
    result = self.runnable.invoke(state)
    return {'messages': result}

Known Issues

  • Supporting more authentication methods - currently only bearer token is implemented here.
  • Not sure how to support a HTTP body with arrays or embedded objects - currently expect a flat body schema.

Contribution

This is an ongoing project, your contributions are extremely appreciated. Please create a Pull Request. 🍻🍻

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openapi2tools-0.3.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

openapi2tools-0.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file openapi2tools-0.3.tar.gz.

File metadata

  • Download URL: openapi2tools-0.3.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for openapi2tools-0.3.tar.gz
Algorithm Hash digest
SHA256 d6b0d73cb2f1816f734b04768711d6ce9a1e15f6ccb1406c40159716a3d4017f
MD5 24e3ce1e619c6935563da3a7416545b2
BLAKE2b-256 24b00e4a323bd5c9500b50d71d1e7c97432d9aff6fca2d2fcd03566683acbaea

See more details on using hashes here.

File details

Details for the file openapi2tools-0.3-py3-none-any.whl.

File metadata

  • Download URL: openapi2tools-0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for openapi2tools-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2eadde5391a9cc3c0e4bc1520a3a917022cecfa1584173e60e9406908386aacd
MD5 4dc0fe96813819f0404cebe1f72f51c2
BLAKE2b-256 365845d7b493095d32ef75bf0d29b06f6c398183729938827b917f77f93e44a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page