BtB-ExpC commited on
Commit
cf5b4fc
·
1 Parent(s): b687311
chains/learning_objectives_generator/learning_objectives_chain.py CHANGED
@@ -6,76 +6,7 @@ from langchain_core.prompts.chat import ChatPromptTemplate
6
 
7
  class LearningObjectivesChain(BaseModel):
8
  """
9
-
10
  """
11
 
12
- # Templates
13
- template_generator: ChatPromptTemplate
14
- template_eliminator: ChatPromptTemplate
15
- template_finetuner: ChatPromptTemplate
16
- template_presenter: ChatPromptTemplate
17
-
18
- # LLM references
19
- llm_main: Any # The "Main LLM"
20
- llm_alt: Any # The "Other LLM" used only for the second generator prompt
21
-
22
- async def run(self, standardized_studytext: str) -> str:
23
- """
24
- Main pipeline for a single run. The 'standardized_studytext' is assumed
25
- to be already standardized outside of this chain, so we jump straight to generation.
26
- """
27
-
28
- # 1) Two parallel calls to learning_objective_generator
29
- # with different LLMs (llm_main vs. llm_alt).
30
-
31
- async def run_generator(llm, llm_label) -> str:
32
- # Format the generator prompt
33
- prompt = await self.template_generator.aformat_prompt(
34
- standardized_studytext=standardized_studytext, # possibly other variables
35
- llm_label=llm_label
36
- )
37
- messages = prompt.to_messages()
38
- response = await llm.ainvoke(messages)
39
- return getattr(response, "content", response)
40
-
41
- # Launch in parallel
42
- gen_main_task = asyncio.create_task(run_generator(self.llm_main, "MAIN"))
43
- gen_alt_task = asyncio.create_task(run_generator(self.llm_alt, "ALT"))
44
-
45
- # Wait for both to finish
46
- gen_main_result, gen_alt_result = await asyncio.gather(gen_main_task, gen_alt_task)
47
-
48
- # Combine them
49
- combined_generators = f"[GEN MAIN]\n{gen_main_result}\n\n[GEN ALT]\n{gen_alt_result}"
50
-
51
- # 2) learning_objective_eliminator (llm_main)
52
- prompt_eliminate = await self.template_eliminator.aformat_prompt(
53
- combined_generators=combined_generators,
54
- standardized_studytext=standardized_studytext
55
- )
56
- elim_messages = prompt_eliminate.to_messages()
57
- elim_response = await self.llm_main.ainvoke(elim_messages)
58
- elim_output = getattr(elim_response, "content", elim_response)
59
-
60
- # 3) learning_objective_finetuner (llm_main)
61
- prompt_fine = await self.template_finetuner.aformat_prompt(
62
- elimination_output=elim_output,
63
- standardized_studytext=standardized_studytext
64
- )
65
- fine_messages = prompt_fine.to_messages()
66
- fine_response = await self.llm_main.ainvoke(fine_messages)
67
- fine_output = getattr(fine_response, "content", fine_response)
68
-
69
- # 4) learning_objective_presenter (llm_main)
70
- prompt_present = await self.template_presenter.aformat_prompt(
71
- finetuned_output=fine_output,
72
- standardized_studytext=standardized_studytext
73
- )
74
- present_messages = prompt_present.to_messages()
75
- present_response = await self.llm_main.ainvoke(present_messages)
76
- final_output = getattr(present_response, "content", present_response)
77
-
78
- return final_output
79
-
80
  class Config:
81
  arbitrary_types_allowed = True
 
6
 
7
  class LearningObjectivesChain(BaseModel):
8
  """
 
9
  """
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  class Config:
12
  arbitrary_types_allowed = True
chains/learning_objectives_generator/runner.py CHANGED
@@ -34,6 +34,8 @@ async def run_learning_objectives_generator(
34
  llm_a = llms.get(model_choice_1, config["default_llm_a"])
35
  llm_b = llms.get(model_choice_2, config["default_llm_b"])
36
 
 
 
37
  # We will store the final sanitized results in an array of 4 strings
38
  # (2 prompts × 2 LLMs)
39
  partial_results = ["", "", "", ""]
@@ -50,7 +52,7 @@ async def run_learning_objectives_generator(
50
 
51
  # Step: sanitize
52
  sanitize_msg = await sanitize_prompt.aformat_prompt(raw_output=generation_output)
53
- sanitize_resp = await llm_a.ainvoke(sanitize_msg.to_messages()) # or use a separate LLM for sanitization
54
  sanitized_output = getattr(sanitize_resp, "content", sanitize_resp)
55
 
56
  return (track_index, sanitized_output)
 
34
  llm_a = llms.get(model_choice_1, config["default_llm_a"])
35
  llm_b = llms.get(model_choice_2, config["default_llm_b"])
36
 
37
+ llm_sanitize=llms.get(config["llm_sanitize"])
38
+
39
  # We will store the final sanitized results in an array of 4 strings
40
  # (2 prompts × 2 LLMs)
41
  partial_results = ["", "", "", ""]
 
52
 
53
  # Step: sanitize
54
  sanitize_msg = await sanitize_prompt.aformat_prompt(raw_output=generation_output)
55
+ sanitize_resp = await llm_sanitize.ainvoke(sanitize_msg.to_messages()) # or use a separate LLM for sanitization
56
  sanitized_output = getattr(sanitize_resp, "content", sanitize_resp)
57
 
58
  return (track_index, sanitized_output)
config/chain_configs.py CHANGED
@@ -55,7 +55,11 @@ chain_configs = {
55
  "template_gen_prompt_a": template_gen_prompt_a,
56
  "template_gen_prompt_b": template_gen_prompt_b,
57
  "default_llm_a": llms["o1"],
58
- "default_llm_b": llms["o3-mini (high reasoning_effort)"]
 
 
 
 
59
  },
60
  }
61
 
 
55
  "template_gen_prompt_a": template_gen_prompt_a,
56
  "template_gen_prompt_b": template_gen_prompt_b,
57
  "default_llm_a": llms["o1"],
58
+ "default_llm_b": llms["o3-mini (high reasoning_effort)"],
59
+ "template_sanitize": template_sanitize_learning_objectives,
60
+ "llm_sanitize": llms["GPT-4o-mini (zero temp)"],
61
+
62
+
63
  },
64
  }
65
 
config/templates.py CHANGED
@@ -368,3 +368,13 @@ The latter objective does not specify a single fact but combines two (can be pai
368
  ],
369
  input_variables=["standardized_text"]
370
  )
 
 
 
 
 
 
 
 
 
 
 
368
  ],
369
  input_variables=["standardized_text"]
370
  )
371
+
372
+ template_sanitize_learning_objectives = ChatPromptTemplate(
373
+ messages=[
374
+ ("system", "You are given an output of a brainstorming session that lead to the generation of learning objectives. Your task is to "
375
+ "turn distill this into a numbered list of just the learning objectives."),
376
+ ("human", "Here is the output:\n "
377
+ "{raw_output}")
378
+ ],
379
+ input_variables=["raw_output"]
380
+ )