Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -105,136 +105,154 @@ for subject in incomplete_task_list_json["subjects"]:
|
|
| 105 |
|
| 106 |
# Schema for structured output to use in planning
|
| 107 |
|
| 108 |
-
class Section(BaseModel): # This part defines the structure of a single section of the report, saying that it should return a field name and a description
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
|
| 117 |
-
class Sections(BaseModel): # This part defines what has to be the output of the llm
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
|
| 122 |
|
| 123 |
-
# Augment the LLM with schema for structured output
|
| 124 |
-
planner = llm.with_structured_output(Sections)
|
| 125 |
|
| 126 |
from langgraph.constants import Send
|
| 127 |
|
| 128 |
# This is a state which defines all params that we need to collect in this WF
|
| 129 |
# Graph state
|
| 130 |
-
class State(TypedDict):
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
# Worker state
|
| 140 |
-
class WorkerState(TypedDict):
|
| 141 |
-
section: Section
|
| 142 |
-
completed_sections: Annotated[list, operator.add]
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# Nodes
|
| 146 |
-
def orchestrator(state: State):
|
| 147 |
-
"""Orchestrator that generates a plan for the report"""
|
| 148 |
-
|
| 149 |
-
# Generate queries
|
| 150 |
-
report_sections = planner.invoke(
|
| 151 |
-
[
|
| 152 |
-
SystemMessage(content="""Generate a plan for the report. The report is a review and analysis output, which tells us about
|
| 153 |
-
the study pattern, studying hours, non studying hours and future action plan for the user based on
|
| 154 |
-
performance of task completion on a day of the user. We only want to understand about how the student studies nothing apart from that
|
| 155 |
-
Dont halucinate the data, just keep focus on provided data, no need for data out of the box.
|
| 156 |
-
I want a very short report to understand how many tasks student completed and then what is the studying pattern of the user
|
| 157 |
-
**Do not generate more than 5 sections**
|
| 158 |
-
"""),
|
| 159 |
-
HumanMessage(content=f"Here is the Previous day roadmap of the user: {state['previous_day_roadmap']}"),
|
| 160 |
-
]
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
return {"sections": report_sections.sections}
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
def llm_call(state: WorkerState):
|
| 167 |
-
"""Worker writes a section of the report"""
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
tentative that task can take time
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
content=f"Here is the section name: {state['section'].name} and description: {state['section'].description}"
|
| 183 |
-
),
|
| 184 |
]
|
| 185 |
)
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
return {"completed_sections": [section.content]}
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
def synthesizer(state: State):
|
| 192 |
-
"""Synthesize full report from sections"""
|
| 193 |
-
|
| 194 |
-
# List of completed sections
|
| 195 |
-
completed_sections = state["completed_sections"]
|
| 196 |
-
|
| 197 |
-
# Format completed section to str to use as context for final sections
|
| 198 |
-
completed_report_sections = "\n\n---\n\n".join(completed_sections)
|
| 199 |
-
|
| 200 |
-
return {"final_report": completed_report_sections}
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
# Conditional edge function to create llm_call workers that each write a section of the report
|
| 204 |
-
def assign_workers(state: State):
|
| 205 |
-
"""Assign a worker to each section in the plan"""
|
| 206 |
-
|
| 207 |
-
# Kick off section writing in parallel via Send() API
|
| 208 |
-
return [Send("llm_call", {"section": s}) for s in state["sections"]]
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
# Build workflow
|
| 212 |
-
orchestrator_worker_builder = StateGraph(State)
|
| 213 |
-
|
| 214 |
-
# Add the nodes
|
| 215 |
-
orchestrator_worker_builder.add_node("orchestrator", orchestrator)
|
| 216 |
-
orchestrator_worker_builder.add_node("llm_call", llm_call)
|
| 217 |
-
orchestrator_worker_builder.add_node("synthesizer", synthesizer)
|
| 218 |
-
|
| 219 |
-
# Add edges to connect nodes
|
| 220 |
-
orchestrator_worker_builder.add_edge(START, "orchestrator")
|
| 221 |
-
orchestrator_worker_builder.add_conditional_edges(
|
| 222 |
-
"orchestrator", assign_workers, ["llm_call"]
|
| 223 |
-
)
|
| 224 |
-
orchestrator_worker_builder.add_edge("llm_call", "synthesizer")
|
| 225 |
-
orchestrator_worker_builder.add_edge("synthesizer", END)
|
| 226 |
-
|
| 227 |
-
# Compile the workflow
|
| 228 |
-
orchestrator_worker = orchestrator_worker_builder.compile()
|
| 229 |
-
|
| 230 |
-
# # Show the workflow
|
| 231 |
-
# display(Image(orchestrator_worker.get_graph().draw_mermaid_png()))
|
| 232 |
-
|
| 233 |
-
# Invoke
|
| 234 |
-
state = orchestrator_worker.invoke({"previous_day_roadmap": f"{previous_day_roadmap_str}"})
|
| 235 |
-
|
| 236 |
-
from IPython.display import Markdown
|
| 237 |
-
final_report = state["final_report"] #-->display 2
|
| 238 |
|
| 239 |
#Evaluator-optimizer approach
|
| 240 |
def remove_the_first_day(roadmap):
|
|
|
|
| 105 |
|
| 106 |
# Schema for structured output to use in planning
|
| 107 |
|
| 108 |
+
# class Section(BaseModel): # This part defines the structure of a single section of the report, saying that it should return a field name and a description
|
| 109 |
+
# name: str = Field(
|
| 110 |
+
# description="Name for this section of the report.",
|
| 111 |
+
# )
|
| 112 |
+
# description: str = Field(
|
| 113 |
+
# description="""Very short overview of the main topics and concepts to be covered in this section.""",
|
| 114 |
+
# )
|
| 115 |
|
| 116 |
|
| 117 |
+
# class Sections(BaseModel): # This part defines what has to be the output of the llm
|
| 118 |
+
# sections: List[Section] = Field(
|
| 119 |
+
# description="Sections of the report.",
|
| 120 |
+
# )
|
| 121 |
|
| 122 |
|
| 123 |
+
# # Augment the LLM with schema for structured output
|
| 124 |
+
# planner = llm.with_structured_output(Sections)
|
| 125 |
|
| 126 |
from langgraph.constants import Send
|
| 127 |
|
| 128 |
# This is a state which defines all params that we need to collect in this WF
|
| 129 |
# Graph state
|
| 130 |
+
# class State(TypedDict):
|
| 131 |
+
# previous_day_roadmap: str # Report topic
|
| 132 |
+
# sections: list[Section] # List of report sections
|
| 133 |
+
# completed_sections: Annotated[
|
| 134 |
+
# list, operator.add
|
| 135 |
+
# ] # All workers write to this key in parallel
|
| 136 |
+
# final_report: str # Final report
|
| 137 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
# # Worker state
|
| 140 |
+
# class WorkerState(TypedDict):
|
| 141 |
+
# section: Section
|
| 142 |
+
# completed_sections: Annotated[list, operator.add]
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# # Nodes
|
| 146 |
+
# def orchestrator(state: State):
|
| 147 |
+
# """Orchestrator that generates a plan for the report"""
|
| 148 |
+
|
| 149 |
+
# # Generate queries
|
| 150 |
+
# report_sections = planner.invoke(
|
| 151 |
+
# [
|
| 152 |
+
# SystemMessage(content="""Generate a plan for the report. The report is a review and analysis output, which tells us about
|
| 153 |
+
# the study pattern, studying hours, non studying hours and future action plan for the user based on
|
| 154 |
+
# performance of task completion on a day of the user. We only want to understand about how the student studies nothing apart from that
|
| 155 |
+
# Dont halucinate the data, just keep focus on provided data, no need for data out of the box.
|
| 156 |
+
# I want a very short report to understand how many tasks student completed and then what is the studying pattern of the user
|
| 157 |
+
# **Do not generate more than 5 sections**
|
| 158 |
+
# """),
|
| 159 |
+
# HumanMessage(content=f"Here is the Previous day roadmap of the user: {state['previous_day_roadmap']}"),
|
| 160 |
+
# ]
|
| 161 |
+
# )
|
| 162 |
+
|
| 163 |
+
# return {"sections": report_sections.sections}
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
# def llm_call(state: WorkerState):
|
| 167 |
+
# """Worker writes a section of the report"""
|
| 168 |
+
|
| 169 |
+
# # Generate section
|
| 170 |
+
# section = llm.invoke(
|
| 171 |
+
# [
|
| 172 |
+
# SystemMessage(
|
| 173 |
+
# content="""Write a report section following the provided name and description. Make this report userful for parents, teachers
|
| 174 |
+
# and students himself, also try to motivate the student.This report is for a Joint Entrance Examination aspirant.
|
| 175 |
+
# Keep the report very short but will all clear parameter and make sure the report is not so big but contains all the necessary
|
| 176 |
+
# details of the student's day. Keep the report very short and avoid texts and focus on numbers
|
| 177 |
+
# Include no preamble for each section.Make sure that a task is completed only when the "task_completed" key is true and the "time" key tells about how much
|
| 178 |
+
# tentative that task can take time
|
| 179 |
+
# Use markdown formatting."""
|
| 180 |
+
# ),
|
| 181 |
+
# HumanMessage(
|
| 182 |
+
# content=f"Here is the section name: {state['section'].name} and description: {state['section'].description}"
|
| 183 |
+
# ),
|
| 184 |
+
# ]
|
| 185 |
+
# )
|
| 186 |
+
|
| 187 |
+
# # Write the updated section to completed sections
|
| 188 |
+
# return {"completed_sections": [section.content]}
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
# def synthesizer(state: State):
|
| 192 |
+
# """Synthesize full report from sections"""
|
| 193 |
+
|
| 194 |
+
# # List of completed sections
|
| 195 |
+
# completed_sections = state["completed_sections"]
|
| 196 |
+
|
| 197 |
+
# # Format completed section to str to use as context for final sections
|
| 198 |
+
# completed_report_sections = "\n\n---\n\n".join(completed_sections)
|
| 199 |
+
|
| 200 |
+
# return {"final_report": completed_report_sections}
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# # Conditional edge function to create llm_call workers that each write a section of the report
|
| 204 |
+
# def assign_workers(state: State):
|
| 205 |
+
# """Assign a worker to each section in the plan"""
|
| 206 |
+
|
| 207 |
+
# # Kick off section writing in parallel via Send() API
|
| 208 |
+
# return [Send("llm_call", {"section": s}) for s in state["sections"]]
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
# # Build workflow
|
| 212 |
+
# orchestrator_worker_builder = StateGraph(State)
|
| 213 |
+
|
| 214 |
+
# # Add the nodes
|
| 215 |
+
# orchestrator_worker_builder.add_node("orchestrator", orchestrator)
|
| 216 |
+
# orchestrator_worker_builder.add_node("llm_call", llm_call)
|
| 217 |
+
# orchestrator_worker_builder.add_node("synthesizer", synthesizer)
|
| 218 |
+
|
| 219 |
+
# # Add edges to connect nodes
|
| 220 |
+
# orchestrator_worker_builder.add_edge(START, "orchestrator")
|
| 221 |
+
# orchestrator_worker_builder.add_conditional_edges(
|
| 222 |
+
# "orchestrator", assign_workers, ["llm_call"]
|
| 223 |
+
# )
|
| 224 |
+
# orchestrator_worker_builder.add_edge("llm_call", "synthesizer")
|
| 225 |
+
# orchestrator_worker_builder.add_edge("synthesizer", END)
|
| 226 |
+
|
| 227 |
+
# # Compile the workflow
|
| 228 |
+
# orchestrator_worker = orchestrator_worker_builder.compile()
|
| 229 |
+
|
| 230 |
+
# # # Show the workflow
|
| 231 |
+
# # display(Image(orchestrator_worker.get_graph().draw_mermaid_png()))
|
| 232 |
+
|
| 233 |
+
# # Invoke
|
| 234 |
+
# state = orchestrator_worker.invoke({"previous_day_roadmap": f"{previous_day_roadmap_str}"})
|
| 235 |
+
|
| 236 |
+
# from IPython.display import Markdown
|
| 237 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 238 |
+
|
| 239 |
+
response = client.chat.completions.create(
|
| 240 |
+
model="gpt-4o-mini",
|
| 241 |
+
messages=[
|
| 242 |
+
{"role": "system", "content": """You will be given a JEE student's previous_day_roadmap and then you have to create
|
| 243 |
+
a completely interactive and useful report for the user.
|
| 244 |
+
The report should include a table for task completion rates and data
|
| 245 |
+
The student's study pattern
|
| 246 |
+
The student's weaknesses and tips to improve.
|
| 247 |
+
Make sure that a task is completed only when the "task_completed" key is true and the "time" key tells about how much
|
| 248 |
tentative that task can take time
|
| 249 |
+
Use markdown formatting.
|
| 250 |
+
"""},
|
| 251 |
+
{"role": "user", "content": f"""Here is the user's previous day roadmap in json : {previous_day_roadmap_str}"""}
|
|
|
|
|
|
|
| 252 |
]
|
| 253 |
)
|
| 254 |
+
output = response.choices[0].message.content
|
| 255 |
+
final_report = output #-->display 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
#Evaluator-optimizer approach
|
| 258 |
def remove_the_first_day(roadmap):
|