youngtsai commited on
Commit
357dba3
·
1 Parent(s): a6f92d7

questions_answers # REDROCK_CLIENT

Browse files
Files changed (1) hide show
  1. app.py +40 -20
app.py CHANGED
@@ -1117,13 +1117,7 @@ def get_questions_answers(video_id, df_string, source="gcs"):
1117
  return questions_answers
1118
 
1119
  def generate_questions_answers(df_string):
1120
- # 使用 OpenAI 生成基于上传数据的问题
1121
- if isinstance(df_string, str):
1122
- df_string_json = json.loads(df_string)
1123
- else:
1124
- df_string_json = df_string
1125
-
1126
- # JSON FORMAT: [{"question": "問題", "answer": "答案"}, ...]
1127
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,並用既有資料為本質猜測用戶可能會問的問題,使用 zh-TW"
1128
  user_content = f"""
1129
  請根據 {content_text} 生成三個問題跟答案,主要與學科有關,不要問跟情節故事相關的問題
@@ -1133,20 +1127,46 @@ def generate_questions_answers(df_string):
1133
  並用 JSON 格式返回 questions_answers: [{{question: q1的敘述text, answer: q1的答案text}}, ...]
1134
  k-v pair 的 key 是 question, value 是 answer
1135
  """
1136
- messages = [
1137
- {"role": "system", "content": sys_content},
1138
- {"role": "user", "content": user_content}
1139
- ]
1140
- response_format = { "type": "json_object" }
1141
- request_payload = {
1142
- "model": "gpt-4-turbo",
1143
- "messages": messages,
1144
- "max_tokens": 4000,
1145
- "response_format": response_format
1146
- }
1147
 
1148
- response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
1149
- questions_answers = json.loads(response.choices[0].message.content)["questions_answers"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1150
  print("=====json_response=====")
1151
  print(questions_answers)
1152
  print("=====json_response=====")
 
1117
  return questions_answers
1118
 
1119
  def generate_questions_answers(df_string):
1120
+ content_text = str(df_string)
 
 
 
 
 
 
1121
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,並用既有資料為本質猜測用戶可能會問的問題,使用 zh-TW"
1122
  user_content = f"""
1123
  請根據 {content_text} 生成三個問題跟答案,主要與學科有關,不要問跟情節故事相關的問題
 
1127
  並用 JSON 格式返回 questions_answers: [{{question: q1的敘述text, answer: q1的答案text}}, ...]
1128
  k-v pair 的 key 是 question, value 是 answer
1129
  """
 
 
 
 
 
 
 
 
 
 
 
1130
 
1131
+ try:
1132
+ # OPENAI
1133
+ messages = [
1134
+ {"role": "system", "content": sys_content},
1135
+ {"role": "user", "content": user_content}
1136
+ ]
1137
+ response_format = { "type": "json_object" }
1138
+ request_payload = {
1139
+ "model": "gpt-4-turbo",
1140
+ "messages": messages,
1141
+ "max_tokens": 4000,
1142
+ "response_format": response_format
1143
+ }
1144
+
1145
+ response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
1146
+ questions_answers = json.loads(response.choices[0].message.content)["questions_answers"]
1147
+ except:
1148
+ # REDROCK_CLIENT
1149
+ messages = [
1150
+ {"role": "user", "content": user_content}
1151
+ ]
1152
+ model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
1153
+ # model_id = "anthropic.claude-3-haiku-20240307-v1:0"
1154
+ kwargs = {
1155
+ "modelId": model_id,
1156
+ "contentType": "application/json",
1157
+ "accept": "application/json",
1158
+ "body": json.dumps({
1159
+ "anthropic_version": "bedrock-2023-05-31",
1160
+ "max_tokens": 4000,
1161
+ "system": sys_content,
1162
+ "messages": messages
1163
+ })
1164
+ }
1165
+ response = BEDROCK_CLIENT.invoke_model(**kwargs)
1166
+ response_body = json.loads(response.get('body').read())
1167
+ response_completion = response_body.get('content')[0].get('text')
1168
+ questions_answers = json.loads(response_completion)["questions_answers"]
1169
+
1170
  print("=====json_response=====")
1171
  print(questions_answers)
1172
  print("=====json_response=====")