forked from React-Group/interstellar_ai
Compare commits
No commits in common. "913c4f069a94546ddb2cebcc07e795020b8167fd" and "1744148b1b5ef9c8e8417b3c45be4ede0e67005e" have entirely different histories.
913c4f069a
...
1744148b1b
6 changed files with 20 additions and 67 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -38,7 +38,6 @@ next-env.d.ts
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
venv/
|
venv/
|
||||||
__pycache__/
|
|
||||||
|
|
||||||
key.pem
|
key.pem
|
||||||
cert.pem
|
cert.pem
|
||||||
|
|
BIN
py/__pycache__/ai.cpython-312.pyc
Normal file
BIN
py/__pycache__/ai.cpython-312.pyc
Normal file
Binary file not shown.
38
py/ai.py
38
py/ai.py
|
@ -1,5 +1,4 @@
|
||||||
from mistralai import Mistral
|
from mistralai import Mistral
|
||||||
from openai import OpenAI
|
|
||||||
import ollama
|
import ollama
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,15 +12,19 @@ class AI:
|
||||||
options={"temperature": 0.5},
|
options={"temperature": 0.5},
|
||||||
)
|
)
|
||||||
|
|
||||||
with return_class.ai_response_lock:
|
for i in messages:
|
||||||
return_class.ai_response[access_token] = ""
|
print(i)
|
||||||
|
|
||||||
|
return_class.ai_response[access_token] = ""
|
||||||
|
|
||||||
for chunk in stream:
|
for chunk in stream:
|
||||||
with return_class.ai_response_lock:
|
print(chunk['message']['content'])
|
||||||
return_class.ai_response[access_token] += chunk['message']['content']
|
return_class.ai_response[access_token] += chunk['message']['content']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def process_mistralai(model, messages, return_class, access_token, api_key):
|
def process_mistralai(model, messages, return_class, access_token):
|
||||||
|
with open("api_key.txt", 'r') as f:
|
||||||
|
api_key = f.read().strip()
|
||||||
|
|
||||||
client = Mistral(api_key=api_key)
|
client = Mistral(api_key=api_key)
|
||||||
|
|
||||||
|
@ -30,26 +33,7 @@ class AI:
|
||||||
messages=messages
|
messages=messages
|
||||||
)
|
)
|
||||||
|
|
||||||
with return_class.ai_response_lock:
|
return_class.ai_response[access_token] = ""
|
||||||
return_class.ai_response[access_token] = ""
|
|
||||||
|
|
||||||
for chunk in stream_response:
|
for chunk in stream_response:
|
||||||
with return_class.ai_response_lock:
|
return_class.ai_response[access_token] += chunk.data.choices[0].delta.content
|
||||||
return_class.ai_response[access_token] += chunk.data.choices[0].delta.content
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def process_openai(model, messages, return_class, access_token, api_key):
|
|
||||||
|
|
||||||
client = OpenAI(api_key=api_key)
|
|
||||||
|
|
||||||
stream_response = client.chat.completions.create(
|
|
||||||
model=model,
|
|
||||||
messages=messages
|
|
||||||
)
|
|
||||||
|
|
||||||
with return_class.ai_response_lock:
|
|
||||||
return_class.ai_response[access_token] = ""
|
|
||||||
|
|
||||||
for chunk in stream_response:
|
|
||||||
with return_class.ai_response_lock:
|
|
||||||
return_class.ai_response[access_token] += chunk.choices[0].delta.content
|
|
||||||
|
|
32
py/api.py
32
py/api.py
|
@ -1,7 +1,6 @@
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
import secrets
|
import secrets
|
||||||
import threading
|
|
||||||
from ai import AI
|
from ai import AI
|
||||||
from db import DB
|
from db import DB
|
||||||
from OpenSSL import crypto
|
from OpenSSL import crypto
|
||||||
|
@ -9,13 +8,11 @@ from OpenSSL import crypto
|
||||||
|
|
||||||
class API:
|
class API:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.crypt_size = 1
|
self.crypt_size = 4096
|
||||||
self.app = Flask(__name__)
|
self.app = Flask(__name__)
|
||||||
self.ai_response = {}
|
self.ai_response = {}
|
||||||
self.ai = AI()
|
self.ai = AI()
|
||||||
self.db = DB()
|
self.db = DB()
|
||||||
self.db.load_database()
|
|
||||||
self.ai_response_lock = threading.Lock()
|
|
||||||
CORS(self.app)
|
CORS(self.app)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
@ -29,25 +26,12 @@ class API:
|
||||||
def send_ai():
|
def send_ai():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
messages = data.get('messages')
|
messages = data.get('messages')
|
||||||
model_type = data.get('model_type')
|
|
||||||
ai_model = data.get('ai_model')
|
ai_model = data.get('ai_model')
|
||||||
access_token = data.get('access_token')
|
access_token = data.get('access_token')
|
||||||
if access_token not in self.ai_response:
|
if access_token not in self.ai_response:
|
||||||
return jsonify({'status': 401, 'error': 'Invalid access token'})
|
return jsonify({'status': 401, 'error': 'Invalid access token'})
|
||||||
|
self.ai.process_local(ai_model, messages, self, access_token)
|
||||||
if model_type == "local":
|
return jsonify({'status': 200})
|
||||||
thread = threading.Thread(target=self.ai.process_local, args=(ai_model, messages, self, access_token))
|
|
||||||
thread.start()
|
|
||||||
thread.join()
|
|
||||||
return jsonify({'status': 200})
|
|
||||||
elif model_type == "mistral":
|
|
||||||
api_key = data.get('api_key')
|
|
||||||
thread = threading.Thread(target=self.ai.process_mistralai, args=(ai_model, messages, self, access_token, api_key))
|
|
||||||
thread.start()
|
|
||||||
thread.join()
|
|
||||||
return jsonify({'status': 200})
|
|
||||||
|
|
||||||
return jsonify({'status': 401, 'error': 'Invalid AI model type'})
|
|
||||||
|
|
||||||
@self.app.route('/interstellar/api/ai_get', methods=['GET'])
|
@self.app.route('/interstellar/api/ai_get', methods=['GET'])
|
||||||
def get_ai():
|
def get_ai():
|
||||||
|
@ -62,13 +46,13 @@ class API:
|
||||||
data = request.args.get('data')
|
data = request.args.get('data')
|
||||||
if action == "create_account":
|
if action == "create_account":
|
||||||
self.db.add_user(data)
|
self.db.add_user(data)
|
||||||
elif action == "change_password":
|
if action == "change_password":
|
||||||
self.db.update_password(data)
|
self.db.update_password(data)
|
||||||
elif action == "get_data":
|
if action == "get_data":
|
||||||
self.db.get_data(data)
|
self.db.get_data(data)
|
||||||
elif action == "change_data":
|
if action == "change_data":
|
||||||
self.db.change_data(data)
|
self.db.change_data(data)
|
||||||
elif action == "check_credentials":
|
if action == "check_credentials":
|
||||||
self.db.check_credentials(data)
|
self.db.check_credentials(data)
|
||||||
|
|
||||||
email_address = "emailAddress"
|
email_address = "emailAddress"
|
||||||
|
@ -105,7 +89,7 @@ class API:
|
||||||
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode("utf-8"))
|
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode("utf-8"))
|
||||||
|
|
||||||
ssl_context = ("cert.pem", "key.pem")
|
ssl_context = ("cert.pem", "key.pem")
|
||||||
self.app.run(debug=True, host='0.0.0.0', port=5000)
|
self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
15
py/db.py
15
py/db.py
|
@ -1,5 +1,5 @@
|
||||||
import hashlib
|
|
||||||
import json
|
import json
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
|
||||||
class DB:
|
class DB:
|
||||||
|
@ -26,7 +26,6 @@ class DB:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.database[username]['data'] = data
|
self.database[username]['data'] = data
|
||||||
self.save_database()
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def update_password(self, data):
|
def update_password(self, data):
|
||||||
|
@ -37,7 +36,6 @@ class DB:
|
||||||
|
|
||||||
hashed_new_password = self.hash_password(new_password)
|
hashed_new_password = self.hash_password(new_password)
|
||||||
self.database[username].update({"hashed_password": hashed_new_password})
|
self.database[username].update({"hashed_password": hashed_new_password})
|
||||||
self.save_database()
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def check_credentials(self, data):
|
def check_credentials(self, data):
|
||||||
|
@ -57,14 +55,3 @@ class DB:
|
||||||
|
|
||||||
send_back = self.database[username].get['data']
|
send_back = self.database[username].get['data']
|
||||||
return send_back
|
return send_back
|
||||||
|
|
||||||
def save_database(self):
|
|
||||||
with open("database.json", 'w') as file:
|
|
||||||
json.dump(self.database, file)
|
|
||||||
|
|
||||||
def load_database(self):
|
|
||||||
try:
|
|
||||||
with open("database.json", 'r') as file:
|
|
||||||
self.database = json.load(file)
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
|
@ -2,5 +2,4 @@ flask
|
||||||
flask-cors
|
flask-cors
|
||||||
ollama
|
ollama
|
||||||
mistralai
|
mistralai
|
||||||
openai
|
|
||||||
pyOpenSSL
|
pyOpenSSL
|
Loading…
Reference in a new issue