python azure webApp,with openai

dhxwm5r4  于 2023-06-24  发布在  Python
关注(0)|答案(1)|浏览(101)

我做错了什么,我是新来的,我不知道是什么。
我使用的是一个基本模板,只是试图从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()

只是想得到一个开放的回应。

gmxoilav

gmxoilav1#

首先,我通过这个link创建了一个openai_API key,如下所示:

我试过这个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**文件运行成功,如下所示,

使用上面提到的URL,在Postman中得到结果。

输出:

使用上面提到的URL,在Postman中得到结果。

参考:

查看此link以了解有关将Python(Django或Flask)Web应用部署到Azure App Service的更多信息。

相关问题