Euryeth commited on
Commit
17aeec1
·
verified ·
1 Parent(s): 6db605f

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +11 -24
api.py CHANGED
@@ -7,40 +7,27 @@ app = Flask(__name__)
7
  @app.route('/v1/chat/completions', methods=['POST'])
8
  def chat_completions():
9
  data = request.json
10
- messages = data.get('messages', [])
11
- max_tokens = data.get('max_tokens', 560)
12
- temperature = data.get('temperature', 0.8)
13
-
14
  if not messages or not isinstance(messages, list):
15
  return jsonify({"error": "A valid 'messages' list is required."}), 400
16
 
17
  try:
18
- start_time = time.time()
19
-
20
- # Expecting plain role-content dicts (not Gradio tuples)
21
- result = generate_chat_completion(
22
- message_history=messages,
23
- max_tokens=max_tokens,
24
- temperature=temperature
25
  )
26
-
27
- # Get only the assistant's latest message
28
- assistant_msg = result[-1] if isinstance(result, list) else result
29
- elapsed = time.time() - start_time
30
 
31
  return jsonify({
32
- "model": "mistralai/Mistral-7B-Instruct-v0.2",
33
  "choices": [{
34
- "message": {
35
- "role": "assistant",
36
- "content": assistant_msg
37
- }
38
  }],
39
- "usage": {
40
- "generation_time": round(elapsed, 2)
41
- }
42
  })
43
-
44
  except Exception as e:
45
  return jsonify({"error": str(e)}), 500
46
 
 
7
  @app.route('/v1/chat/completions', methods=['POST'])
8
  def chat_completions():
9
  data = request.json
10
+ messages = data.get('messages') # list of {"role":..., "content":...}
 
 
 
11
  if not messages or not isinstance(messages, list):
12
  return jsonify({"error": "A valid 'messages' list is required."}), 400
13
 
14
  try:
15
+ start = time.time()
16
+ new_history = generate_chat_completion(
17
+ message=messages[-1]['content'],
18
+ history=messages[:-1] # feed all except last user message
 
 
 
19
  )
20
+ assistant_msg = new_history[-1]['content']
21
+ elapsed = time.time() - start
 
 
22
 
23
  return jsonify({
24
+ "model": "tiiuae/falcon-rw-1b",
25
  "choices": [{
26
+ "message": {"role": "assistant", "content": assistant_msg}
 
 
 
27
  }],
28
+ "usage": {"generation_time": round(elapsed, 2)},
29
+ "history": new_history
 
30
  })
 
31
  except Exception as e:
32
  return jsonify({"error": str(e)}), 500
33