Reputation: 1
I have a python azure functions app deployed via azure devops in an api_env virtual environment. Similarly, I have a python flask app i.e. python backend and typescript frontend. The python backend is deployed by activate a different virtual environment called backend_env. The application backend code calls an api i.e. that was deployed as azure function. However, some of the packages used such as openai, langchain etc are in requirements.txt and are in the api_env folder on the azure function app when i look at it via debug console but the azure functions is throwing module not found error for these packages. I tried various options i.e. in addition to activating the api_env via the devops pipeline, I also added the setting in the config setting but nothing seems to help. Can anyone point me in the right direction?
`YAML code steps: - script: | python3.9 -m venv api_env source api_env/bin/activate pip install --upgrade setuptools pip3.9 install -r requirements.txt workingDirectory: $(Build.SourcesDirectory)/api/python displayName: 'Install Python dependencies for api'
- task: AzureFunctionApp@1
inputs:
azureSubscription: 'spn'
appType: 'functionAppLinux'
appName: '$(knowbot-funcapp-name)'
package: '$(System.DefaultWorkingDirectory)/api/python'
startUpCommand: |
source api_env/bin/activate
func start --python
workingDirectory: '$(System.DefaultWorkingDirectory)/api/python'
displayName: 'Deploy Function App'`
Also tried adding the below in the function init.py
sys.path.append('/home/site/wwwroot/api_env/lib/python3.9/site-packages')
I am expecting the api call to succeed.
Upvotes: 0
Views: 848
Reputation: 8157
I tried to deploy a Function app with virtual environment in Azure Devops using Openai module in requirements.txt and it was successful, Refer below:-
My init.py code:-
import openai
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
# Set up OpenAI API key
openai.api_key = "sk-8Zg7i4icv8gh60e3IhnaT3Bxxxxx"
# Use the OpenAI API to generate some text
prompt = "Hello, OpenAI!"
response = openai.Completion.create(engine="text-davinci-002", prompt=prompt)
# Log the response
logging.info(response)
# Return the response as an HTTP response
return func.HttpResponse(response.choices[0].text)
My function.json code:-
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
],
"scriptFile": "__init__.py",
"entryPoint": "main"
}
My host.json:-
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
}
}
My requirements.txt :-
azure-functions
openai
My YAML Pipeline script:-
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.9'
addToPath: true
- script: |
python3 -m venv env
source env/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Install Python dependencies'
- task: AzureFunctionApp@1
inputs:
azureSubscription: '<Subscription>'
appType: 'functionAppLinux'
appName: 'siliconfunc32'
package: '$(System.DefaultWorkingDirectory)'
startUpCommand: 'func start --python'
Check if your virtual environment api_env starts before your Function app is started. In your YAML code you're running virtual environment before your dependencies are installed try initiating your virtual environment right before your Function starts like below:-
startUpCommand: |
source api_env/bin/activate
func start --python
Also, Validate where your virtual environment is located you can enter this command to check your system path below:-
import sys
print(sys.path)
Add full path of your virtual environment while initiating it with the code below:-
startUpCommand: |
source /path/to/api_env/bin/activate
func start --python
Upvotes: 0