theRealNG commited on
Commit
81e622d
·
1 Parent(s): cb8ca6d

workflows(check_question): Add support for suggest check question

Browse files
endpoints.py CHANGED
@@ -1,15 +1,17 @@
1
- import uvicorn
2
- from typing import List, Optional
3
- from pydantic import UUID4, BaseModel
4
- from fastapi.middleware.cors import CORSMiddleware
5
- from fastapi import FastAPI, Query
6
- from .workflows.utils.feedback import Feedback, post_feedback
7
- from .workflows.til import TilCrew, TilFeedbackResponse
8
- from .workflows.courses.suggest_expectations import SuggestExpectations, Inputs as SuggestExpectationsInputs, Expectation, Response as SuggestExpectationsResponse
9
- from .workflows.courses.expectation_revision import ExpectationRevision, Inputs as ExpectationRevisionInputs, Response as ExpectationRevisionResponse
10
  from dotenv import load_dotenv
11
  load_dotenv()
12
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  description = """
15
  API helps you do awesome stuff. 🚀
@@ -101,6 +103,26 @@ async def capture_expectation_revision_feedback(run_id: UUID4, feedback: Feedbac
101
  return "ok"
102
 
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  @app.get("/healthcheck")
105
  async def read_root():
106
  return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
1
  from dotenv import load_dotenv
2
  load_dotenv()
3
 
4
+ from .workflows.courses.expectation_revision import ExpectationRevision, Inputs as ExpectationRevisionInputs, Response as ExpectationRevisionResponse
5
+ from .workflows.courses.suggest_check_question import SuggestCheckQuestion, Inputs as SuggestCheckQuestionInputs, Response as SuggestCheckQuestionResponse
6
+ from .workflows.courses.suggest_expectations import SuggestExpectations, Inputs as SuggestExpectationsInputs, Expectation, Response as SuggestExpectationsResponse
7
+ from .workflows.til import TilCrew, TilFeedbackResponse
8
+ from .workflows.utils.feedback import Feedback, post_feedback
9
+ from fastapi import FastAPI, Query
10
+ from fastapi.middleware.cors import CORSMiddleware
11
+ from pydantic import UUID4, BaseModel
12
+ from typing import List, Optional
13
+ import uvicorn
14
+
15
 
16
  description = """
17
  API helps you do awesome stuff. 🚀
 
103
  return "ok"
104
 
105
 
106
+ @app.post("/course_learn/suggest_check_question", tags=["course_learn"])
107
+ async def course_learn_suggest_check_question(inputs: SuggestCheckQuestionInputs) -> SuggestCheckQuestionResponse:
108
+ print("Inputs: ", inputs)
109
+ result = SuggestCheckQuestion().kickoff(inputs={
110
+ "course": inputs.course,
111
+ "module": inputs.module,
112
+ "tasks": inputs.tasks,
113
+ "expectation": inputs.expectation,
114
+ })
115
+ return result
116
+
117
+
118
+ @app.post("/course_learn/suggest_check_question/{run_id}/feedback", tags=["course_learn"])
119
+ async def course_learn_suggest_check_question_feedback(run_id: UUID4, feedback: Feedback) -> str:
120
+ print("Helful Score: ", feedback.metric_type)
121
+ print("Feedback On: ", feedback.feedback_on)
122
+ post_feedback(run_id=run_id, feedback=feedback)
123
+ return "ok"
124
+
125
+
126
  @app.get("/healthcheck")
127
  async def read_root():
128
  return {"status": "ok"}
workflows/courses/suggest_check_question.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .suggest_expectations import Expectation
2
+ from langchain import hub, callbacks
3
+ from langchain_core.output_parsers import JsonOutputParser
4
+ from langchain_openai import ChatOpenAI
5
+ from pydantic import BaseModel, UUID4, Field
6
+ from typing import List
7
+ import os
8
+
9
+
10
+ class Inputs(BaseModel):
11
+ course: str
12
+ module: str
13
+ tasks: List[str]
14
+ expectation: str
15
+
16
+
17
+ class Response(BaseModel):
18
+ run_id: UUID4
19
+ expectation: str
20
+ check_question: str
21
+
22
+ class CheckQuestion(BaseModel):
23
+ check_question: str = Field(
24
+ description="Targeted question that the course designer have developed to assess the learner's understanding of the learning outcomes.")
25
+
26
+
27
+ class SuggestCheckQuestion:
28
+ def kickoff(self, inputs={}):
29
+ self.course = inputs["course"]
30
+ self.module = inputs["module"]
31
+ self.learning_outcome = inputs["expectation"]
32
+ self.tasks = inputs["tasks"]
33
+ llm_response = self._get_check_quesiton()
34
+ return {
35
+ "run_id": self.run_id,
36
+ "expectation": self.learning_outcome,
37
+ "check_question": llm_response["check_question"]
38
+ }
39
+
40
+ def _get_check_quesiton(self):
41
+ parser = JsonOutputParser(pydantic_object=CheckQuestion)
42
+ prompt = hub.pull("course_learn_suggest_check_question")
43
+ llm = ChatOpenAI(model=os.environ['OPENAI_MODEL'], temperature=0.2)
44
+ chain = (prompt | llm | parser).with_config({
45
+ "tags": ["course_learn", "suggest_check_question"], "run_name": "Suggest Module Expectations",
46
+ "metadata": {
47
+ "versoin": "v1.0.0",
48
+ "growth_activity": "course_learn",
49
+ "env": os.environ["ENV"],
50
+ "model": os.environ["OPENAI_MODEL"],
51
+ }
52
+ })
53
+
54
+ with callbacks.collect_runs() as cb:
55
+ llm_response = chain.invoke({
56
+ "course": self.course, "module": self.module, "tasks": "* " + ("\n* ".join(self.tasks)),
57
+ "format_instructions": parser.get_format_instructions(),
58
+ "learning_outcome": self.learning_outcome
59
+ })
60
+ self.run_id = cb.traced_runs[0].id
61
+
62
+ return llm_response