我做错了什么,我是新人,我不确定是什么。
我使用这个基本模板,只是尝试从 openai 获取响应并显示它。
我认为我的缩进困惑了,但我不确定。
我在第 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如下,
我试过这个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 应用服务的更多信息。
关于python azure webApp,带有 openai,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76382230/