Basshole commited on
Commit
f4dcfae
·
verified ·
1 Parent(s): c7e2b0d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -3
app.py CHANGED
@@ -114,7 +114,7 @@ def generate_prompt(achievement: str, emotion: str, information: str,
114
  ############################################
115
  # 呼叫模型 API (OpenAI / Claude)
116
  ############################################
117
- def call_openai_api(prompt: str, model_name: str = "gpt-3.5-turbo") -> str:
118
  """參考新版 usage: from openai import OpenAI -> client.chat.completions.create()"""
119
  if not client.api_key:
120
  return "[OpenAI API Key 未設定,無法呼叫 API]"
@@ -125,7 +125,7 @@ def call_openai_api(prompt: str, model_name: str = "gpt-3.5-turbo") -> str:
125
  messages=[{"role": "user", "content": prompt}],
126
  model=model_name,
127
  temperature=0.7,
128
- max_tokens=1024,
129
  )
130
  return chat_completion.choices[0].message.content
131
  except Exception as e:
@@ -154,7 +154,7 @@ def call_claude_api(prompt: str) -> str:
154
  messages=[
155
  {"role": "user", "content": prompt}
156
  ],
157
- max_tokens=1024,
158
  temperature=0.7,
159
  )
160
  text = response.content
@@ -164,6 +164,8 @@ def call_claude_api(prompt: str) -> str:
164
  # 移除或替換 TextBlock(...) 的外層結構,只保留其中文字
165
  import re
166
  text = re.sub(r"TextBlock\(.*?text='(.*?)'.*?\)", r"\1", text, flags=re.DOTALL)
 
 
167
  return text.strip()
168
  except Exception as e:
169
  return f"[Claude API 呼叫失敗]: {str(e)}"
 
114
  ############################################
115
  # 呼叫模型 API (OpenAI / Claude)
116
  ############################################
117
+ def call_openai_api(prompt: str, model_name: str) -> str:
118
  """參考新版 usage: from openai import OpenAI -> client.chat.completions.create()"""
119
  if not client.api_key:
120
  return "[OpenAI API Key 未設定,無法呼叫 API]"
 
125
  messages=[{"role": "user", "content": prompt}],
126
  model=model_name,
127
  temperature=0.7,
128
+ max_completion_tokens=2048,
129
  )
130
  return chat_completion.choices[0].message.content
131
  except Exception as e:
 
154
  messages=[
155
  {"role": "user", "content": prompt}
156
  ],
157
+ max_tokens=2048,
158
  temperature=0.7,
159
  )
160
  text = response.content
 
164
  # 移除或替換 TextBlock(...) 的外層結構,只保留其中文字
165
  import re
166
  text = re.sub(r"TextBlock\(.*?text='(.*?)'.*?\)", r"\1", text, flags=re.DOTALL)
167
+ # 先將原本的 "\n"(backslash-n) 字串轉成真正的換行符
168
+ text = text.replace('\\n', '\n')
169
  return text.strip()
170
  except Exception as e:
171
  return f"[Claude API 呼叫失敗]: {str(e)}"