python azure webApp,带有 openai

标签 python azure

我做错了什么,我是新人,我不确定是什么。

我使用这个基本模板,只是尝试从 openai 获取响应并显示它。

https://learn.microsoft.com/en-us/azure/app-service/quickstart-python?tabs=flask%2Cmac-linux%2Cvscode-aztools%2Cvscode-deploy%2Cdeploy-instructions-azportal%2Cterminal-bash%2Cdeploy-instructions-zip-azcli

我认为我的缩进困惑了,但我不确定。

我在第 27 行收到有关缩进的错误。

openai.api_type = "azure"是出现错误的行。

import os
import openai

from flask import (Flask, redirect, render_template, request,
                   send_from_directory, url_for)

app = Flask(__name__)


@app.route('/')
def index():
   print('Request for index page received')
   return render_template('index.html')

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'),
                               'favicon.ico', mimetype='image/vnd.microsoft.icon')

@app.route('/hello', methods=['POST'])
def hello():
   name = request.form.get('name')
   openai.api_type = "azure"
   openai.api_base = "https://itsavvy.openai.azure.com/"
   openai.api_version = "2022-12-01"
   openai.api_key = os.getenv("jjjj")


   response = openai.Completion.create(
     engine="ITsavvy",
     prompt=name,
     temperature=1,
     max_tokens=100,
     top_p=0.5,
     frequency_penalty=0,
     presence_penalty=0,
     stop=None)





   if name:
       print('Request for hello page received with name=%s' % response)
       return render_template('hello.html', name = response)
   else:
       print('Request for hello page received with no name or blank name -- redirecting')
       return redirect(url_for('index'))


if __name__ == '__main__':
   app.run()

只是想得到 openai 的回复。

最佳答案

首先,我通过这个link创建了一个openai_Api key如下,

enter image description here

我试过这个Github获取openai响应的代码。

代码:

<强> test.py :

import os
import openai
from flask import Flask, redirect, render_template, request, send_from_directory, url_for

app = Flask(__name__)

@app.route('/')
def index():
    print('Request for index page received')
    return render_template('index.html')

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')

@app.route('/hello', methods=['POST'])
def hello():
    name = request.form.get('name')
    openai.api_key = "YOUR_API_KEY"

    response = openai.Completion.create(
        engine="davinci",
        prompt=name,
        temperature=1,
        max_tokens=100,
        top_p=0.5,
        frequency_penalty=0,
        presence_penalty=0,
        stop=None
    )

    if name:
        print('Request page with name=%s' % response)
        return render_template('hello.html', name=response)
    else:
        print('Request page with no name -- redirecting')
        return redirect(url_for('index'))

if __name__ == '__main__':
    app.run()

index.html:

<!DOCTYPE html>
<html>
<head>
  <title>My Flask Application</title>
</head>
<body>
  <h1>Welcome to My Flask Application</h1>
  <p>This is the index page.</p>
</body>
</html>

hello.html:

<!doctype html>
<html>
<head>
    <title>Hello Page</title>
</head>
<body>
    <h1>Hello, {{ name }}</h1>
</body>
</html>

输出:

<强> test.py 文件成功运行如下,

enter image description here

通过上述URL,在Postman中得到结果。

enter image description here

输出:

enter image description here

通过上述URL,在Postman中得到结果。

enter image description here

引用:

检查这个link了解有关将 Python(Django 或 Flask)Web 应用部署到 Azure 应用服务的更多信息。

关于python azure webApp,带有 openai,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76382230/

相关文章:

python - 如何使用 numpy 优化数组上的双循环?

azure - 我可以将 Azure Kubernetes 节点连接到我的本地网络吗?

azure - 从 Azure AD OAuth v2 token 端点获取 client_credentials token 时,sub 和 oid 声明中包含什么内容?

.net - 删除 Assets 和转换作业(Azure 媒体服务 v3)

azure - 如何在 Azure DataLake 中合并基本结构流和多个增量结构流

azure - 以编程方式配置 Azure 缓存客户端

python - 削波 FFT 矩阵

python - 如何删除 Django 1.7 中的现有迁移

python - Telnet 到 Python 服务器卡住

Python线程,线程不关闭