-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
270 lines (217 loc) · 8.94 KB
/
server.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import argparse
from flask import Flask, request, jsonify
from flask.logging import create_logger
from mlx_lm import load, generate
app = Flask(__name__)
logger = create_logger(app)
def validate_temperature(temperature):
if not (0.0 <= temperature <= 1.0):
raise ValueError("Temperature must be between 0.0 and 1.0")
def validate_max_tokens(max_tokens):
if not (1 <= max_tokens <= 131072):
raise ValueError("Max tokens must be between 1 and 131072")
def validate_citation_mode(citation_mode):
if citation_mode not in ['fast', 'accurate']:
raise ValueError("Citation mode must be either 'fast' or 'accurate'")
def validate_tools(tools):
for tool in tools:
if 'name' not in tool:
raise ValueError("Each tool must have a 'name' field")
if 'description' not in tool:
raise ValueError("Each tool must have a 'description' field")
if 'parameter_definitions' not in tool:
raise ValueError("Each tool must have a 'parameter_definitions' field")
for param_name, param_def in tool['parameter_definitions'].items():
if 'description' not in param_def:
raise ValueError(f"Parameter '{param_name}' must have a 'description' field")
if 'type' not in param_def:
raise ValueError(f"Parameter '{param_name}' must have a 'type' field")
if 'required' not in param_def:
raise ValueError(f"Parameter '{param_name}' must have a 'required' field")
@app.route('/generate', methods=['POST'])
def generate_text():
"""
Generate text based on the given prompt.
Request JSON:
{
"prompt": "The prompt to generate text from",
"temperature": (optional, default=0.2) The temperature for text generation (0.0 to 1.0),
"max_tokens": (optional, default=131072) The maximum number of tokens to generate (1 to 131072)
}
Response JSON:
{
"generated_text": "The generated text"
}
"""
logger.info("Received a generation request.")
try:
data = request.get_json()
prompt = data['prompt']
temperature = data.get('temperature', 0.2)
max_tokens = data.get('max_tokens', 131072)
validate_temperature(temperature)
validate_max_tokens(max_tokens)
response = generate(
model,
tokenizer,
prompt=prompt,
verbose=True,
temp=temperature,
max_tokens=max_tokens,
)
logger.debug(f"Generated response: {response}")
return jsonify({"generated_text": response})
except KeyError as e:
logger.error(f"Missing key in request JSON: {str(e)}")
return jsonify({"error": f"Missing key in request JSON: {str(e)}"}), 400
except ValueError as e:
logger.error(f"An error occurred: {str(e)}")
return jsonify({"error": str(e)}), 400
except Exception as e:
logger.error(f"An error occurred: {str(e)}")
return jsonify({"error": "An unexpected error occurred", "details": str(e)}), 500
@app.route('/chat', methods=['POST'])
def chat():
"""
Generate a chat response based on the given conversation.
Request JSON:
{
"conversation": [
{"role": "user", "content": "User's message"},
{"role": "assistant", "content": "Assistant's response"},
...
],
"temperature": (optional, default=0.2) The temperature for response generation (0.0 to 1.0),
"max_tokens": (optional, default=131072) The maximum number of tokens to generate (1 to 131072)
}
Response JSON:
{
"generated_text": "The generated chat response"
}
"""
try:
data = request.get_json()
conversation = data['conversation']
temperature = data.get('temperature', 0.2)
max_tokens = data.get('max_tokens', 131072)
validate_temperature(temperature)
validate_max_tokens(max_tokens)
inputs = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
response = generate(
model,
tokenizer,
prompt=inputs,
verbose=True,
temp=temperature,
max_tokens=max_tokens,
)
return jsonify({"generated_text": response})
except KeyError as e:
return jsonify({"error": f"Missing key in request JSON: {str(e)}"}), 400
except ValueError as e:
return jsonify({"error": str(e)}), 400
except Exception as e:
return jsonify({"error": "An unexpected error occurred", "details": str(e)}), 500
@app.route('/tool', methods=['POST'])
def use_tool():
"""
Generate a tool response based on the given conversation and tools.
Request JSON:
{
"conversation": [
{"role": "user", "content": "User's message"},
{"role": "assistant", "content": "Assistant's response"},
...
],
"tools": [
{
"name": "internet_search",
"description": "Returns a list of relevant document snippets for a textual query retrieved from the internet",
"parameter_definitions": {
"query": {
"description": "Query to search the internet with",
"type": 'str',
"required": True
},
},
...
]
}
Response JSON:
{
"tool_response": "The generated tool response"
}
"""
logger.info("Received a tool request.")
try:
data = request.get_json()
conversation = data['conversation']
tools = data['tools']
validate_tools(tools)
formatted_input = tokenizer.apply_tool_use_template(conversation, tools=tools, tokenize=False, add_generation_prompt=True)
response = generate(model, tokenizer, prompt=formatted_input, verbose=True)
return jsonify({"tool_response": response})
except KeyError as e:
logger.error(f"Missing key in request JSON: {str(e)}")
return jsonify({"error": f"Missing key in request JSON: {str(e)}"}), 400
except ValueError as e:
logger.error(f"An error occurred: {str(e)}")
return jsonify({"error": str(e)}), 400
except Exception as e:
logger.error(f"An error occurred: {str(e)}")
return jsonify({"error": "An unexpected error occurred", "details": str(e)}), 500
@app.route('/rag', methods=['POST'])
def rag():
"""
Generate a grounded response based on the given conversation and documents.
Request JSON:
{
"conversation": [
{"role": "user", "content": "User's message"},
{"role": "assistant", "content": "Assistant's response"},
...
],
"documents": [
{ "title": "Tall penguins", "text": "Emperor penguins are the tallest growing up to 122 cm in height." },
{ "title": "Penguin habitats", "text": "Emperor penguins only live in Antarctica."}
],
"citation_mode": (optional, default="accurate") The citation mode ("fast" or "accurate")
}
Response JSON:
{
"rag_response": "The generated grounded response"
}
"""
logger.info("Received a RAG request.")
try:
data = request.get_json()
conversation = data['conversation']
documents = data['documents']
citation_mode = data.get('citation_mode', 'accurate')
validate_citation_mode(citation_mode)
formatted_input = tokenizer.apply_grounded_generation_template(
conversation,
documents=documents,
citation_mode=citation_mode,
tokenize=False,
add_generation_prompt=True,
)
response = generate(model, tokenizer, prompt=formatted_input, verbose=True)
return jsonify({"rag_response": response})
except KeyError as e:
logger.error(f"Missing key in request JSON: {str(e)}")
return jsonify({"error": f"Missing key in request JSON: {str(e)}"}), 400
except ValueError as e:
logger.error(f"An error occurred: {str(e)}")
return jsonify({"error": str(e)}), 400
except Exception as e:
logger.error(f"An error occurred: {str(e)}")
return jsonify({"error": "An unexpected error occurred", "details": str(e)}), 500
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run the Command-R MLX API server')
parser.add_argument('--port', '-p', type=int, default=5000, help='Port number for the server')
parser.add_argument('--model', '-m', type=str, default='mlx-community/c4ai-command-r-v01-4bit', help='Model name')
parser.add_argument('--debug', '-d', action='store_true', default=True, help='Enable debug mode')
args = parser.parse_args()
model, tokenizer = load(args.model)
app.run(port=args.port, debug=args.debug)