class_docstr: OpenAPIServiceToFunctions is responsible for converting an OpenAPI service specification into a format suitable
  for OpenAI function calling, based on the provided OpenAPI specification. Given an OpenAPI specification,
  OpenAPIServiceToFunctions processes it, and extracts function definitions that can be invoked via OpenAI's
  function calling mechanism. The format of the extracted functions is compatible with OpenAI's function calling
  JSON format.
  
  Minimal requirements for OpenAPI specification:
  - OpenAPI version 3.0.0 or higher
  - Each function must have a unique operationId
  - Each function must have a description
  - Each function must have a requestBody or parameters or both
  - Each function must have a schema for the requestBody and/or parameters
  
  
  See https://github.com/OAI/OpenAPI-Specification for more details on OpenAPI specification.
  See https://platform.openai.com/docs/guides/function-calling for more details on OpenAI function calling.
class_docstr_tok: ["OpenAPIServiceToFunctions","is","responsible","for","converting","an","OpenAPI","service","specification","into","a","format","suitable","for","OpenAI","function","calling",",","based","on","the","provided","OpenAPI","specification",".","Given","an","OpenAPI","specification",",","OpenAPIServiceToFunctions","processes","it",",","and","extracts","function","definitions","that","can","be","invoked","via","OpenAI's","function","calling","mechanism",".","The","format","of","the","extracted","functions","is","compatible","with","OpenAI","'s","function","calling","JSON","format",".","Minimal","requirements","for","OpenAPI","specification",":","-","OpenAPI","version","3.0.0","or","higher","-","Each","function","must","have","a","unique","operationId","-","Each","function","must","have","a","description","-","Each","function","must","have","a","requestBody","or","parameters","or","both","-","Each","function","must","have","a","schema","for","the","requestBody","and\/or","parameters","See","https",":","\/\/github.com\/OAI\/OpenAPI-Specification","for","more","details","on","OpenAPI","specification",".","See","https",":","\/\/platform.openai.com\/docs\/guides\/function-calling","for","more","details","on","OpenAI","function","calling","."]
docstr: Processes OpenAPI specification URLs or files to extract functions that can be invoked via OpenAI function
  calling mechanism. Each source is paired with a system message in one-to-one correspondence. The system message
  is used to assist LLM in the response generation.
  
  :param sources: A list of OpenAPI specification sources, which can be URLs, file paths, or ByteStream objects.
  :type sources: List[Union[str, Path, ByteStream]]
  :param system_messages: A list of system messages corresponding to each source.
  :type system_messages: List[str]
  :return: A dictionary with a key 'documents' containing a list of Document objects. Each Document object
           encapsulates a function definition and relevant metadata.
  :rtype: Dict[str, Any]
  :raises RuntimeError: If the OpenAPI specification cannot be downloaded or processed.
  :raises ValueError: If the source type is not recognized or no functions are found in the OpenAPI specification.
docstr_tok: ["Processes","OpenAPI","specification","URLs","or","files","to","extract","functions","that","can","be","invoked","via","OpenAI","function","calling","mechanism",".","Each","source","is","paired","with","a","system","message","in","one-to-one","correspondence",".","The","system","message","is","used","to","assist","LLM","in","the","response","generation",".",":","param","sources",":","A","list","of","OpenAPI","specification","sources",",","which","can","be","URLs",",","file","paths",",","or","ByteStream","objects",".",":","type","sources",":","List","[","Union","[","str",",","Path",",","ByteStream","]","]",":","param","system_messages",":","A","list","of","system","messages","corresponding","to","each","source",".",":","type","system_messages",":","List","[","str","]",":","return",":","A","dictionary","with","a","key","'documents","'","containing","a","list","of","Document","objects",".","Each","Document","object","encapsulates","a","function","definition","and","relevant","metadata",".",":","rtype",":","Dict","[","str",",","Any","]",":","raises","RuntimeError",":","If","the","OpenAPI","specification","can","not","be","downloaded","or","processed",".",":","raises","ValueError",":","If","the","source","type","is","not","recognized","or","no","functions","are","found","in","the","OpenAPI","specification","."]
code: def run(
      self,
      sources: List[Union[str, Path, ByteStream]],
      system_messages: List[str],
  ) -> Dict[str, Any]:
      """
      Processes OpenAPI specification URLs or files to extract functions that can be invoked via OpenAI function
      calling mechanism. Each source is paired with a system message in one-to-one correspondence. The system message
      is used to assist LLM in the response generation.
  
      :param sources: A list of OpenAPI specification sources, which can be URLs, file paths, or ByteStream objects.
      :type sources: List[Union[str, Path, ByteStream]]
      :param system_messages: A list of system messages corresponding to each source.
      :type system_messages: List[str]
      :return: A dictionary with a key 'documents' containing a list of Document objects. Each Document object
               encapsulates a function definition and relevant metadata.
      :rtype: Dict[str, Any]
      :raises RuntimeError: If the OpenAPI specification cannot be downloaded or processed.
      :raises ValueError: If the source type is not recognized or no functions are found in the OpenAPI specification.
      """
      documents: List[Document] = []
      for source, system_message in zip(sources, system_messages):
          openapi_spec_content = None
          if isinstance(source, (str, Path)):
              # check if the source is a file path or a URL
              if os.path.exists(source):
                  openapi_spec_content = self._read_from_file(source)
              else:
                  openapi_spec_content = self._read_from_url(
                      str(source)
                  )
          elif isinstance(source, ByteStream):
              openapi_spec_content = source.data.decode("utf-8")
          else:
              logger.warning(
                  "Invalid source type %s. Only str, Path, and ByteStream are supported.",
                  type(source),
              )
              continue
  
          if openapi_spec_content:
              try:
                  service_openapi_spec = self._parse_openapi_spec(
                      openapi_spec_content
                  )
                  functions: List[
                      Dict[str, Any]
                  ] = self._openapi_to_functions(service_openapi_spec)
                  docs = [
                      Document(
                          content=json.dumps(function),
                          meta={
                              "spec": service_openapi_spec,
                              "system_message": system_message,
                          },
                      )
                      for function in functions
                  ]
                  documents.extend(docs)
              except Exception as e:
                  logger.error(
                      "Error processing OpenAPI specification from source %s: %s",
                      source,
                      e,
                  )
  
      return {"documents": documents}
  
file_path: haystack/haystack/components/converters/openapi_functions.py
imports: import json
  import logging
  import os
  from pathlib import Path
  from typing import List, Dict, Any, Union, Optional
  import requests
  import yaml
  from requests import RequestException
  from haystack import component, Document
  from haystack.dataclasses.byte_stream import ByteStream
  from haystack.lazy_imports import LazyImport
label_desc: Use image node_id 2 for calling the OpenAPIServiceToFunctions obj's underlying member method code with example usage: obj.run(sources, system_messages) and returns: dict