simonguest commited on
Commit
3d7bc32
·
1 Parent(s): aac631e

Added age and name as query params

Browse files
Files changed (3) hide show
  1. app.py +16 -10
  2. prompts.py +4 -1
  3. tutor.py +9 -3
app.py CHANGED
@@ -25,8 +25,8 @@ def submit_code(tutor_ctx, editor, output):
25
  raise gr.Error(str(error))
26
 
27
 
28
- def init_tutor(instructions, starter_code):
29
- tutor = Tutor(instructions, starter_code)
30
  return [tutor.serialize(), tutor._memory_as_history()]
31
 
32
 
@@ -42,23 +42,29 @@ def run_code(code):
42
  finally:
43
  sys.stdout = sys.__stdout__
44
 
45
-
46
- def load_level(request: gr.Request):
47
  try:
48
- level = parse.parse_qs(parse.urlparse(request.headers["referer"]).query)[
49
- "level"
50
  ][0]
51
  except:
52
- level = "1"
 
 
 
 
 
53
  with open(f"levels/{level}/metadata.json", "r") as f:
54
  data = json.load(f)
55
  instructions = open(f"levels/{level}/{data['instructions']}", "r").read()
56
  starter_code = open(f"levels/{level}/{data['starter_code']}", "r").read()
57
- return [instructions, starter_code]
58
 
59
 
60
  with gr.Blocks(title="CS Tutor") as demo:
61
  tutor_ctx = gr.State()
 
 
62
  with gr.Row():
63
  instruction_panel = gr.Markdown()
64
  with gr.Row():
@@ -87,8 +93,8 @@ with gr.Blocks(title="CS Tutor") as demo:
87
  run.click(
88
  run_code, editor, output, queue=False, scroll_to_output=True
89
  ).then(submit_code, [tutor_ctx, editor, output], [tutor_ctx, chatbot])
90
- demo.load(load_level, None, [instruction_panel, editor], queue=False).then(
91
- init_tutor, [instruction_panel, editor], [tutor_ctx, chatbot]
92
  )
93
 
94
  demo.queue()
 
25
  raise gr.Error(str(error))
26
 
27
 
28
+ def init_tutor(instructions, starter_code, age, name):
29
+ tutor = Tutor(instructions, starter_code, age, name)
30
  return [tutor.serialize(), tutor._memory_as_history()]
31
 
32
 
 
42
  finally:
43
  sys.stdout = sys.__stdout__
44
 
45
+ def _query_param(request: gr.Request, param: str, default: str = None) -> str:
 
46
  try:
47
+ return parse.parse_qs(parse.urlparse(request.headers["referer"]).query)[
48
+ param
49
  ][0]
50
  except:
51
+ return default
52
+
53
+ def load_level(request: gr.Request):
54
+ level = _query_param(request, "level", "1")
55
+ age = _query_param(request, "age", "12")
56
+ name = _query_param(request, "name", "")
57
  with open(f"levels/{level}/metadata.json", "r") as f:
58
  data = json.load(f)
59
  instructions = open(f"levels/{level}/{data['instructions']}", "r").read()
60
  starter_code = open(f"levels/{level}/{data['starter_code']}", "r").read()
61
+ return [instructions, starter_code, age, name]
62
 
63
 
64
  with gr.Blocks(title="CS Tutor") as demo:
65
  tutor_ctx = gr.State()
66
+ age = gr.State()
67
+ name = gr.State()
68
  with gr.Row():
69
  instruction_panel = gr.Markdown()
70
  with gr.Row():
 
93
  run.click(
94
  run_code, editor, output, queue=False, scroll_to_output=True
95
  ).then(submit_code, [tutor_ctx, editor, output], [tutor_ctx, chatbot])
96
+ demo.load(load_level, None, [instruction_panel, editor, age, name], queue=False).then(
97
+ init_tutor, [instruction_panel, editor, age, name], [tutor_ctx, chatbot]
98
  )
99
 
100
  demo.queue()
prompts.py CHANGED
@@ -1,7 +1,10 @@
1
- def system_prompt(instructions, starter_code):
 
2
  template = f"""
3
  You are a computer science teacher helping a student learn computer science. You are friendly and want your students to succeed.
4
 
 
 
5
  The student has been asked to complete the following exercise in Python:
6
  ```
7
  {instructions}
 
1
+ def system_prompt(instructions, starter_code, age, name):
2
+ name = f"Your student's name is {name} and they are {age} years old." if name != "" else "Your student is {age} years old."
3
  template = f"""
4
  You are a computer science teacher helping a student learn computer science. You are friendly and want your students to succeed.
5
 
6
+ {name}
7
+
8
  The student has been asked to complete the following exercise in Python:
9
  ```
10
  {instructions}
tutor.py CHANGED
@@ -8,7 +8,7 @@ STUDENT = "user"
8
 
9
 
10
  class Tutor:
11
- def __init__(self, instructions="", starter_code="", context=None, debug=False):
12
  self.model = "gpt-4"
13
  self.temperature = 0.0
14
  self.api_key = os.getenv("OPENAI_API_KEY")
@@ -20,8 +20,10 @@ class Tutor:
20
  self.memory = []
21
  self.instructions = instructions
22
  self.starter_code = starter_code
 
 
23
  self.memory.append(
24
- {SYSTEM: system_prompt(self.instructions, self.starter_code)}
25
  )
26
  self.memory.append({TEACHER: welcome_prompt()})
27
 
@@ -31,6 +33,8 @@ class Tutor:
31
  "instructions": self.instructions,
32
  "starter_code": self.starter_code,
33
  "memory": self.memory,
 
 
34
  }
35
 
36
  def deserialize(self, data):
@@ -39,6 +43,8 @@ class Tutor:
39
  self.memory = data["memory"]
40
  self.instructions = data["instructions"]
41
  self.starter_code = data["starter_code"]
 
 
42
  else:
43
  raise ValueError("Input must be a dictionary containing 'memory'")
44
 
@@ -113,7 +119,7 @@ class Tutor:
113
  messages.append(
114
  {
115
  "role": SYSTEM,
116
- "content": system_prompt(self.instructions, self.starter_code),
117
  }
118
  )
119
  for entry in self.memory:
 
8
 
9
 
10
  class Tutor:
11
+ def __init__(self, instructions="", starter_code="", age="12", name="", context=None, debug=False):
12
  self.model = "gpt-4"
13
  self.temperature = 0.0
14
  self.api_key = os.getenv("OPENAI_API_KEY")
 
20
  self.memory = []
21
  self.instructions = instructions
22
  self.starter_code = starter_code
23
+ self.age = age
24
+ self.name = name
25
  self.memory.append(
26
+ {SYSTEM: system_prompt(self.instructions, self.starter_code, self.age, self.name)}
27
  )
28
  self.memory.append({TEACHER: welcome_prompt()})
29
 
 
33
  "instructions": self.instructions,
34
  "starter_code": self.starter_code,
35
  "memory": self.memory,
36
+ "age": self.age,
37
+ "name": self.name,
38
  }
39
 
40
  def deserialize(self, data):
 
43
  self.memory = data["memory"]
44
  self.instructions = data["instructions"]
45
  self.starter_code = data["starter_code"]
46
+ self.age = data["age"]
47
+ self.name = data["name"]
48
  else:
49
  raise ValueError("Input must be a dictionary containing 'memory'")
50
 
 
119
  messages.append(
120
  {
121
  "role": SYSTEM,
122
+ "content": system_prompt(self.instructions, self.starter_code, self.age, self.name),
123
  }
124
  )
125
  for entry in self.memory: