-
Notifications
You must be signed in to change notification settings - Fork 7
Feature/openapi automation #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0x090909
wants to merge
6
commits into
Pipelex:dev
Choose a base branch
from
0x090909:feature/openapi_automation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
010896e
adds openapi automation example
0x090909 0ecd37b
adds openapi automation dependency
0x090909 18e44a1
fixes lint errors and formatting errors
0x090909 b7415d7
Merge branch 'dev' into feature/openapi_automation
0x090909 ef61e3a
fixes the merge-check-pyright by adding the necessary types to the va…
0x090909 3d32823
Merge remote-tracking branch 'origin/feature/openapi_automation' into…
0x090909 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| from pipelex.core.stuffs.structured_content import StructuredContent | ||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class FunctionParameter(StructuredContent): | ||
| name: str = Field(description="parameter name") | ||
| value: str = Field(description="parameter value") | ||
| type: str = Field(description="parameter type") | ||
|
|
||
|
|
||
| # OpenAPI Specification Models | ||
| class OpenAPIParameter(BaseModel): | ||
| name: str | ||
| in_: str = Field(alias="in") | ||
| required: Optional[bool] = False | ||
| description: Optional[str] = None | ||
| schema_: Optional[Dict[str, Any]] = Field(default=None, alias="schema") | ||
|
|
||
|
|
||
| class OpenAPIRequestBody(BaseModel): | ||
| description: Optional[str] = None | ||
| required: Optional[bool] = False | ||
| content: Optional[Dict[str, Any]] = None | ||
|
|
||
|
|
||
| class OpenAPIResponse(BaseModel): | ||
| description: Optional[str] = None | ||
| content: Optional[Dict[str, Any]] = None | ||
|
|
||
|
|
||
| class OpenAPIOperation(BaseModel): | ||
| operationId: Optional[str] = None | ||
| summary: Optional[str] = None | ||
| description: Optional[str] = None | ||
| parameters: Optional[List[OpenAPIParameter]] = None | ||
| requestBody: Optional[OpenAPIRequestBody] = None | ||
| responses: Optional[Dict[str, OpenAPIResponse]] = None | ||
| tags: Optional[List[str]] = None | ||
|
|
||
|
|
||
| class OpenAPIPathItem(BaseModel): | ||
| get: Optional[OpenAPIOperation] = None | ||
| post: Optional[OpenAPIOperation] = None | ||
| put: Optional[OpenAPIOperation] = None | ||
| delete: Optional[OpenAPIOperation] = None | ||
| patch: Optional[OpenAPIOperation] = None | ||
| options: Optional[OpenAPIOperation] = None | ||
| head: Optional[OpenAPIOperation] = None | ||
|
|
||
|
|
||
| class OpenAPIInfo(BaseModel): | ||
| title: str | ||
| version: str | ||
| description: Optional[str] = None | ||
|
|
||
|
|
||
| class OpenAPISpec(StructuredContent): | ||
| openapi: str = Field(description="OpenAPI version") | ||
| info: OpenAPIInfo = Field(description="API metadata") | ||
| paths: Dict[str, OpenAPIPathItem] = Field(description="API endpoints") | ||
| components: Optional[Dict[str, Any]] = Field(default=None, description="Reusable components") | ||
| servers: Optional[List[Dict[str, Any]]] = Field(default=None, description="API servers") | ||
|
|
||
|
|
||
| class FunctionInfo(StructuredContent): | ||
| function_name: str = Field(description="The operation ID / function name") | ||
| description: Optional[str] = Field(default=None, description="Function description") | ||
|
|
||
|
|
||
| class FunctionChoice(StructuredContent): | ||
| explanation: str = Field(description="Explanation of the choice.") | ||
| function_name: str = Field(description="Name of the function.") | ||
|
|
||
|
|
||
| class ParameterDetail(StructuredContent): | ||
| """Detailed parameter information for API calls""" | ||
|
|
||
| name: str = Field(description="Parameter name") | ||
| param_in: str = Field(description="Where the parameter goes: path, query, header, cookie") | ||
| required: bool = Field(default=False, description="Whether the parameter is required") | ||
| param_type: Optional[str] = Field(default=None, description="Parameter data type") | ||
| description: Optional[str] = Field(default=None, description="Parameter description") | ||
| default: Optional[Any] = Field(default=None, description="Default value if any") | ||
|
|
||
|
|
||
| class FunctionDetails(StructuredContent): | ||
| """Complete details needed to make an API request""" | ||
|
|
||
| function_name: str = Field(description="The operation ID / function name") | ||
| http_method: str = Field(description="HTTP method (GET, POST, PUT, DELETE, etc.)") | ||
| path: str = Field(description="API endpoint path") | ||
| description: Optional[str] = Field(default=None, description="Operation description") | ||
| parameters: List[ParameterDetail] = Field(default_factory=list, description="List of parameters") # type: ignore[reportUnknownVariableType] | ||
| request_body_required: bool = Field(default=False, description="Whether a request body is required") | ||
| request_body_schema: Optional[Dict[str, Any]] = Field(default=None, description="Request body schema if applicable") | ||
| tags: Optional[List[str]] = Field(default=None, description="Operation tags") | ||
|
|
||
|
|
||
| class RequestDetails(StructuredContent): | ||
| """Holds the actual parameter values needed to make an API request""" | ||
|
|
||
| function_name: str = Field(description="The operation ID / function name") | ||
| http_method: str = Field(description="HTTP method (GET, POST, PUT, DELETE, etc.)") | ||
| path: str = Field(description="API endpoint path") | ||
| query_parameters: Optional[Dict[str, Any]] = Field(default=None, description="Query parameters and their values") | ||
| path_parameters: Optional[Dict[str, Any]] = Field(default=None, description="Path parameters and their values") | ||
| header_parameters: Optional[Dict[str, Any]] = Field(default=None, description="Header parameters and their values") | ||
| cookie_parameters: Optional[Dict[str, Any]] = Field(default=None, description="Cookie parameters and their values") | ||
| request_body: Optional[Dict[str, Any]] = Field(default=None, description="Request body data if applicable") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| domain = "openapi_automation" | ||
| description = "Building function info from OpenAPI JSON spec" | ||
| main_pipe = "build_function_info" | ||
|
|
||
| [concept] | ||
| OpenAPISpec = "Structured OpenAPI specification for the backend." | ||
| FunctionInfo = "Information about a function in the OpenAPI spec." | ||
| FunctionChoice = "Choice of OpenAPI function to accomplish an operation." | ||
| RequestDetails = "Request Details containing actual values for the request" | ||
|
|
||
| [concept.OpenAPIURL] | ||
| description = "The URL of the OpenAPI JSON spec" | ||
| refines = "Text" | ||
|
|
||
| [concept.OperationToAccomplish] | ||
| description = "The specific operation to accomplish." | ||
| refines = "Text" | ||
|
|
||
| [concept.RelevantOpenapiPaths] | ||
| description = """ | ||
| Relevant information (e.g., paths, methods) from the OpenAPI JSON specification that pertains to the operation to accomplish. | ||
| """ | ||
|
|
||
| [concept.RelevantOpenapiPaths.structure] | ||
| paths = { type = "text", description = "List of relevant paths.", required = true } | ||
| methods = { type = "text", description = "List of relevant methods.", required = true } | ||
|
|
||
| [concept.FunctionName] | ||
| description = "The name of the function." | ||
| refines = "Text" | ||
|
|
||
| [concept.FunctionParameter] | ||
| description = "The necessary function parameters." | ||
|
|
||
| [concept.FunctionParameter.structure] | ||
| name = { type = "text", description = "Name of a function parameter.", required = true } | ||
| type = { type = "text", description = "Data type of a function parameter.", required = true } | ||
| value = { type = "text", description = "Values of each function parameter.", required = true } | ||
|
|
||
| [concept.ApiResponseResult] | ||
| description = "The compiled api response content." | ||
|
|
||
| [concept.ApiResponseResult.structure] | ||
| response = { type = "text", description = "The response of the api server", required = true } | ||
|
|
||
|
|
||
| [pipe.build_function_info] | ||
| type = "PipeSequence" | ||
| description = """ | ||
| Main pipeline that builds the function name and function parameters and values necessary for the task based on the OpenAPI JSON spec and the operation to accomplish. | ||
| """ | ||
| inputs = { openapi_url = "OpenAPIURL", operation_to_accomplish = "OperationToAccomplish" } | ||
| output = "ApiResponseResult" | ||
| #output = "FunctionDetails" | ||
| steps = [ | ||
| { pipe = "obtain_api_spec", result = "openapi_spec"}, | ||
| { pipe = "extract_available_functions", result = "function_info" }, | ||
| { pipe = "choose_function", result = "function_choice" }, | ||
| { pipe = "get_function_details", result = "function_details" }, | ||
| { pipe = "prepare_request", result = "request_details"}, | ||
| { pipe = "execute_api_call", result = "result_api_call" }, | ||
| ] | ||
|
|
||
| [pipe.obtain_api_spec] | ||
| type = "PipeFunc" | ||
| description = "Obtains the OpenAPI spec given a URL." | ||
| inputs = { openapi_url = "OpenAPIURL" } | ||
| output = "OpenAPISpec" | ||
| function_name = "obtain_openapi_model" | ||
|
|
||
| [pipe.extract_available_functions] | ||
| type = "PipeFunc" | ||
| description = "Extracts the available functions from the OpenAPI spec." | ||
| inputs = { openapi_url = "OpenAPIURL" } | ||
| output = "FunctionInfo[]" | ||
| function_name = "extract_available_functions" | ||
|
|
||
|
|
||
| [pipe.choose_function] | ||
| type = "PipeLLM" | ||
| description = "Uses the operation to accomplish and relevant OpenAPI paths to determine the function name." | ||
| inputs = { operation_to_accomplish = "OperationToAccomplish", function_info = "FunctionInfo[]" } | ||
| output = "FunctionChoice" | ||
| model = "llm_to_engineer" | ||
| system_prompt = """ | ||
| Determine a function name based on the operation to accomplish and relevant OpenAPI paths. Be concise. | ||
| """ | ||
| prompt = """ | ||
| Based on the operation to accomplish and the available OpenAPI functions, choose the relevant function name. | ||
|
|
||
| @operation_to_accomplish | ||
|
|
||
| @function_info | ||
| """ | ||
|
|
||
| [pipe.get_function_details] | ||
| type = "PipeFunc" | ||
| description = "Gets the details of a function from the OpenAPI spec." | ||
| inputs = { function_choice = "FunctionChoice" } | ||
| output = "FunctionDetails" | ||
| function_name = "get_function_details" | ||
|
|
||
|
|
||
|
|
||
| [pipe.prepare_request] | ||
| type = "PipeLLM" | ||
| description = "Prepares the request body corresponding to the actual request" | ||
| inputs = { operation_to_accomplish = "OperationToAccomplish", function_details = "FunctionDetails" } | ||
| output = "RequestDetails" | ||
| model = "llm_to_engineer" | ||
| prompt = """ | ||
| Based on the operation to accomplish and the available OpenAPI functions, fill in the actual values for the current request. | ||
|
|
||
| @operation_to_accomplish | ||
|
|
||
| @function_details | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| [pipe.execute_api_call] | ||
| type = "PipeFunc" | ||
| description = "Execute the API request given a CompiledFunctionInfo." | ||
| inputs = { request_details = "RequestDetails" } | ||
| output = "ApiResponseResult" | ||
| function_name = "invoke_function_api_backend" | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import asyncio | ||
|
|
||
| from pipelex.pipelex import Pipelex | ||
| from pipelex.pipeline.execute import execute_pipeline | ||
|
|
||
| spec_url_1 = """ | ||
| https://developer.keap.com/docs/rest/2025-11-05-v1.json | ||
| """ | ||
|
|
||
|
|
||
| spec_url_2 = """ | ||
| https://petstore3.swagger.io/api/v3/openapi.json | ||
| """ | ||
|
|
||
| USE_CASE_1 = (spec_url_1, "extract all the contacts with email [email protected]") | ||
| USE_CASE_2 = (spec_url_2, "get me the user with username: johndoe") | ||
|
|
||
|
|
||
| async def run_build_function_info(use_case: tuple[str, str]): | ||
| spec_url, operation_to_accomplish = use_case | ||
| return await execute_pipeline( | ||
| pipe_code="build_function_info", | ||
| inputs={ | ||
| "openapi_url": { | ||
| "concept": "openapi_function_builder.OpenAPIURL", | ||
| "content": spec_url, | ||
| }, | ||
| "operation_to_accomplish": { | ||
| "concept": "openapi_function_builder.OperationToAccomplish", | ||
| "content": operation_to_accomplish, | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Initialize Pipelex | ||
| Pipelex.make() | ||
|
|
||
| # Run the pipeline | ||
| result = asyncio.run(run_build_function_info(use_case=USE_CASE_2)) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can clean up this dead code