Skip to content

Commit 9a6cc87

Browse files
committed
mued API schema for response and request
1 parent b779cc6 commit 9a6cc87

File tree

12 files changed

+855
-944
lines changed

12 files changed

+855
-944
lines changed

index.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
11
import json
2+
from pydantic import ValidationError
3+
4+
from lf_toolkit.chat import ChatRequest
25
from src.module import chat_module
3-
from src.agent.utils.types import JsonType
46

5-
def handler(event: JsonType, context):
7+
8+
def handler(event, context):
69
"""
710
Lambda handler function
811
"""
9-
# Log the input event for debugging purposes
10-
# print("Received event:", " ".join(json.dumps(event, indent=2).splitlines()))
11-
1212
if "body" in event:
1313
try:
1414
event = json.loads(event["body"])
1515
except json.JSONDecodeError:
1616
return {
1717
"statusCode": 400,
18-
"body": "Invalid JSON format in the body or body not found. Please check the input."
18+
"body": "Invalid JSON format in the body. Please check the input.",
1919
}
2020

21-
if "message" not in event:
22-
return {
23-
"statusCode": 400,
24-
"body": "Missing 'message' key in event. Please confirm the key in the json body."
25-
}
26-
if "params" not in event:
27-
return {
28-
"statusCode": 400,
29-
"body": "Missing 'params' key in event. Please confirm the key in the json body. Make sure it contains the necessary conversation_id."
30-
}
31-
32-
message = event.get("message")
33-
params = event.get("params")
21+
try:
22+
request = ChatRequest.model_validate(event)
23+
except ValidationError as e:
24+
return {"statusCode": 400, "body": e.json()}
3425

3526
try:
36-
chatbot_response = chat_module(message, params)
27+
result = chat_module(request)
3728
except Exception as e:
3829
return {
3930
"statusCode": 500,
40-
"body": f"An error occurred within the chat_module(): {str(e)}"
31+
"body": f"An error occurred within the chat_module(): {str(e)}",
4132
}
4233

43-
# Create a response
44-
response = {
45-
"statusCode": 200,
46-
"body": json.dumps(chatbot_response)
47-
}
48-
49-
# Log the response for debugging purposes
34+
response = {"statusCode": 200, "body": result.model_dump_json()}
5035
print("Returning response:", " ".join(json.dumps(response, indent=2).splitlines()))
51-
52-
return response
36+
return response

