youngtsai commited on
Commit
b5efa21
·
1 Parent(s): 2c76de1

system_prompt = self.instructions

Browse files
Files changed (2) hide show
  1. app.py +37 -31
  2. chatbot.py +2 -29
app.py CHANGED
@@ -1650,6 +1650,26 @@ def download_exam_result(content):
1650
  return word_path
1651
 
1652
  # ---- Chatbot ----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1653
  def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
1654
  verify_password(password)
1655
 
@@ -1668,6 +1688,19 @@ def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user
1668
  else:
1669
  simple_transcript = trascript_state
1670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1671
  chatbot_config = {
1672
  "video_id": video_id,
1673
  "transcript": simple_transcript,
@@ -1676,7 +1709,8 @@ def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user
1676
  "content_grade": content_grade,
1677
  "jutor_chat_key": JUTOR_CHAT_KEY,
1678
  "ai_name": ai_name,
1679
- "ai_client": ai_client
 
1680
  }
1681
 
1682
  chatbot = Chatbot(chatbot_config)
@@ -1736,21 +1770,7 @@ def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript_state
1736
  moment.pop('text', None)
1737
  key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
1738
 
1739
- instructions = f"""
1740
- subject: {content_subject}
1741
- grade: {content_grade}
1742
- context: {key_moments_text}
1743
- Assistant Role: you are a {content_subject} teacher
1744
- User Role: {content_grade} th-grade student.
1745
- Method: Socratic style, guide thinking, no direct answers. this is very important, please be seriously following.
1746
- Language: Traditional Chinese ZH-TW (it's very important), suitable for {content_grade} th-grade level.
1747
- Response: Single question, under 100 characters, include math symbols (use LaTeX $), hint with video timestamp which format【參考:00:00:00】.
1748
- Sometimes encourage user by Taiwanese style with relaxing atmosphere.
1749
- if user ask questions not include in context,
1750
- just tell them to ask the question in context and give them example question.
1751
- Restrictions: Answer within video content, no external references
1752
- """
1753
-
1754
  print("=== instructions ===")
1755
  print(instructions)
1756
 
