Moonfanz commited on
Commit
8b094bd
·
verified ·
1 Parent(s): d29e7e6

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +18 -13
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
- except (AttributeError, IndexError, TypeError, ValueError) as e:
526
- if "response.candidates" in str(e) or "response.text" in str(e):
527
- logger.error(f"用户输入被AI安全过滤器阻止")
528
- return jsonify({
529
- 'error': {
530
- 'message': '用户输入被AI安全过滤器阻止',
531
- 'type': 'prompt_blocked_error',
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': 'stop'
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,