Skip to content

Commit 6423bca

Browse files
committed
set up commands for using kiota to generate client codes
1 parent 9b5d63a commit 6423bca

File tree

8 files changed

+550
-19
lines changed

8 files changed

+550
-19
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ repos:
1313
rev: 24.2.0
1414
hooks:
1515
- id: black
16+
exclude: 'client/.*'

Makefile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ format: ## format code
3030
poetry run isort .
3131
poetry run black . --verbose
3232

33+
.PHONY: fix
34+
fix: format ## apply auto-fixes
35+
poetry run ruff check --fix
36+
3337
.PHONY: lint
3438
lint: ## lint
35-
poetry run ruff check .
39+
poetry run ruff check . \
40+
--exclude client \
41+
--respect-gitignore
3642
shellcheck scripts/*.sh
3743

3844
.PHONY: test
@@ -121,3 +127,14 @@ azure-functions-functionapp-deploy: ## deploy Azure Functions App
121127
.PHONY: generate-openapi-spec
122128
generate-openapi-spec: ## generate OpenAPI spec
123129
poetry run python main.py generate-openapi-spec
130+
131+
.PHONY: generate-openapi-client
132+
generate-openapi-client: ## generate OpenAPI client
133+
@kiota generate \
134+
--language Python \
135+
--class-name ApiClient \
136+
--namespace-name client \
137+
--openapi ./specs/openapi.json \
138+
--output ./client
139+
@echo "Get the list of dependencies"
140+
@kiota info -d ./specs/openapi.json --language Python

backend/fastapi.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import FastAPI
2+
from fastapi.openapi.utils import get_openapi
23

34
from backend.routers import azure_openai as azure_openai_router
45
from backend.routers import document_intelligence as document_intelligence_router
@@ -9,3 +10,29 @@
910

1011
app.include_router(azure_openai_router.router)
1112
app.include_router(document_intelligence_router.router)
13+
14+
15+
def custom_openapi():
16+
if app.openapi_schema:
17+
return app.openapi_schema
18+
openapi_schema = get_openapi(
19+
title="Azure AI Services Solutions",
20+
version="0.0.1",
21+
description="This contains a collection of solutions that leverage Azure AI services.",
22+
routes=app.routes,
23+
servers=[
24+
{
25+
"url": "http://localhost:8000",
26+
}
27+
],
28+
)
29+
openapi_schema["info"]["x-logo"] = {
30+
"url": "https://news.microsoft.com/wp-content/uploads/prod/2022/05/Microsoft-logo_rgb_c-gray-1024x459.png"
31+
}
32+
openapi_schema["openapi"] = "3.0.0"
33+
app.openapi_schema = openapi_schema
34+
return app.openapi_schema
35+
36+
37+
app.openapi_version = "3.0.0" # to use Kiota, downgrade to 3.0.0 (ref. https://github.com/microsoft/kiota/issues/3914)
38+
app.openapi = custom_openapi

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Common
44

55
- [Typer](https://typer.tiangolo.com/#installation)
6+
- [Kiota > Generate tailored Python and PHP API clients for any API with Kiota](https://devblogs.microsoft.com/microsoft365dev/generate-tailored-python-and-php-api-clients-for-any-api-with-kiota/)
67

78
## Backend
89

frontend/solutions/sandbox.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,37 @@
44

55
import streamlit as st
66

7-
from frontend.solutions.utilities import http_get, http_post
7+
from frontend.solutions.utilities import http_get
88

99
logger = logging.getLogger(__name__)
1010

1111

12+
async def chat_completions_post(
13+
backend_url: str,
14+
prompt: str,
15+
):
16+
from kiota_abstractions.authentication.anonymous_authentication_provider import AnonymousAuthenticationProvider
17+
from kiota_http.httpx_request_adapter import HttpxRequestAdapter
18+
19+
from client.api_client import ApiClient
20+
from client.models.chat_completion_request import ChatCompletionRequest
21+
22+
auth_provider = AnonymousAuthenticationProvider()
23+
request_adapter = HttpxRequestAdapter(
24+
authentication_provider=auth_provider,
25+
base_url=backend_url,
26+
)
27+
client = ApiClient(request_adapter)
28+
29+
response = await client.azure_openai.chat_completions.post(
30+
ChatCompletionRequest(
31+
content=prompt,
32+
stream=False,
33+
),
34+
)
35+
return response.content
36+
37+
1238
def start(
1339
backend_url: str,
1440
log_level: int,
@@ -42,12 +68,9 @@ def start(
4268
try:
4369
with st.spinner("Calling API..."):
4470
response = asyncio.run(
45-
http_post(
46-
url=urljoin(base=backend_url, url="/azure_openai/chat_completions/"),
47-
data={
48-
"content": prompt,
49-
"stream": False,
50-
},
71+
chat_completions_post(
72+
backend_url=backend_url,
73+
prompt=prompt,
5174
)
5275
)
5376
st.write(response)

0 commit comments

Comments
 (0)