theRealNG commited on
Commit
a78cc33
·
1 Parent(s): f551bf5

workflows(tilv2): migrate tilv2 prompt to langsmith

Browse files
Files changed (3) hide show
  1. ui/til_feedback.py +2 -2
  2. ui_main.py +10 -10
  3. workflows/til/analyse_til_v2.py +8 -34
ui/til_feedback.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from dotenv import load_dotenv
3
  from workflows.til.analyse_til import TilCrew
 
4
  from streamlit_extras.capture import stdout
5
  from workflows.utils.feedback import Feedback
6
 
@@ -63,7 +64,7 @@ def feedback_main():
63
  with st.container(height=500, border=False):
64
  log_container = st.empty()
65
  with stdout(log_container.code, terminator=""):
66
- feedback = TilCrew()
67
  inputs = {"content": til_content}
68
  results = feedback.kickoff(inputs=inputs)
69
  status.update(
@@ -81,7 +82,6 @@ def feedback_main():
81
  st.markdown(f"#### TIL: {result['til']}")
82
  st.markdown(f"**Feedback:** {result['feedback']}")
83
  if result['feedback'] == "not_ok":
84
- st.markdown(f"**Criteria:** {result['feedback_criteria']}")
85
  st.markdown(f"**Reason:** {result['reason']}")
86
  if result.get('suggestion') is not None:
87
  st.markdown(f"**Suggestion:** {result['suggestion']}")
 
1
  import streamlit as st
2
  from dotenv import load_dotenv
3
  from workflows.til.analyse_til import TilCrew
4
+ from workflows.til.analyse_til_v2 import AnalyseTilV2
5
  from streamlit_extras.capture import stdout
6
  from workflows.utils.feedback import Feedback
7
 
 
64
  with st.container(height=500, border=False):
65
  log_container = st.empty()
66
  with stdout(log_container.code, terminator=""):
67
+ feedback = AnalyseTilV2()
68
  inputs = {"content": til_content}
69
  results = feedback.kickoff(inputs=inputs)
70
  status.update(
 
82
  st.markdown(f"#### TIL: {result['til']}")
83
  st.markdown(f"**Feedback:** {result['feedback']}")
84
  if result['feedback'] == "not_ok":
 
85
  st.markdown(f"**Reason:** {result['reason']}")
86
  if result.get('suggestion') is not None:
87
  st.markdown(f"**Suggestion:** {result['suggestion']}")
ui_main.py CHANGED
@@ -1,6 +1,6 @@
1
  from dotenv import load_dotenv
2
  from streamlit_extras.stylable_container import stylable_container
3
- from ui.til_feedback import feedback_main
4
  from ui.course_learn_suggest_expectations import course_suggester_main
5
  import math
6
  import streamlit as st
@@ -42,12 +42,12 @@ def show_main_page():
42
  }
43
  [data-testid="column"] {
44
  text-align: center;
45
- }
46
-
47
  </style>
48
  '''
49
  st.markdown(page_bg_img, unsafe_allow_html=True)
50
-
51
  css = load_css("ui/main.css")
52
 
53
  st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
@@ -77,7 +77,7 @@ def show_main_page():
77
  with st.container(height=280,border=False):
78
  with stylable_container(
79
  key="inside_container_with_border",
80
-
81
  css_styles="""
82
  {
83
  background-color: #e9eaec;
@@ -97,7 +97,7 @@ def show_main_page():
97
  st.markdown(
98
  f"""
99
  <div style="
100
- font-size: 4rem;
101
  margin-bottom: 10px;
102
  ">
103
  {card['image']}
@@ -125,14 +125,14 @@ def show_main_page():
125
  # border-radius: 50%;
126
  # border: 2px solid white;
127
 
128
- # }
129
 
130
  [data-testid="column"] div[data-testid="column"]{
131
  margin: 10px;
132
- }
133
  </style>
134
  """, unsafe_allow_html=True
135
- )
136
 
137
 
138
  def feedback_main_wrapper():
@@ -144,7 +144,7 @@ def course_suggester_main_wrapper():
144
  course_suggester_main()
145
 
146
 
147
-
148
  router = sr.StreamlitRouter()
149
 
150
  router.register(show_main_page, '/')
 
1
  from dotenv import load_dotenv
2
  from streamlit_extras.stylable_container import stylable_container
3
+ from ui.til_feedback import feedback_main
4
  from ui.course_learn_suggest_expectations import course_suggester_main
5
  import math
6
  import streamlit as st
 
42
  }
43
  [data-testid="column"] {
44
  text-align: center;
45
+ }
46
+
47
  </style>
48
  '''
49
  st.markdown(page_bg_img, unsafe_allow_html=True)
50
+
51
  css = load_css("ui/main.css")
52
 
53
  st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
 
77
  with st.container(height=280,border=False):
78
  with stylable_container(
79
  key="inside_container_with_border",
80
+
81
  css_styles="""
82
  {
83
  background-color: #e9eaec;
 
97
  st.markdown(
98
  f"""
99
  <div style="
100
+ font-size: 4rem;
101
  margin-bottom: 10px;
102
  ">
103
  {card['image']}
 
125
  # border-radius: 50%;
126
  # border: 2px solid white;
127
 
128
+ # }
129
 
130
  [data-testid="column"] div[data-testid="column"]{
131
  margin: 10px;
132
+ }
133
  </style>
134
  """, unsafe_allow_html=True
135
+ )
136
 
137
 
138
  def feedback_main_wrapper():
 
144
  course_suggester_main()
145
 
146
 
147
+
148
  router = sr.StreamlitRouter()
149
 
150
  router.register(show_main_page, '/')
workflows/til/analyse_til_v2.py CHANGED
@@ -1,4 +1,4 @@
1
- from langchain import callbacks
2
  from langchain_core.messages import SystemMessage
3
  from langchain_core.output_parsers import JsonOutputParser
4
  from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
@@ -28,14 +28,12 @@ class AnalyseTilV2:
28
  "feedback": "not_ok",
29
  }
30
  if feedback["factuality_categorization"] != 'High':
31
- result["feedback_criteria"] = "factuality_feedback"
32
- result["reason"] = feedback["factuality_reason"]
33
  final_results = final_results + [result]
34
  continue
35
 
36
  if feedback["insightful_categorization"] != 'High':
37
- result["feedback_criteria"] = "insightful_feedback"
38
- result["reason"] = feedback["insightful_reason"]
39
  final_results = final_results + [result]
40
  continue
41
 
@@ -49,10 +47,11 @@ class AnalyseTilV2:
49
 
50
  def _gather_feedback(self):
51
  feedback_chain = self._build_feedback_chain()
 
52
  pprint.pp("Analysing the TIL.....")
53
  with callbacks.collect_runs() as cb:
54
  self.feedback_results = feedback_chain.invoke(
55
- {"til_content": self.content})['tils']
56
  self.run_id = cb.traced_runs[0].id
57
  print("Run ID: ", self.run_id)
58
 
@@ -61,28 +60,7 @@ class AnalyseTilV2:
61
 
62
  def _build_feedback_chain(self):
63
  feedback_parser = JsonOutputParser(pydantic_object=TilV2FeedbackResults)
64
- feedback_prompt = ChatPromptTemplate.from_messages([
65
- SystemMessage(
66
- "You are a 'Personal TIL Reviewer' who works in a Product Engineering Services company. "
67
- "You are an expert in writing TILs which are Insightful, Factually correct, Easy to read and grammatically correct."
68
- "Your goal is to review user's TILs and categorize their correctness as High, Medium, or Low based on the following metrics:"
69
- "1. Is the TIL insightful?"
70
- "2. Is the TIL factually correct and accurate?"
71
-
72
- "The criteria to use for assessing if they are insightful or not are:\n"
73
- "* They TIL shouldn't just be a outright statement, it should contain even the reason on why the statement is true."
74
- "* It should showcase the understanding of the user on the subject.\n\n"
75
-
76
- "The criteria to use for assessing if they are factual or not are:\n"
77
- "* They are related to facts."
78
- "* You are able to find a source which agrees to the fact from reputable websites.\n\n"
79
-
80
- "Give reason for your assessment in one or two sentences for each metric. "
81
- "Evaluate each TIL in the context of all the user's TILs."
82
- f"Formatting Instructions: {feedback_parser.get_format_instructions()}"
83
- ),
84
- HumanMessagePromptTemplate.from_template("{til_content}")
85
- ])
86
  print("Prompt: ")
87
  pprint.pp(feedback_prompt, width=80)
88
  llm = ChatOpenAI(model=os.environ["OPENAI_MODEL"], temperature=0.2)
@@ -104,12 +82,10 @@ class TilV2FeedbackResult(BaseModel):
104
  description="TIL as exactly captured by the user without any modifications.")
105
  insightful_categorization: str = Field(
106
  description="TIL categorization as High/Medium/Low based on correctness on the insightful metric.")
107
- insightful_reason: str = Field(
108
- description="Reason for your assessment in one or two sentences on insightful metric for the user.")
109
  factuality_categorization: str = Field(
110
  description="TIL categorization as High/Medium/Low based on correctness on the factuality metric.")
111
- factuality_reason: str = Field(
112
- description="Reason for your assessment in one or two sentences on factuality metric for the user.")
113
 
114
 
115
  class TilV2FeedbackResults(BaseModel):
@@ -119,9 +95,7 @@ class TilV2FeedbackResults(BaseModel):
119
  class TilV2FinalFeedback(BaseModel):
120
  til: str
121
  feedback: str
122
- feedback_criteria: Optional[str] = None
123
  reason: Optional[str] = None
124
- suggestion: Optional[str] = None
125
 
126
 
127
  class TilV2FeedbackResponse(BaseModel):
 
1
+ from langchain import callbacks, hub
2
  from langchain_core.messages import SystemMessage
3
  from langchain_core.output_parsers import JsonOutputParser
4
  from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
 
28
  "feedback": "not_ok",
29
  }
30
  if feedback["factuality_categorization"] != 'High':
31
+ result["reason"] = feedback["assessment_reason"]
 
32
  final_results = final_results + [result]
33
  continue
34
 
35
  if feedback["insightful_categorization"] != 'High':
36
+ result["reason"] = feedback["assessment_reason"]
 
37
  final_results = final_results + [result]
38
  continue
39
 
 
47
 
48
  def _gather_feedback(self):
49
  feedback_chain = self._build_feedback_chain()
50
+ feedback_parser = JsonOutputParser(pydantic_object=TilV2FeedbackResults)
51
  pprint.pp("Analysing the TIL.....")
52
  with callbacks.collect_runs() as cb:
53
  self.feedback_results = feedback_chain.invoke(
54
+ {"til_content": self.content, "format_instructions": feedback_parser.get_format_instructions()})['tils']
55
  self.run_id = cb.traced_runs[0].id
56
  print("Run ID: ", self.run_id)
57
 
 
60
 
61
  def _build_feedback_chain(self):
62
  feedback_parser = JsonOutputParser(pydantic_object=TilV2FeedbackResults)
63
+ feedback_prompt = hub.pull("til_analysis")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  print("Prompt: ")
65
  pprint.pp(feedback_prompt, width=80)
66
  llm = ChatOpenAI(model=os.environ["OPENAI_MODEL"], temperature=0.2)
 
82
  description="TIL as exactly captured by the user without any modifications.")
83
  insightful_categorization: str = Field(
84
  description="TIL categorization as High/Medium/Low based on correctness on the insightful metric.")
 
 
85
  factuality_categorization: str = Field(
86
  description="TIL categorization as High/Medium/Low based on correctness on the factuality metric.")
87
+ assessment_reason: str = Field(
88
+ description="Reason for your assessment in one or two sentences on factuality metric and insightful metric for the user if they are not High. The assesment should be from FPV")
89
 
90
 
91
  class TilV2FeedbackResults(BaseModel):
 
95
  class TilV2FinalFeedback(BaseModel):
96
  til: str
97
  feedback: str
 
98
  reason: Optional[str] = None
 
99
 
100
 
101
  class TilV2FeedbackResponse(BaseModel):