Mr-Help commited on
Commit
9e57d26
·
verified ·
1 Parent(s): 0fe8321

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -4
app.py CHANGED
@@ -69,13 +69,29 @@ def chat_simple(req: SimpleChatRequest):
69
  top_p=req.top_p,
70
  )
71
 
72
- reply = response.choices[0].message.content if response.choices else ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  return JSONResponse(
74
  {
75
  "ok": True,
76
  "model": MODEL_NAME,
77
  "reply": reply,
78
- "usage": response.usage.model_dump() if getattr(response, "usage", None) else None,
 
79
  }
80
  )
81
 
@@ -94,13 +110,29 @@ def chat(req: ChatRequest):
94
  top_p=req.top_p,
95
  )
96
 
97
- reply = response.choices[0].message.content if response.choices else ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  return JSONResponse(
99
  {
100
  "ok": True,
101
  "model": MODEL_NAME,
102
  "reply": reply,
103
- "usage": response.usage.model_dump() if getattr(response, "usage", None) else None,
 
104
  }
105
  )
106
 
 
69
  top_p=req.top_p,
70
  )
71
 
72
+ choice = response.choices[0]
73
+
74
+ reply = ""
75
+ if choice.message and choice.message.content:
76
+ reply = choice.message.content
77
+
78
+ finish_reason = getattr(choice, "finish_reason", None)
79
+
80
+ usage = None
81
+ if getattr(response, "usage", None):
82
+ usage = {
83
+ "prompt_tokens": response.usage.prompt_tokens,
84
+ "completion_tokens": response.usage.completion_tokens,
85
+ "total_tokens": response.usage.total_tokens,
86
+ }
87
+
88
  return JSONResponse(
89
  {
90
  "ok": True,
91
  "model": MODEL_NAME,
92
  "reply": reply,
93
+ "finish_reason": finish_reason,
94
+ "usage": usage,
95
  }
96
  )
97
 
 
110
  top_p=req.top_p,
111
  )
112
 
113
+ choice = response.choices[0]
114
+
115
+ reply = ""
116
+ if choice.message and choice.message.content:
117
+ reply = choice.message.content
118
+
119
+ finish_reason = getattr(choice, "finish_reason", None)
120
+
121
+ usage = None
122
+ if getattr(response, "usage", None):
123
+ usage = {
124
+ "prompt_tokens": response.usage.prompt_tokens,
125
+ "completion_tokens": response.usage.completion_tokens,
126
+ "total_tokens": response.usage.total_tokens,
127
+ }
128
+
129
  return JSONResponse(
130
  {
131
  "ok": True,
132
  "model": MODEL_NAME,
133
  "reply": reply,
134
+ "finish_reason": finish_reason,
135
+ "usage": usage,
136
  }
137
  )
138