-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix pydantic name annotated is not defined main (#364)
* Implement patch for same schema in parameters issues * Add schema test scenario * Rebuild docs * Fix template codegen test
- Loading branch information
Showing
9 changed files
with
260 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
11 changes: 11 additions & 0 deletions
11
...n/api/fastagency/api/openapi/patch_fastapi_code_generator/patch_parse_schema.md
This file contains 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,11 @@ | ||
--- | ||
# 0.5 - API | ||
# 2 - Release | ||
# 3 - Contributing | ||
# 5 - Template Page | ||
# 10 - Default | ||
search: | ||
boost: 0.5 | ||
--- | ||
|
||
::: fastagency.api.openapi.patch_fastapi_code_generator.patch_parse_schema |
This file contains 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
This file contains 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,22 @@ | ||
from functools import wraps | ||
from typing import Any | ||
|
||
from fastapi_code_generator.parser import OpenAPIParser | ||
|
||
from ...logging import get_logger | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
def patch_parse_schema() -> None: | ||
org_parse_schema = OpenAPIParser.parse_schema | ||
|
||
@wraps(org_parse_schema) | ||
def my_parse_schema(*args: Any, **kwargs: Any) -> Any: | ||
data_type = org_parse_schema(*args, **kwargs) | ||
if data_type.reference and data_type.reference.duplicate_name: | ||
data_type.reference.name = data_type.reference.duplicate_name | ||
return data_type | ||
|
||
OpenAPIParser.parse_schema = my_parse_schema | ||
logger.info("Patched OpenAPIParser.parse_schema") |
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "FastAPI", | ||
"title": "openapi", | ||
"version": "0.1.0" | ||
}, | ||
"servers": [ | ||
|
This file contains 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
88 changes: 88 additions & 0 deletions
88
tests/api/openapi/templates/same_schema_in_parameters.json
This file contains 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,88 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "same_schema_in_parameters", | ||
"version": "0.1.0" | ||
}, | ||
"servers": [ | ||
{ | ||
"url": "http://127.0.0.1:50369", | ||
"description": "Local development server" | ||
} | ||
], | ||
"paths": { | ||
"/get-sheet": { | ||
"get": { | ||
"summary": "Get Sheet", | ||
"description": "Get data from a Google Sheet", | ||
"operationId": "get_sheet_get_sheet_get", | ||
"parameters": [ | ||
{ | ||
"name": "spreadsheet_id", | ||
"in": "query", | ||
"required": false, | ||
"schema": { | ||
"anyOf": [ | ||
{ | ||
"type": "string" | ||
} | ||
], | ||
"title": "Spreadsheet Id" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "string", | ||
"title": "Response Get Sheet Get Sheet Get" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"/update-sheet": { | ||
"post": { | ||
"summary": "Update Sheet", | ||
"description": "Update data in a Google Sheet within the existing spreadsheet", | ||
"operationId": "update_sheet_update_sheet_post", | ||
"parameters": [ | ||
{ | ||
"name": "spreadsheet_id", | ||
"in": "query", | ||
"required": false, | ||
"schema": { | ||
"anyOf": [ | ||
{ | ||
"type": "string" | ||
} | ||
], | ||
"title": "Spreadsheet Id" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "string", | ||
"title": "Response Update Sheet Update Sheet Post" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": {} | ||
} | ||
} | ||
} |
This file contains 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
113 changes: 113 additions & 0 deletions
113
tests/api/openapi/test_openapi_same_schema_in_parameters.py
This file contains 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,113 @@ | ||
from typing import Annotated, Any, Optional | ||
|
||
import pytest | ||
from autogen.agentchat import ConversableAgent | ||
from fastapi import FastAPI | ||
|
||
|
||
def create_app_with_two_enpoints_same_datamodel(host: str, port: str) -> FastAPI: | ||
app = FastAPI( | ||
title="test", | ||
servers=[ | ||
{"url": f"http://{host}:{port}", "description": "Local development server"} | ||
], | ||
) | ||
|
||
@app.get("/get-sheet", description="Get data from a Google Sheet") | ||
async def get_sheet( | ||
spreadsheet_id: Annotated[ | ||
Optional[str], "ID of the Google Sheet to fetch data from" | ||
] = None, | ||
) -> str: | ||
return "sheet retrieved" | ||
|
||
@app.post( | ||
"/update-sheet", | ||
description="Update data in a Google Sheet within the existing spreadsheet", | ||
) | ||
async def update_sheet( | ||
spreadsheet_id: Annotated[ | ||
Optional[str], "ID of the Google Sheet to fetch data from" | ||
] = None, | ||
) -> str: | ||
return "Updated" | ||
|
||
return app | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"fastapi_openapi_url", | ||
[(create_app_with_two_enpoints_same_datamodel)], | ||
indirect=["fastapi_openapi_url"], | ||
) | ||
def test_openapi_same_schema_in_parameters_register_for_llm( | ||
fastapi_openapi_url: str, | ||
azure_gpt35_turbo_16k_llm_config: dict[str, Any], | ||
) -> None: | ||
from fastagency.api.openapi import OpenAPI | ||
|
||
api_client = OpenAPI.create( | ||
openapi_url=fastapi_openapi_url, | ||
) | ||
agent = ConversableAgent(name="agent", llm_config=azure_gpt35_turbo_16k_llm_config) | ||
api_client._register_for_llm(agent) | ||
tools = agent.llm_config["tools"] | ||
|
||
expected = [ | ||
{ | ||
"type": "function", | ||
"function": { | ||
"description": "Get data from a Google Sheet", | ||
"name": "get_sheet_get_sheet_get", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"spreadsheet_id": { | ||
"$defs": { | ||
"SpreadsheetId": { | ||
"anyOf": [{"type": "string"}, {"type": "null"}], | ||
"title": "SpreadsheetId", | ||
} | ||
}, | ||
"anyOf": [ | ||
{"$ref": "#/$defs/SpreadsheetId"}, | ||
{"type": "null"}, | ||
], | ||
"default": None, | ||
"description": "spreadsheet_id", | ||
} | ||
}, | ||
"required": [], | ||
}, | ||
}, | ||
}, | ||
{ | ||
"type": "function", | ||
"function": { | ||
"description": "Update data in a Google Sheet within the existing spreadsheet", | ||
"name": "update_sheet_update_sheet_post", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"spreadsheet_id": { | ||
"$defs": { | ||
"SpreadsheetId": { | ||
"anyOf": [{"type": "string"}, {"type": "null"}], | ||
"title": "SpreadsheetId", | ||
} | ||
}, | ||
"anyOf": [ | ||
{"$ref": "#/$defs/SpreadsheetId"}, | ||
{"type": "null"}, | ||
], | ||
"default": None, | ||
"description": "spreadsheet_id", | ||
} | ||
}, | ||
"required": [], | ||
}, | ||
}, | ||
}, | ||
] | ||
|
||
assert tools == expected, tools |