本人大学英语专业,对代码一窍不通,没学过。但是有时候闲着没事,想搞一个自己的非常简易的ai问答网站,现在已经有了第三方中转api,但是错误连连,求佬们帮助!感激不尽!
我让AI生成的代码:
目录
project/
app.py
templates/
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI 问答网站</title>
</head>
<body>
<h1>AI 问答网站</h1>
<input type="text" id="question" placeholder="请输入你的问题">
<button onclick="askQuestion()">提问</button>
<div id="answer"></div>
<script>
async function askQuestion() {
const question = document.getElementById('question').value;
const response = await fetch('/ask', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ question })
});
const data = await response.json();
document.getElementById('answer').innerText = data.answer;
}
</script>
</body>
</html>
from flask import Flask, request, jsonify, render_template
import requests
app = Flask(__name__)
# 你的API URL和API Key
API_URL = 'https://binary.hex.cloudns.be/v1/chat/completions'
API_KEY = 'sk-m1otgm4cNN...(保密,嘿嘿嘿)'
@app.route('/')
def home():
return render_template('index.html')
@app.route('/ask', methods=['POST'])
def ask():
data = request.json
question = data.get('question')
# 调用第三方API
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
payload = {
'model': 'gpt-4o',
'messages': [{'role': 'user', 'content': question}]
}
response = requests.post(API_URL, headers=headers, json=payload)
answer = response.json().get('choices', [{}])[0].get('message', {}).get('content', 'No answer available.')
return jsonify({'answer': answer})
if __name__ == '__main__':
app.run(debug=True)
以上是AI生成的全部代码,但出现了以下问题:
然后我就不知道怎么搞了,大佬勿喷,我真是0基础
[date-range from=2024-06-09T00:53:00 to=2024-06-09T00:54:00 timezone=“Asia/Shanghai”]后续:问题已解决,是那个api的问题,我换了个中转站,发现可以用了(代码本身没啥大问题)。
然后也是第一次发帖子,没想到这么多好心人回复我了,哈哈哈,感谢大家的解答