You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
I believe many may want to use this as an API to access from other applications, I tried asking chatgpt to build the application, and this is the result (amazing...)
import sys
import json
from flask import Flask, request
from revChatGPT.V1 import Chatbot
app = Flask(__name__)
def chatbot(prompt, c_id, p_id, access_token):
if not access_token:
error_resp = {
"error": "Access token is missing"
}
return json.dumps(error_resp), 400
c = {"access_token": access_token, "conversation_id": c_id, "parent_id": p_id}
chatbot_obj = Chatbot(config=c)
response = ""
for data in chatbot_obj.ask(prompt + "\n\n", c_id, p_id):
response = data["message"]
resp = {
"msg": "-",
"chat_id": "-",
"parent_id": "-",
"detail": "-"
}
if "message" in data:
resp["msg"] = data["message"]
resp["chat_id"] = data["conversation_id"]
resp["parent_id"] = data["parent_id"]
if "detail" in data:
resp["detail"] = data["detail"]
return resp
@app.route('/', methods=['POST'])
def webhook():
remote_ip = request.remote_addr
remote_port = request.environ.get('REMOTE_PORT')
if remote_ip == "0.0.0.0" and remote_port == "8081":
if request.method == 'POST':
content = request.get_json()
prompt = content['prompt']
chat_id = content['chat_id']
parent_id = content['parent_id']
access_token = content['access_token']
response = chatbot(prompt, chat_id, parent_id, access_token)
return json.dumps(response)
return "Forbidden" + remote_ip, 403
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081, debug=True)
here's an example of using api called from php: (which chatgpt also builds)
<?php
$url = 'http://localhost:8081/'; // Replace with the URL of the API you are using
$data = array(
'access_token' => 'xxxxxx', // Fill it with the access_token that you got
'chat_id' => '1', // Fill in the conversation_id that is being used
'parent_id' => '0', // Fill in the parent_id that is being used
'prompt' => 'Halo', //Fill it with the message you want to send to the bot
);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result, true);
if (isset($response['error'])) {
echo "Error: " . $response['error'];
} else {
echo "Response: " . $response['msg'];
}
?>
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I believe many may want to use this as an API to access from other applications, I tried asking chatgpt to build the application, and this is the result (amazing...)
here's an example of using api called from php: (which chatgpt also builds)
Beta Was this translation helpful? Give feedback.
All reactions