src/agent/agent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ def invoke_base_agent(query: str, conversation_history: list, summary: str, conv
181181
print(f'in invoke_base_agent(), thread_id = {session_id}')
182182

183183
config = {"configurable": {"thread_id": session_id, "summary": summary, "conversational_style": conversationalStyle, "question_response_details": question_response_details}}
184-
response_events = agent.app.invoke({"messages": conversation_history, "summary": summary, "conversational_style": conversationalStyle}, config=config, stream_mode="values") #updates
184+
messages = conversation_history + [HumanMessage(content=query)]
185+
response_events = agent.app.invoke({"messages": messages, "summary": summary, "conversational_style": conversationalStyle}, config=config, stream_mode="values") #updates
185186
pretty_printed_response = agent.pretty_response_value(response_events) # get last event/ai answer in the response
186187

187188
# Gather Metadata from the agent
Lines changed: 128 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,138 @@
11
{
2-
"message": "i dont remember anything",
3-
"params": {
4-
"include_test_data": true,
5-
"conversation_history": [
6-
{ "type": "user", "content": "what should I do?" },
7-
{
8-
"type": "assistant",
9-
"content": "It seems like you're currently working on Part (a) of the dot product question. Since you haven't submitted an answer yet, let's take a moment to break it down together. \n\nWhat do you remember about how to calculate the dot product of two vectors? Can you describe the steps you would take?"
10-
},
11-
{ "type": "user", "content": "i dont remember anything" }
12-
],
13-
"summary": "",
14-
"conversational_style": "",
15-
"question_response_details": {
16-
"questionSubmissionSummary": [
17-
{
18-
"publishedPartId": "0e0432e4-90a2-47a1-b597-55f76596b7d5",
19-
"publishedPartPosition": 0,
20-
"publishedResponseAreaId": "ce28b12b-f583-4d83-a0e4-36b17127746a",
21-
"publishedResponseAreaPosition": 0,
22-
"responseAreaUniversalId": "f8857876-482d-411c-8ffc-734fb07712cf",
23-
"publishedResponseAreaPreResponseText": "$\\vec{a} \\ \\cdot \\ \\vec{b} \\ =$",
24-
"publishedResponseType": "NUMBER",
25-
"publishedResponseConfig": null,
26-
"totalSubmissions": 0,
27-
"totalWrongSubmissions": 0
28-
},
29-
{
30-
"publishedPartId": "a74a6fef-8c94-474c-b381-8d97a4b54725",
31-
"publishedPartPosition": 1,
32-
"publishedResponseAreaId": "f3be55ed-af9b-4483-ba45-636ccbad7a24",
33-
"publishedResponseAreaPosition": 0,
34-
"responseAreaUniversalId": "054bbcce-facb-4aaa-a1d2-7bec7627a6ef",
35-
"publishedResponseAreaPreResponseText": "$\\left(\\vec{a} - \\vec{b}\\right)\\cdot \\vec{c}\\ =$",
36-
"publishedResponseType": "NUMBER",
37-
"publishedResponseConfig": null,
38-
"totalSubmissions": 0,
39-
"totalWrongSubmissions": 0
40-
},
41-
{
42-
"publishedPartId": "66160c45-0554-470a-88ae-82f36e755ef6",
43-
"publishedPartPosition": 2,
44-
"publishedResponseAreaId": "79260525-1edb-4f19-bcdd-d827b5b692f3",
45-
"publishedResponseAreaPosition": 0,
46-
"responseAreaUniversalId": "bba9308c-55c7-4733-9315-37bc26fc8ec1",
47-
"publishedResponseAreaPreResponseText": "$\\left( \\ \\vec{a} \\cdot \\vec{c} \\ \\right) \\vec{b}\\ =$",
48-
"publishedResponseType": "MATRIX",
49-
"publishedResponseConfig": { "cols": 1, "rows": 3 },
50-
"totalSubmissions": 0,
51-
"totalWrongSubmissions": 0
52-
}
53-
],
54-
"questionInformation": {
55-
"questionTitle": "Dot Product",
56-
"questionGuidance": "",
57-
"questionContent": "$$\n\\vec{a}=\\begin{bmatrix}1 \\\\ 3\\\\ -2\\end{bmatrix} \\quad \\vec{b}=\\begin{bmatrix}0 \\\\ 3\\\\ 1\\end{bmatrix} \\quad \\vec{c}=\\begin{bmatrix} 1 \\\\\\ -1\\\\ -3\\end{bmatrix}\n$$",
58-
"durationLowerBound": 1,
59-
"durationUpperBound": 4,
60-
"parts": [
2+
"conversationId": "9f4d40af-5f65-4f90-ac8e-9b92fea92f9f",
3+
"messages": [
4+
{ "role": "USER", "content": "How can I start solving this question?" }
5+
],
6+
"user": {
7+
"userId": "a6632248-578b-4976-9d42-7daea459a905",
8+
"type": "LEARNER",
9+
"preference": { "conversationalStyle": "" },
10+
"taskProgress": {
11+
"currentQuestionId": "c5b258ec-0165-4595-a7ee-cbe0cd5dc012",
12+
"timeSpentOnQuestion": "58 minutes",
13+
"accessStatus": "too much time spent on this question today.",
14+
"markedDone": "This question is still being worked on.",
15+
"currentPart": {
16+
"partId": "bbfcc2e4-3bf7-4016-a300-f09e3204ed8e",
17+
"position": 0,
18+
"timeSpentOnPart": "37 minutes",
19+
"markedDone": "This part is not marked done.",
20+
"responseAreas": [
6121
{
62-
"publishedPartId": "0e0432e4-90a2-47a1-b597-55f76596b7d5",
63-
"publishedPartPosition": 0,
64-
"publishedPartContent": "",
65-
"publishedPartAnswerContent": "",
66-
"publishedWorkedSolutionSections": [
67-
{
68-
"id": "a7b1150a-05b7-4337-b902-5c6b1835cc74",
69-
"position": 0,
70-
"title": "",
71-
"content": "Assuming the given basis is orthonormal, the dot product between two vectors, $\\vec{a}$ and $\\vec{b}$, can be calculated simply by multiplying their corresponding components and summing them up:\n\n   \n\n$$\n\\begin{array}{rl}\\vec{a} \\cdot \\vec{b} &=\\begin{bmatrix}1 \\\\ 3\\\\ -2\\end{bmatrix}•\\begin{bmatrix}0 \\\\ 3\\\\ 1\\end{bmatrix} \\\\\\\\ &= (1 \\cdot 0) + (3 \\cdot 3) + (-2 \\cdot 1) \\\\\\\\ &= 0 + 9 + (-2) \\\\\\\\ &= 7\\end{array}\n$$\n\n   \n\nTherefore, the dot product between vectors $\\vec{a}$ and $\\vec{b}$ is \\$7\\$"
72-
}
73-
],
74-
"publishedResponseAreas": [
75-
{
76-
"id": "ce28b12b-f583-4d83-a0e4-36b17127746a",
77-
"position": 0,
78-
"universalResponseAreaId": "f8857876-482d-411c-8ffc-734fb07712cf",
79-
"preResponseText": "$\\vec{a} \\ \\cdot \\ \\vec{b} \\ =$",
80-
"Response": {
81-
"id": "4d0dfa85-fc34-4649-bb6e-0f194e8a5a04",
82-
"responseType": "NUMBER",
83-
"config": null,
84-
"answer": 7
85-
},
86-
"responseType": "NUMBER",
87-
"answer": 7
88-
}
89-
]
22+
"responseAreaId": "4ceb8d4b-b892-4388-a1b0-d80219e5b86e",
23+
"responseType": "NUMBER",
24+
"totalSubmissions": 1,
25+
"wrongSubmissions": 1,
26+
"latestSubmission": {
27+
"submission": 1,
28+
"feedback": "Incorrect",
29+
"answer": "26"
30+
}
9031
},
9132
{
92-
"publishedPartId": "a74a6fef-8c94-474c-b381-8d97a4b54725",
93-
"publishedPartPosition": 1,
94-
"publishedPartContent": "",
95-
"publishedPartAnswerContent": "",
96-
"publishedWorkedSolutionSections": [
97-
{
98-
"id": "0e91a91d-c536-4120-a70f-5cfea57b4235",
99-
"position": 0,
100-
"title": "",
101-
"content": "To calculate $\\left(\\vec{a} - \\vec{b}\\right) \\cdot \\vec{c}$ , we first need to find the vector resulting from the subtraction of $\\vec{b}$ from $\\vec{a}$:\n\n   \n\n$$\n\\begin{array}{rl}\\vec{a} - \\vec{b} &= \\begin{bmatrix}1 \\\\ 3 \\\\ -2\\end{bmatrix} - \\begin{bmatrix}0 \\\\ 3 \\\\ 1\\end{bmatrix} \\\\\\\\&= \\begin{bmatrix}1 - 0 \\\\ 3 - 3 \\\\ -2 - 1\\end{bmatrix} \\\\\\\\&= \\begin{bmatrix}1 \\\\ 0 \\\\ -3\\end{bmatrix}\\end{array}\n$$\n\n\n\nNext, we can compute the dot product of $\\left(\\vec{a} - \\vec{b}\\right)$ and $\\vec{c}$\n\n   \n\n$$\n\\begin{array}{rl}\\left(\\vec{a} - \\vec{b}\\right) \\cdot \\vec{c} &= \\begin{bmatrix}1 \\\\ 0 \\\\ -3\\end{bmatrix} \\cdot \\begin{bmatrix}1 \\\\ -1 \\\\ -3\\end{bmatrix} \\\\\\\\ &= (1 \\cdot 1) + (0 \\cdot -1) + (-3 \\cdot -3) \\\\\\\\&= 1 + 0 + 9 \\\\\\\\&= 10\\end{array}\n$$\n\n\n\nTherefore, $\\left(\\vec{a} - \\vec{b}\\right) \\cdot \\vec{c}$ is equal to \\$10"
102-
}
103-
],
104-
"publishedResponseAreas": [
105-
{
106-
"id": "f3be55ed-af9b-4483-ba45-636ccbad7a24",
107-
"position": 0,
108-
"universalResponseAreaId": "054bbcce-facb-4aaa-a1d2-7bec7627a6ef",
109-
"preResponseText": "$\\left(\\vec{a} - \\vec{b}\\right)\\cdot \\vec{c}\\ =$",
110-
"Response": {
111-
"id": "01edd054-b1fd-4049-8551-a8551d7a63ea",
112-
"responseType": "NUMBER",
113-
"config": null,
114-
"answer": 10
115-
},
116-
"responseType": "NUMBER",
117-
"answer": 10
118-
}
119-
]
120-
},
121-
{
122-
"publishedPartId": "66160c45-0554-470a-88ae-82f36e755ef6",
123-
"publishedPartPosition": 2,
124-
"publishedPartContent": "",
125-
"publishedPartAnswerContent": "",
126-
"publishedWorkedSolutionSections": [
127-
{
128-
"id": "c3225276-7bf8-4962-a459-d9583442c26e",
129-
"position": 0,
130-
"title": "",
131-
"content": "To calculate $\\left( \\ \\vec{a} \\cdot \\vec{c} \\ \\right) \\vec{b}$ , we first need to find the dot product of vectors $\\vec{a}$ and $\\vec{c}$\n\n   \n\n$$\n\\begin{array}{rl}\\vec{a} \\cdot \\vec{c} &= \\begin{bmatrix}1 \\\\ 3 \\\\ -2\\end{bmatrix} \\cdot \\begin{bmatrix}1 \\\\ -1 \\\\ -3\\end{bmatrix} \\\\\\\\ &= (1 \\cdot 1) + (3 \\cdot -1) + (-2 \\cdot -3) \\\\\\\\&= 1 - 3 + 6 \\\\\\\\&= 4\\end{array}\n$$\n\n   \n\nNext, we can scale $\\vec{b}$ by $\\vec{a} \\cdot \\vec{c}$\n\n   \n\n$$\n\\begin{array}{rl}\\left( \\ \\vec{a} \\cdot \\vec{c} \\ \\right) \\vec{b} &= 4\\begin{bmatrix}0 \\\\ 3 \\\\ 1\\end{bmatrix} \\\\\\\\ &= \\begin{bmatrix}4\\cdot0 \\\\ 4\\cdot3 \\\\ 4\\cdot1\\end{bmatrix} \\\\\\\\&= \\begin{bmatrix}0 \\\\ 12 \\\\ 4\\end{bmatrix} \\end{array}\n$$\n\n   \n"
132-
}
133-
],
134-
"publishedResponseAreas": [
135-
{
136-
"id": "79260525-1edb-4f19-bcdd-d827b5b692f3",
137-
"position": 0,
138-
"universalResponseAreaId": "bba9308c-55c7-4733-9315-37bc26fc8ec1",
139-
"preResponseText": "$\\left( \\ \\vec{a} \\cdot \\vec{c} \\ \\right) \\vec{b}\\ =$",
140-
"Response": {
141-
"id": "cc759374-175f-40be-8481-2c81e34795b6",
142-
"responseType": "MATRIX",
143-
"config": { "cols": 1, "rows": 3 },
144-
"answer": [["0"], ["12"], ["4"]]
145-
},
146-
"responseType": "MATRIX",
147-
"answer": [["0"], ["12"], ["4"]]
148-
}
149-
]
33+
"responseAreaId": "dbff8fab-b129-4a6c-a768-52f5c5923934",
34+
"responseType": "NUMBER",
35+
"totalSubmissions": 1,
36+
"wrongSubmissions": 1,
37+
"latestSubmission": {
38+
"submission": 20,
39+
"feedback": "Incorrect",
40+
"answer": "53"
41+
}
15042
}
15143
]
152-
},
153-
"questionAccessInformation": {
154-
"estimatedMinimumTime": "1 minute",
155-
"estimaredMaximumTime": "4 minutes",
156-
"timeTaken": "20 minutes",
157-
"accessStatus": "too much time spent on this question.",
158-
"markedDone": "",
159-
"currentPart": {
160-
"id": "0e0432e4-90a2-47a1-b597-55f76596b7d5",
161-
"position": 0
162-
}
16344
}
45+
}
46+
},
47+
"context": {
48+
"summary": "",
49+
"set": {
50+
"title": "Computing",
51+
"number": 8,
52+
"description": "Exercises on number systems and binary arithmetic."
16453
},
165-
"conversation_id": "7a65b6ed-85d1-4621-8efb-4fc8e9c5a8de",
166-
"agent_type": "base"
54+
"question": {
55+
"title": "Binary Numbers",
56+
"number": 0,
57+
"guidance": "",
58+
"content": "",
59+
"estimatedTime": "5-10 minutes",
60+
"parts": [
61+
{
62+
"partId": "bbfcc2e4-3bf7-4016-a300-f09e3204ed8e",
63+
"position": 0,
64+
"content": "Convert the following numbers to decimal: $11010_2$, $110101_2$.",
65+
"answerContent": "$11010_2 = 26_{10}$\\n\\n$110101_2 = 53_{10}$ \\n\\n  ",
66+
"workedSolutionSections": [
67+
{
68+
"id": "df588439-f183-4161-ade3-41559113e447",
69+
"position": 0,
70+
"title": "",
71+
"content": "$11010_2 = (0\\\\times1) + (1\\\\times2) + (0\\\\times4) + (1\\\\times8) + (1\\\\times16) = \\\\boxed{26_{10}}$ \\n\\n***\\n\\n$110101_2 = (1\\\\times1) + (0\\\\times2) + (1\\\\times4) + (0\\\\times8) + (1\\\\times16) + (1\\\\times32) = \\\\boxed{53_{10}}$ \\n\\n "
72+
}
73+
],
74+
"structuredTutorialSections": [],
75+
"responseAreas": [
76+
{
77+
"responseAreaId": "4ceb8d4b-b892-4388-a1b0-d80219e5b86e",
78+
"position": 0,
79+
"responseType": "NUMBER",
80+
"answer": 26,
81+
"preResponseText": "$11010_2 =$"
82+
},
83+
{
84+
"responseAreaId": "dbff8fab-b129-4a6c-a768-52f5c5923934",
85+
"position": 1,
86+
"responseType": "NUMBER",
87+
"answer": 53,
88+
"preResponseText": "$110101_2$"
89+
}
90+
]
91+
},
92+
{
93+
"partId": "cdf7de05-0bea-43e4-8c9a-5d47e835e6bd",
94+
"position": 1,
95+
"content": "Convert the following numbers to binary: $101_{10}$, $16_{10}$.",
96+
"answerContent": "$101_{10} = 1100101_2$ \\n\\n$16_{10} = 10000_2$ \\n\\n  ",
97+
"workedSolutionSections": [
98+
{
99+
"id": "aff42475-f0c0-4416-b1da-5cbae086d3f4",
100+
"position": 0,
101+
"title": "",
102+
"content": ""
103+
},
104+
{
105+
"id": "3bb12dbb-f558-4df3-9db7-a31733143d71",
106+
"position": 0,
107+
"title": "By repeated division",
108+
"content": "$101_{10}$ can be divided by 2 to give a quotient of 50 and a remainder of 1. The remainder is the first binary digit: $1_2$.\\n\\n***\\n\\n$50_{10}$ divided by 2 gives a quotient of 25 and a remainder of zero, so the second binary digit is 0: $01_2$.\\n\\n***\\n\\n$25_{10}$ divided by 2 gives a quotient of 12 and a remainder of 1, so the third binary digit is 1: $101_2$\\n\\n***\\n\\n$12_{10}$ divided by 2 gives a quotient of 6 and a remainder of 0, so the fourth binary digit is 0: $0101_2$\\n\\n***\\n\\n$6_{10}$ divided by 2 gives a quotient of 3 and a remainder of 0, so the fifth binary digit is 0: $00101_2$\\n\\n***\\n\\n$3_{10}$ divided by 2 gives a quotient of 1 and a remainder of 1, so the sixth binary digit is 1: $100101_2$\\n\\n***\\n\\n$1_{10}$ divided by 2 gives a quotient of zero and a remainder of 1, so the seventh binary digit is 1: $\\\\boxed{1100101_2}$. \\n\\n  "
109+
},
110+
{
111+
"id": "d95da3de-eafa-4cab-98d8-005b1268393b",
112+
"position": 1,
113+
"title": "By inspection",
114+
"content": "$101_{10}$ can be written in terms of powers of 2 as $101 = 64 + 32 + 4 + 1 = 2^6 + 2^5 + 2^2 + 2^0$, so its binary representation is $\\\\boxed{1100101_2}$.\\n\\n***\\n\\n$16_{10}$ is already a power of 2 ($2^4$), so its binary representation will have 1 bit set: $\\\\boxed{10000_2}$. \\n\\n   "
115+
}
116+
],
117+
"structuredTutorialSections": [],
118+
"responseAreas": [
119+
{
120+
"responseAreaId": "9a307730-7525-4b6e-976a-d8a434c6503a",
121+
"position": 0,
122+
"responseType": "NUMBER",
123+
"answer": 1100101,
124+
"preResponseText": "$101_{10} = $"
125+
},
126+
{
127+
"responseAreaId": "05df22cc-4afd-499c-8533-5f34dfec7844",
128+
"position": 1,
129+
"responseType": "NUMBER",
130+
"answer": 10000,
131+
"preResponseText": "$16_{10} = $"
132+
}
133+
]
134+
}
135+
]
136+
}
167137
}
168138
}

0 commit comments

Comments
 (0)