mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> This allows us to import OpenAPI documents, including ChatGPT plugins, into the Semantic Kernel. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - The interface reads the operationIds of the openapi spec into a skill: ```python from semantic_kernel.connectors.openapi import register_openapi_skill skill = register_openapi_skill(kernel=kernel, skill_name="test", openapi_document="url/or/path/to/openapi.yamlorjson") skill['operationId'].invoke_async() ``` - Parse an OpenAPI document - For each operation in the document, create a function that will execute the operation - Add all those operations to a skill in the kernel - Modified `import_skill` to accept a dictionary of functions instead of just class so that we can import dynamically created functions - Created unit tests TESTING: I've been testing this with the following ChatGPT plugins: - [Semantic Kernel Starter's Python Flask plugin](https://github.com/microsoft/semantic-kernel-starters/tree/main/sk-python-flask) - [ChatGPT's example retrieval plugin](https://github.com/openai/chatgpt-retrieval-plugin/blob/main/docs/providers/azuresearch/setup.md) - This one was annoying to setup. I didn't get the plugin functioning, but I was able to send the right API requests - Also, their openapi file was invalid. The "servers" attribute is misindented - [Google ChatGPT plugin](https://github.com/Sogody/google-chatgpt-plugin) - [Chat TODO plugin](https://github.com/lencx/chat-todo-plugin) - This openapi file is also invalid. I checked with an online validator. I had to remove"required" from the referenced request objects' properties: https://github.com/lencx/chat-todo-plugin/blob/main/openapi.yaml#L85 Then I used this python file to test the examples: ```python import asyncio import logging import semantic_kernel as sk from semantic_kernel import ContextVariables, Kernel from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion from semantic_kernel.connectors.openapi.sk_openapi import register_openapi_skill # Example usage chatgpt_retrieval_plugin = { "openapi": # location of the plugin's openapi.yaml file, "payload": { "queries": [ { "query": "string", "filter": { "document_id": "string", "source": "email", "source_id": "string", "author": "string", "start_date": "string", "end_date": "string", }, "top_k": 3, } ] }, "operation_id": "query_query_post", } sk_python_flask = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"skill_name": "FunSkill", "function_name": "Joke"}, "payload": {"input": "dinosaurs"}, "operation_id": "executeFunction", } google_chatgpt_plugin = { "openapi": # location of the plugin's openapi.yaml file, "query_params": {"q": "dinosaurs"}, "operation_id": "searchGet", } todo_plugin_add = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"username": "markkarle"}, "payload": {"todo": "finish this"}, "operation_id": "addTodo", } todo_plugin_get = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"username": "markkarle"}, "operation_id": "getTodos", } todo_plugin_delete = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"username": "markkarle"}, "payload": {"todo_idx": 0}, "operation_id": "deleteTodo", } plugin = todo_plugin_get # set this to the plugin you want to try logger = logging.getLogger(__name__) logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) kernel = Kernel(log=logger) deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env() kernel.add_text_completion_service( "dv", AzureTextCompletion(deployment, endpoint, api_key) ) skill = register_openapi_skill( kernel=kernel, skill_name="test", openapi_document=plugin["openapi"] ) context_variables = ContextVariables(variables=plugin) result = asyncio.run( skill[plugin["operation_id"]].invoke_async(variables=context_variables) ) print(result) ``` ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: Abby Harrison <[email protected]>
25 lines
540 B
Python
25 lines
540 B
Python
from aiohttp import web
|
|
|
|
"""Server"""
|
|
routes = web.RouteTableDef()
|
|
|
|
|
|
@routes.post("/{name}")
|
|
async def hello(request):
|
|
# Get path parameters
|
|
name = request.match_info.get("name", "")
|
|
# Get query parameters
|
|
q = request.rel_url.query.get("q", "")
|
|
# Get body
|
|
body = await request.json()
|
|
# Get headers
|
|
headers = request.headers
|
|
return web.Response(text=f"Hello, {name}: q={q}, body={body}, headers={headers}")
|
|
|
|
|
|
app = web.Application()
|
|
app.add_routes(routes)
|
|
|
|
if __name__ == "__main__":
|
|
web.run_app(app)
|