Spaces:
Building
Building
Upload 4 files
Browse files
app.py
CHANGED
@@ -93,6 +93,16 @@ class ResponseWrapper:
|
|
93 |
self._prompt_token_count = self._extract_prompt_token_count()
|
94 |
self._candidates_token_count = self._extract_candidates_token_count()
|
95 |
self._total_token_count = self._extract_total_token_count()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
def _extract_text(self) -> str:
|
98 |
try:
|
@@ -522,18 +532,13 @@ def chat_completions():
|
|
522 |
prompt_tokens = response.prompt_token_count
|
523 |
completion_tokens = response.candidates_token_count
|
524 |
total_tokens = response.total_token_count
|
525 |
-
|
526 |
-
if
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
'details': str(e)
|
533 |
-
}
|
534 |
-
}), 400
|
535 |
-
else:
|
536 |
-
return jsonify({
|
537 |
'error': {
|
538 |
'message': 'AI响应处理失败',
|
539 |
'type': 'response_processing_error'
|
@@ -551,7 +556,7 @@ def chat_completions():
|
|
551 |
'role': 'assistant',
|
552 |
'content': text_content
|
553 |
},
|
554 |
-
'finish_reason':
|
555 |
}],
|
556 |
'usage': {
|
557 |
'prompt_tokens': prompt_tokens,
|
|
|
93 |
self._prompt_token_count = self._extract_prompt_token_count()
|
94 |
self._candidates_token_count = self._extract_candidates_token_count()
|
95 |
self._total_token_count = self._extract_total_token_count()
|
96 |
+
self._thoughts = self._extract_thoughts()
|
97 |
+
|
98 |
+
def _extract_thoughts(self) -> Optional[str]:
|
99 |
+
try:
|
100 |
+
for part in self._data['candidates'][0]['content']['parts']:
|
101 |
+
if 'thought' in part:
|
102 |
+
return part['text']
|
103 |
+
return None
|
104 |
+
except (KeyError, IndexError):
|
105 |
+
return None
|
106 |
|
107 |
def _extract_text(self) -> str:
|
108 |
try:
|
|
|
532 |
prompt_tokens = response.prompt_token_count
|
533 |
completion_tokens = response.candidates_token_count
|
534 |
total_tokens = response.total_token_count
|
535 |
+
finish_reason = response.finish_reason
|
536 |
+
if is_thinking and show_thoughts:
|
537 |
+
# 把thoughts加到text_content的前面再加一个回车
|
538 |
+
text_content = response.thoughts + '\n' + text_content
|
539 |
+
logger.info("finish_reason: ")
|
540 |
+
except AttributeError as e:
|
541 |
+
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
542 |
'error': {
|
543 |
'message': 'AI响应处理失败',
|
544 |
'type': 'response_processing_error'
|
|
|
556 |
'role': 'assistant',
|
557 |
'content': text_content
|
558 |
},
|
559 |
+
'finish_reason': finish_reason
|
560 |
}],
|
561 |
'usage': {
|
562 |
'prompt_tokens': prompt_tokens,
|