-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
124 lines (97 loc) · 3.76 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from flask import Flask, request, jsonify
import time
import api.openai
import api.qwen
import api.gemini
import api.llama
import api.claude
import api.grok
from rag import base
# Initialize the knowledge base
base.init_knowledge_base()
# Initialize the Flask app
app = Flask(__name__)
# Cache variables for models
cached_models = None
cache_timestamp = 0
cache_duration = 3600 # Cache duration in seconds (e.g., 1 hour)
# Routes
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
if not data:
return jsonify({'error': 'Invalid JSON'}), 400
messages = data.get('messages')
model = data.get('model', 'gpt-4o-mini') # Default model if none provided
if not messages:
return jsonify({'error': 'No messages provided'}), 400
try:
if model.startswith('gpt-'):
reply = api.openai.chat(model, messages)
elif model.startswith('qwen-'):
reply = api.qwen.chat(model, messages)
elif model.startswith('gemini-'):
reply = api.gemini.chat(model, messages)
elif model.startswith('llama-'):
reply = api.llama.chat(model, messages)
elif model.startswith('claude-'):
reply = api.claude.chat(model, messages)
elif model.startswith('grok-'):
reply = api.grok.chat(model, messages)
else:
# TODO: Use custom InvalidModelError
# since model can be non-GPT model
raise NotImplementedError("Invalid model specified.")
# Return the reply to the client
return jsonify({'reply': reply}), 200
except NotImplementedError as e:
app.logger.error(f"Invalid request: {e}", exc_info=True)
return jsonify({'error': 'Invalid model specified.'}), 400
except Exception as e:
app.logger.error(f"Exception occurred: {e}", exc_info=True)
return jsonify({'error': str(e)}), 500
@app.route('/models', methods=['GET'])
def list_models():
global cached_models, cache_timestamp
try:
current_time = time.time()
# Check if cache is expired or doesn't exist
if not cached_models or (current_time - cache_timestamp) > cache_duration:
model_details = []
model_details += api.openai.models()
model_details += api.qwen.models()
model_details += api.gemini.models()
model_details += api.llama.models()
model_details += api.claude.models()
model_details += api.grok.models()
# Update cache
cached_models = model_details
cache_timestamp = current_time
else:
model_details = cached_models
# Return the list of models to the client
return jsonify({'models': model_details}), 200
except Exception as e:
app.logger.error(f"Exception occurred: {e}", exc_info=True)
return jsonify({'error': str(e)}), 500
@app.route('/list_entries', methods=['GET'])
def list_entries():
n = int(request.args.get('n', 0)) # if n == 0, return all entries
entries = base.list_entries(n)
return jsonify({'entries': entries}), 200
@app.route('/add_entries', methods=['POST'])
def add_entries():
data = request.get_json()
entry_list = data.get('entries')
if not entry_list or not isinstance(entry_list, list):
return jsonify({'error': 'A list of entries is required.'}), 400
base.add_entries(entry_list)
return jsonify({'message': 'Entries added successfully.'})
@app.route('/search', methods=['GET'])
def search():
query = request.args.get('query')
top_k = int(request.args.get('top_k', 5))
if not query:
return jsonify({'error': 'Query parameter is required.'}), 400
results = base.search(query, top_k)
return jsonify({'results': results}), 200