@@ -1914,21 +1934,7 @@ def streaming_chat_with_open_ai(user_message, chat_history, password, thread_id,
1914
  moment.pop('images', None)
1915
  key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
1916
 
1917
- instructions = f"""
1918
- subject: {content_subject}
1919
- grade: {content_grade}
1920
- context: {key_moments_text}
1921
- Assistant Role: you are a {content_subject} teacher
1922
- User Role: {content_grade} th-grade student.
1923
- Method: Socratic style, guide thinking, no direct answers. this is very important, please be seriously following.
1924
- Language: Traditional Chinese ZH-TW (it's very important), suitable for {content_grade} th-grade level.
1925
- Response: Single question, under 100 characters, include math symbols (use LaTeX $), hint with video timestamp which format 【參考:00:00:00】.
1926
- Please encourage student by Taiwanese style with relaxing atmosphere.
1927
- if user ask questions not include in context,
1928
- just tell them to ask the question in context and give them example question.
1929
- Restrictions: Answer within video content, no external references
1930
- """
1931
-
1932
  # 创建线程
1933
  if not thread_id:
1934
  thread = client.beta.threads.create()
 
1650
  return word_path
1651
 
1652
  # ---- Chatbot ----
1653
+ def get_instructions(content_subject, content_grade, key_moments):
1654
+ instructions = f"""
1655
+ subject: {content_subject}
1656
+ grade: {content_grade}
1657
+ context: {key_moments}
1658
+ Assistant Role: you are a {content_subject} teacher
1659
+ User Role: {content_grade} th-grade student.
1660
+ Method: Socratic style, guide thinking, no direct answers. this is very important, please be seriously following.
1661
+ Language: Traditional Chinese ZH-TW (it's very important), suitable for {content_grade} th-grade level.
1662
+ Response:
1663
+ - Single question, under 100 characters
1664
+ - include math symbols (use LaTeX $ to cover before and after)
1665
+ - hint with video timestamp which format 【參考:00:00:00】.
1666
+ - Sometimes encourage user by Taiwanese style with relaxing atmosphere.
1667
+ - if user ask questions not include in context,
1668
+ - just tell them to ask the question in context and give them example question.
1669
+ Restrictions: Answer within video content, no external references
1670
+ """
1671
+ return instructions
1672
+
1673
  def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
1674
  verify_password(password)
1675
 
 
1688
  else:
1689
  simple_transcript = trascript_state
1690
 
1691
+ if isinstance(key_moments, str):
1692
+ key_moments_json = json.loads(key_moments)
1693
+ else:
1694
+ key_moments_json = key_moments
1695
+ # key_moments_json remove images
1696
+ for moment in key_moments_json:
1697
+ moment.pop('images', None)
1698
+ moment.pop('end', None)
1699
+ moment.pop('text', None)
1700
+ key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
1701
+
1702
+ instructions = get_instructions(content_subject, content_grade, key_moments_text)
1703
+
1704
  chatbot_config = {
1705
  "video_id": video_id,
1706
  "transcript": simple_transcript,
 
1709
  "content_grade": content_grade,
1710
  "jutor_chat_key": JUTOR_CHAT_KEY,
1711
  "ai_name": ai_name,
1712
+ "ai_client": ai_client,
1713
+ "instructions": instructions
1714
  }
1715
 
1716
  chatbot = Chatbot(chatbot_config)
 
1770
  moment.pop('text', None)
1771
  key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
1772
 
1773
+ instructions = get_instructions(content_subject, content_grade, key_moments_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1774
  print("=== instructions ===")
1775
  print(instructions)
1776
 
 
1934
  moment.pop('images', None)
1935
  key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
1936
 
1937
+ instructions = get_instructions(content_subject, content_grade, key_moments_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1938
  # 创建线程
1939
  if not thread_id:
1940
  thread = client.beta.threads.create()
chatbot.py CHANGED
@@ -12,6 +12,7 @@ class Chatbot:
12
  self.key_moments_text = self.get_key_moments_text(config.get('key_moments'))
13
  self.ai_name = config.get('ai_name')
14
  self.ai_client = config.get('ai_client')
 
15
 
16
  def get_transcript_text(self, transcript_data):
17
  if isinstance(transcript_data, str):
@@ -40,41 +41,13 @@ class Chatbot:
40
 
41
  def chat(self, user_message, chat_history, socratic_mode=False, service_type='jutor'):
42
  messages = self.prepare_messages(chat_history, user_message)
43
- system_prompt = self.prepare_system_prompt(socratic_mode)
44
  if service_type in ['jutor', 'groq', 'claude3']:
45
  response_text = self.chat_with_service(service_type, system_prompt, messages)
46
  return response_text
47
  else:
48
  raise gr.Error("不支持此服務")
49
 
50
- def prepare_system_prompt(self, socratic_mode):
51
- content_subject = self.content_subject
52
- content_grade = self.content_grade
53
- video_id = self.video_id
54
- transcript_text = self.transcript_text
55
- key_moments_text = self.key_moments_text
56
- socratic_mode = str(socratic_mode)
57
- ai_name = self.ai_name
58
-
59
- system_prompt = f"""
60
- subject: {content_subject}
61
- grade: {content_grade}
62
- context: {key_moments_text}
63
- Assistant Role: you are a {content_subject} teacher
64
- User Role: {content_grade} th-grade student.
65
- Method: Socratic style, guide thinking, no direct answers. this is very important, please be seriously following.
66
- Language: Traditional Chinese ZH-TW (it's very important), suitable for {content_grade} th-grade level.
67
- Response: Single question, under 100 characters, include math symbols (use LaTeX $), hint with video timestamp which format 【參考:00:00:00】.
68
- Sometimes encourage user by Taiwanese style with relaxing atmosphere.
69
- if user ask questions not include in context,
70
- just tell them to ask the question in context and give them example question.
71
- Restrictions: Answer within video content, no external references
72
- """
73
- print("====system_prompt====")
74
- print(system_prompt)
75
-
76
- return system_prompt
77
-
78
  def prepare_messages(self, chat_history, user_message):
79
  messages = []
80
  if chat_history is not None:
 
12
  self.key_moments_text = self.get_key_moments_text(config.get('key_moments'))
13
  self.ai_name = config.get('ai_name')
14
  self.ai_client = config.get('ai_client')
15
+ self.instructions = config.get('instructions')
16
 
17
  def get_transcript_text(self, transcript_data):
18
  if isinstance(transcript_data, str):
 
41
 
42
  def chat(self, user_message, chat_history, socratic_mode=False, service_type='jutor'):
43
  messages = self.prepare_messages(chat_history, user_message)
44
+ system_prompt = self.instructions
45
  if service_type in ['jutor', 'groq', 'claude3']:
46
  response_text = self.chat_with_service(service_type, system_prompt, messages)
47
  return response_text
48
  else:
49
  raise gr.Error("不支持此服務")
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def prepare_messages(self, chat_history, user_message):
52
  messages = []
53
  if chat_history is not None: