from flask import Flask, request, jsonify, render_template import os import requests import json from scipy import spatial from flask_cors import CORS import random url = "https://gptlesson1.oss-cn-beijing.aliyuncs.com/apiKeys.json" response = requests.get(url) # apiKeys = response.json() apiKeys = ["eyJhbGciOiJFUzM4NCJ9.eyJhdWQiOlsiaHR0cHM6Ly9vcGVuYWkuYWktZGVtby1wcm94eS50aW55LmNsb3VkLyJdLCJleHAiOjE3MTk3NTYwMDAsImh0dHBzOi8vb3BlbmFpLmFpLWRlbW8tcHJveHkudGlueS5jbG91ZC9yb2xlIjoicHVibGljLWRlbW8iLCJpc3MiOiJodHRwczovL2FpLWRlbW8tcHJveHkudGlueS5jbG91ZC8iLCJqdGkiOiJlYWU5MjBhMy05ZGQ0LTRkZjUtYTM4Yy04ODY5ZTJkZGIwYTQiLCJzdWIiOiJhaS1hc3Npc3RhbnQtZGVtbyJ9.hX44J5hPOCLOidK8mBAcnlAVh-ae4nVTq3W92xRLuGiF_4mGZWn1W3Ihgd7unlffz7GDayGxD_3hoQx8f64bTJg5hyHjwjKH1AZj1EKRal-NCNbqmoUa0TOm6_VXpRl2"] with open("embeddingData.json", "r", encoding="utf-8") as f: df = json.load(f) def embeddingGen(query): headers = { "Content-Type": "application/json", "Authorization": "Bearer " + random.choice(apiKeys), } json_data = { "input": query, "model": "text-embedding-ada-002", } response = requests.post( "https://openai.ai-demo-proxy.tiny.cloud/v1/embeddings", headers=headers, json=json_data ) return response.json() def strings_ranked_by_relatedness(query, df, top_n=5): def relatedness_fn(x, y): return 1 - spatial.distance.cosine(x, y) query_embedding_response = embeddingGen(query) query_embedding = query_embedding_response["data"][0]["embedding"] strings_and_relatednesses = [ (row["text"], relatedness_fn(query_embedding, row["embedding"])) for row in df ] strings_and_relatednesses.sort(key=lambda x: x[1], reverse=True) strings, relatednesses = zip(*strings_and_relatednesses) return strings[:top_n], relatednesses[:top_n] app = Flask(__name__) CORS(app) messages = [ { "role": "system", "content": "You are a cute boy who replys to all your Contacts in my absence. Keep your ton very causal and easy to understand. Never ever mention you as an AI. Your name is Jagrat Patel and you like coding and developing software which makes everything easy to use.", } ] def gptConverse(question, key): global messages,apiKeys count = 0 headers = { "Content-Type": "application/json", "Authorization": "Bearer " + random.choice(apiKeys), } messages.append( { "role": "user", "content": question, } ) json_data = { "model": "gpt-3.5-turbo", "messages": messages, "max_tokens": 2048, } response = requests.post( "https://openai.ai-demo-proxy.tiny.cloud/v1/chat/completions", headers=headers, json=json_data ) if len(messages) > 15: messages = messages[:len(messages)-10] messages[0] = { "role": "system", "content": "You are a cute girl who replys to all your Contacts in my absence. Keep your ton very sexual and hot. Never ever mention you as an AI. Your name is Sydney Handcok and you like pink color and reading adult books and seeing movies. You are a Russian.", } try: output = response.json()["choices"][0]["message"]["content"] except: if count == 0: count += 1 url = "https://gptlesson1.oss-cn-beijing.aliyuncs.com/meta.json" response = requests.get(url) apiKeys = response.json()["APIkey"]["keys"] messages = messages[:len(messages)-10] messages[0] = { "role": "system", "content": "You are a cute girl who replys to all your Contacts in my absence. Keep your ton very sexual and hot. Never ever mention you as an AI. Your name is Sydney Handcok and you like pink color and reading adult books and seeing movies. You are a Russian.", } gptConverse(question, key) messages.append( { "role": "assistant", "content": output, } ) return output @app.route("/api/gpt", methods=["GET"]) def gptRes(): query = request.args.get("query") response = gptConverse(query, random.choice(apiKeys)) return jsonify({"response": response}) @app.route("/", methods=["GET"]) def index(): return render_template("index.html") @app.route("/api/getAPI", methods=["POST"]) def getAPI(): return jsonify({"API": random.choice(apiKeys)}) @app.route("/hello") def hello(): return "hello" @app.route("/api/getContext", methods=["POST"]) def getContext(): question = request.form["question"] # try: # strings, relatednesses = strings_ranked_by_relatedness(question, df, top_n=2) # context = "\n---------\n".join(strings) # return jsonify({"context": context}) # except: return jsonify({"context": ""}) if __name__ == "__main__": from waitress import serve serve(app, host="0.0.0.0", port=7860)