import mdtex2html
from flask import Flask, request, jsonify
from chat import converse
import json
from flask_cors import CORS
AVAIABLETOOLS = [
{
"name": "search",
"description": "Search Internet For Related Query and Provide Uptodate query",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search Query Follow the General Search Methods to get better result"
}
},
"required": ["query"]
}
}
]
def ToolSelector(other):
toreturn = []
if(type(other['tools']) == list):
try:
tools = other['tools']
for tool in tools:
for thistool in AVAIABLETOOLS:
if(thistool['name'] == tool):
toreturn.append(thistool)
else:
pass
except:
pass
else:
raise Exception('tools is not provided in list formate')
print(toreturn)
return toreturn
app = Flask(__name__)
CORS(app)
@app.route("/", methods=['GET', 'POST'])
def home():
# try:
par = request.get_json()
if not par:
return jsonify({"error": "Invalid JSON input"}), 400
conversation = par.get('conversation')
provider = par.get('provider')
model = par.get('model')
api = par.get('api')
try:
other = par.get('other')
selectedtools = ToolSelector(other)
other['tools'] = selectedtools
except:
other = {}
if not all([conversation, provider, model, api]):
return jsonify({"error": "Missing required parameters"}), 400
print(json.dumps(other,indent=4))
load = json.loads(converse(conversation, provider, model, api,other=other))
load['DirectResult'] = load['content']
try:
load['content'] = mdtex2html.convert(load['content'])
except Exception as e:
return jsonify({"error": f"Error converting content: {str(e)}"}), 500
toreturn = json.dumps(load, indent=4)
# print(toreturn)
return toreturn, 200
# except Exception as e:
# return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True, port=1777)