ChatGPT是美国人工智能研究公司OpenAI开发的智能聊天机器人。这套AI系统瞬间秒杀市面上所有AI功能,几乎完美突破了以往所有同类软件的瓶颈。它可以走向人工智能技术,更加自然和精致。实现各种语言的处理。 ChatGPT官方网站: 什...
Tag:ChatGPT网页版登录入口11月26日
import openai
from flask import Flask, request, jsonify
app = Flask(__name__)
openai.api_key = "YOUR_API_KEY"
@app.route('/chat', methods=['POST'])
def chat():
message = request.json['message']
prompt = f"User: {message}\nChatbot:"
completions = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
response = completions.choices[0].text.strip()
return jsonify({'message': response})
if __name__ == '__main__':
app.run(debug=True)
这段代码使用OpenAI Python客户端与ChatGPT API进行交互,并使用Flask创建了一个名为“/chat”的路由来处理POST请求。当应用程序收到POST请求时,它将提取请求的消息并将其用作ChatGPT API的输入,然后返回API的输出作为JSON格式的响应。python app.py
编写前端代码
在微信开发者工具中创建一个新的小程序项目,并添加以下代码:Page({
data: {
messages: [],
message: '',
},
onMessageInput: function (event) {
this.setData({message: event.detail.value});
},
onSendMessage: function () {
const that = this;
wx.request({
url: 'http://localhost:5000/chat',
method: 'POST',
header: {'Content-Type': 'application/json'},
data: {message: this.data.message},
success: function (res) {
that.setData({
messages: [...that.data.messages, {text: that.data.message, isUser: true}],
message: '',
});
setTimeout(function () {
that.setData({
messages: [...that.data.messages, {text: res.data.message, isUser: false}],
});
}, 500);
},
});
},
})
继续浏览有关 ChatGPT 的文章