semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Python, Error publishing it to Azure as a Rest API using FastAPI

Open Afsarsoft opened this issue 2 years ago • 1 comments

The first Python sample provided “Running a prompt” cause an error when publish it to Azure as a REST API using FastAPI.

Prerequisites in environment: Python 3.11.x (my exact version 3.11.2) FastAPI 0.95.1 (pip install fastapi & pip install uvicorn) pip install python-dotenv python -m pip install --upgrade semantic-kernel

1-Created main.py as below: from fastapi import FastAPI
from pydantic import BaseModel
from call_sk import process_ask

app = FastAPI()

class InputData(BaseModel):
ask: str

@app.get("/")
def home():
return {"Data": "Hello, you are in home page."}

@app.post("/ask_sk/")
async def ask_sk(input_data: InputData):
print(f"Ask: {input_data.ask}")
response: str = generate_response(input_data.ask)
return {"response": response}

def generate_response(ask: str) -> str:
processed_ask: str = process_ask(ask) response_msg: str = f"Response: {processed_ask}"
return response_msg

2-Create call_sk.py as follow: import semantic_kernel as sk from semantic_kernel.ai.open_ai import OpenAITextCompletion, AzureTextCompletion

kernel = sk.Kernel()

def process_ask(ask: str) -> str:
result: str = process_sk(ask) return result

def process_sk(ask: str) -> str:
deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
kernel.config.add_text_backend("dv", AzureTextCompletion(deployment, endpoint, api_key))

prompt: str = kernel.create_semantic_function(ask)  
result: str = prompt()  
return result

3-Add and update the following in .env OPENAI_API_KEY="" OPENAI_ORG_ID="" AZURE_OPENAI_DEPLOYMENT_NAME="" AZURE_OPENAI_ENDPOINT="" AZURE_OPENAI_API_KEY=""

4-Run and test the solution as follow: uvicorn main:app --reload

Expected behavior same as actual behavior. Works great as expected.

5-Create a requirements.txt as follow pip freeze -- local > requirements.txt

6-Make sure main.py, call_sk.py, and requirements.txt files in you root folder

7-Publish it to GitHub

8-In Azure, create a web app.

9-In Deployment Center, settings, please use GitHub as source to connect to the project

10-Make sure in Configuration, General Setting is as follow: Stack = Python Major version = Python 3 Minor Version = Python 3.11 Startup Command = uvicorn main:app --host 0.0.0.0 --port 8000

11-After the deployment, wait and make sure everything goes well with no issues in Configuration, Logs

12- Browse the site

13-Expected behavior to see the site as you tested locally. However, errors out!

14- to fix the error, change the call_sk.py as follow: def process_ask(ask: str) -> str:
result: str = "This is a test" return result

15-Run and test the solution locally as follow: uvicorn main:app --reload

16-Create a requirements.txt as follow pip freeze -- local > requirements.txt

17- commit the changes and publish to GitHub.

18-the deployment should start automatically

19-After deployment finished and no issues in Configuration, Logs

20-Browse the site

Expected result same as actual result. Same OK behavior as tested locally.

So, based on the above steps looks like there is an issue with Azure in installing components in requirements.txt related to SK!

Afsarsoft avatar Apr 20 '23 18:04 Afsarsoft

@mkarle can you take a look at this?

evchaki avatar Apr 20 '23 20:04 evchaki

Rebuilt and reinstalled. Looks like python -m pip install --upgrade semantic-kernel fixed it. Looks good. Please close.

Afsarsoft avatar May 02 '23 23:05 Afsarsoft