使用Azure DevOps分别在Azure Function App和Azure App上部署Python API和Python Flask应用程序

7gyucuyw  于 2023-05-18  发布在  Python
关注(0)|答案(1)|浏览(136)

我有一个python azure functions应用程序通过azure devops部署在API_env虚拟环境中。同样,我有一个python flask应用程序。python后端和typescript前端。python后端是通过激活一个名为backend_env的不同虚拟环境来部署的。应用程序后端代码调用API,即它被部署为Azure功能。然而,一些使用的包,如openai,langchain等是在requirements.txt和API_env文件夹在azure函数应用程序,当我通过调试控制台查看它时,azure函数抛出模块未找到错误。我尝试了各种选择,即。除了通过devops管道激活API_env之外,我还在config设置中添加了该设置,但似乎没有什么帮助。有人能给我指个方向吗?
`YAML代码步骤:- 脚本:|python3.9 -m venv API_env source api_env/bin/activate pip install --upgrade setuptools pip3.9 install -r requirements.txt工作目录:$(Build.SourcesDirectory)/API/python displayName:'为API安装Python依赖项'

- 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'`

还尝试在函数init中添加以下内容。py sys.path.append('/home/site/wwwroot/api_env/lib/python3.9/site-packages')
我期待API调用成功。

oalqel3c

oalqel3c1#

我尝试使用requirements.txt中的Openai模块在Azure Devops中部署具有虚拟环境的Function应用程序,并取得了成功,参考如下:
我的init.py代码:-

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)

我的function.json代码:-

{
    "bindings": [
        {
            "authLevel": "anonymous",
            "type": "httpTrigger",
            "direction": "in",
            "name": "req",
            "methods": [
                "get"
            ]
        },
        {
            "type": "http",
            "direction": "out",
            "name": "$return"
        }
    ],
    "scriptFile": "__init__.py",
    "entryPoint": "main"
}

我的主机。json:-

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  }
}

我的要求. txt:-

azure-functions
openai

我的YAML管道脚本:-

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'

检查虚拟环境API_env是否在Function应用启动之前启动。在你的YAML代码中,你在安装依赖项之前运行虚拟环境,试着在你的函数开始之前启动你的虚拟环境,如下所示:

startUpCommand: |
  source api_env/bin/activate
  func start --python

此外,验证虚拟环境的位置,您可以输入此命令以检查下面的系统路径:-

import sys
print(sys.path)

添加虚拟环境的完整路径,同时使用以下代码启动它:-

startUpCommand: |
  source /path/to/api_env/bin/activate
  func start --python

相关问题