-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
331 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ ignore = | |
|
||
[FORMAT] | ||
max-line-length=120 | ||
|
||
[MESSAGES CONTROL] | ||
disable=C0103 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Custom exceptions for the AutomatedGrader class.""" | ||
|
||
|
||
class InvalidResponseStructureError(Exception): | ||
"""A custom exception for the AutomatedGrader class.""" | ||
|
||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(self.message) | ||
|
||
|
||
class IncorrectResponseValueError(Exception): | ||
"""A custom exception for the AutomatedGrader class.""" | ||
|
||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(self.message) | ||
|
||
|
||
class IncorrectResponseTypeError(Exception): | ||
"""A custom exception for the AutomatedGrader class.""" | ||
|
||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(self.message) | ||
|
||
|
||
class ResponseFailedError(Exception): | ||
"""A custom exception for the AutomatedGrader class.""" | ||
|
||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(self.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Provide a class for grading a submission against an assignment.""" "" | ||
|
||
import json | ||
|
||
from .exceptions import ( | ||
IncorrectResponseTypeError, | ||
IncorrectResponseValueError, | ||
InvalidResponseStructureError, | ||
ResponseFailedError, | ||
) | ||
|
||
|
||
REQUIRED_KEYS_SPEC = "required-keys.json" | ||
HUMAN_PROMPT = {"content": "a prompt from a human", "additional_kwargs": {}, "type": "human", "example": False} | ||
AI_RESPONSE = {"content": "a response from the AI", "additional_kwargs": {}, "type": "ai", "example": False} | ||
|
||
|
||
# flake8: noqa: E701 | ||
class AutomatedGrader: | ||
"""Grade a submission against an assignment.""" | ||
|
||
def __init__(self, assignment): | ||
self.assignment = assignment | ||
with open(REQUIRED_KEYS_SPEC, "r", encoding="utf-8") as f: # pylint: disable=invalid-name | ||
self.required_keys = json.load(f) | ||
|
||
def validate_keys(self, subject, control): | ||
"""Validate that the subject has all the keys in the control dict.""" | ||
assignment_keys = set(subject.keys()) | ||
required_keys = set(control.keys()) | ||
|
||
if not required_keys.issubset(assignment_keys): | ||
raise InvalidResponseStructureError("The assignment is missing one or more required keys.") | ||
return True | ||
|
||
def validate_statuscode(self): | ||
"""Validate that the assignment's statusCode is 200.""" | ||
if not isinstance(self.assignment.get("statusCode"), int): | ||
raise IncorrectResponseTypeError("The assignment's statusCode must be an integer.") | ||
if not self.assignment["statusCode"] == 200: | ||
raise IncorrectResponseValueError("The assignment's statusCode must be 200.") | ||
return True | ||
|
||
def validate_base64encoded(self): | ||
"""Validate that the assignment's isBase64Encoded is False.""" | ||
if not isinstance(self.assignment.get("isBase64Encoded"), bool): | ||
raise IncorrectResponseTypeError("The assignment's base64Encoded must be a boolean.") | ||
if self.assignment["isBase64Encoded"]: | ||
raise IncorrectResponseValueError("The assignment's isBase64Encoded must be False.") | ||
|
||
def validate_body(self): | ||
"""Validate that the assignment's body is a dict with the correct keys.""" | ||
body = self.assignment.get("body") | ||
if not isinstance(body, dict): | ||
raise IncorrectResponseTypeError("The assignment's body must be a dict.") | ||
if not "chat_memory" in body: | ||
raise InvalidResponseStructureError(f"The assignment's body must have a key named chat_memory. {body}") | ||
if not "messages" in body["chat_memory"]: | ||
raise InvalidResponseStructureError("The assignment's body.chat_memory must has a key named messages.") | ||
messages = body["chat_memory"]["messages"] | ||
if not isinstance(messages, list): | ||
raise IncorrectResponseTypeError("The assignment's body.chat_memory.messages must be a list.") | ||
if len(messages) < 2: | ||
raise InvalidResponseStructureError("The messages list must contain at least two elements.") | ||
|
||
for message in messages: | ||
if not isinstance(message, dict): | ||
raise IncorrectResponseTypeError("All elements in the messages list must be dictionaries.") | ||
|
||
human_prompt = messages[0] | ||
ai_response = messages[1] | ||
|
||
self.validate_keys(human_prompt, HUMAN_PROMPT) | ||
self.validate_keys(ai_response, AI_RESPONSE) | ||
|
||
if not human_prompt["type"] == "human": | ||
raise IncorrectResponseValueError("The first message must be a human prompt.") | ||
if not ai_response["type"] == "ai": | ||
raise IncorrectResponseValueError("The second message must be an AI response.") | ||
|
||
def validate_metadata(self): | ||
"""Validate that the assignment's metadata is a dict with the correct keys.""" | ||
body = self.assignment.get("body") | ||
request_meta_data = body["request_meta_data"] | ||
if not isinstance(request_meta_data, dict): | ||
raise InvalidResponseStructureError("The assignment must has a dict named request_meta_data.") | ||
if not request_meta_data["lambda"] == "lambda_langchain": | ||
raise IncorrectResponseValueError("The request_meta_data.lambda must be lambda_langchain.") | ||
if not request_meta_data["model"] == "gpt-3.5-turbo": | ||
raise IncorrectResponseValueError("The request_meta_data.model must be gpt-3.5-turbo.") | ||
if not request_meta_data["end_point"] == "ChatCompletion": | ||
raise IncorrectResponseValueError("The request_meta_data.end_point must be ChatCompletion.") | ||
|
||
def validate(self): | ||
"""Validate the assignment data structure.""" | ||
if not isinstance(self.assignment, dict): | ||
raise InvalidResponseStructureError("The assignment must be a dictionary.") | ||
self.validate_keys(self.assignment, self.required_keys) | ||
self.validate_statuscode() | ||
self.validate_base64encoded() | ||
self.validate_body() | ||
self.validate_metadata() | ||
|
||
def grade_response(self, grade, message): | ||
"""Create a grade dict from the assignment.""" | ||
return { | ||
"grade": grade, | ||
"message": message, | ||
} | ||
|
||
def grade(self): | ||
"""Grade the assignment.""" | ||
self.validate() | ||
try: | ||
self.validate() | ||
except InvalidResponseStructureError as e: | ||
return self.grade_response(75, e.message) | ||
except ResponseFailedError as e: | ||
return self.grade_response(80, e.message) | ||
except IncorrectResponseValueError as e: | ||
return self.grade_response(85, e.message) | ||
except IncorrectResponseTypeError as e: | ||
return self.grade_response(90, e.message) | ||
return self.grade_response(100, "Great job!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"isBase64Encoded": false, | ||
"statusCode": 200, | ||
"body": { | ||
"chat_memory": { | ||
"messages": [] | ||
}, | ||
"output_key": null, | ||
"input_key": null, | ||
"return_messages": true, | ||
"human_prefix": "Human", | ||
"ai_prefix": "AI", | ||
"memory_key": "chat_history", | ||
"request_meta_data": { | ||
"lambda": "lambda_langchain", | ||
"model": "gpt-3.5-turbo", | ||
"end_point": "ChatCompletion", | ||
"temperature": 0.5, | ||
"max_tokens": 256 | ||
} | ||
} | ||
} |
Empty file.
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
grader/tests/events/lawrence-mcdaniel-homework1-correct.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"isBase64Encoded": false, | ||
"statusCode": 200, | ||
"body": { | ||
"chat_memory": { | ||
"messages": [ | ||
{ | ||
"content": "Marv, I'd like to introduce you to all the nice YouTube viewers.", | ||
"additional_kwargs": {}, | ||
"type": "human", | ||
"example": false | ||
}, | ||
{ | ||
"content": "Oh, how delightful. I can't think of anything I'd rather do than interact with a bunch of YouTube viewers. Just kidding, I'd rather be doing literally anything else. But go ahead, introduce me to your lovely audience. I'm sure they'll be absolutely thrilled to meet me.", | ||
"additional_kwargs": {}, | ||
"type": "ai", | ||
"example": false | ||
} | ||
] | ||
}, | ||
"output_key": null, | ||
"input_key": null, | ||
"return_messages": true, | ||
"human_prefix": "Human", | ||
"ai_prefix": "AI", | ||
"memory_key": "chat_history", | ||
"request_meta_data": { | ||
"lambda": "lambda_langchain", | ||
"model": "gpt-3.5-turbo", | ||
"end_point": "ChatCompletion", | ||
"temperature": 0.5, | ||
"max_tokens": 256 | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
grader/tests/events/lawrence-mcdaniel-homework1-incorrect.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"isBase64Encoded": false, | ||
"statusCode": 200, | ||
"body": { | ||
"chat_memory": { | ||
"messages": [ | ||
{ | ||
"content": "Marv, I'd like to introduce you to all the nice YouTube viewers.", | ||
"additional_kwargs": {}, | ||
"type": "human", | ||
"example": false | ||
}, | ||
{ | ||
"content": "Oh, how delightful. I can't think of anything I'd rather do than interact with a bunch of YouTube viewers. Just kidding, I'd rather be doing literally anything else. But go ahead, introduce me to your lovely audience. I'm sure they'll be absolutely thrilled to meet me.", | ||
"additional_kwargs": {}, | ||
"type": "ai", | ||
"example": false | ||
} | ||
] | ||
}, | ||
"output_key": null, | ||
"input_key": null, | ||
"return_messages": true, | ||
"human_prefix": "Human", | ||
"ai_prefix": "AI", | ||
"memory_key": "chat_history", | ||
"request_meta_data": { | ||
"lambda": "lambda_langchain", | ||
"model": "gpt-3.5-turbo", | ||
"end_point": "ChatCompletion", | ||
"temperature": 0.5, | ||
"max_tokens": 256 | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
grader/tests/events/lawrence-mcdaniel-homework1-invalid-json.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"isBase64Encoded": false, | ||
"statusCode": 200, | ||
"body": { | ||
"chat_memory": { | ||
"messages": [ | ||
{ | ||
"content": "Marv, I'd like to introduce you to all the nice YouTube viewers.", | ||
"additional_kwargs": {}, | ||
"type": "human", | ||
"example": false | ||
}, | ||
{ | ||
"content": "Oh, how delightful. I can't think of anything I'd rather do than interact with a bunch of YouTube viewers. Just kidding, I'd rather be doing literally anything else. But go ahead, introduce me to your lovely audience. I'm sure they'll be absolutely thrilled to meet me.", | ||
"additional_kwargs": {}, | ||
"type": "ai", | ||
"example": false | ||
} | ||
] | ||
}, | ||
"output_key": null, | ||
"input_key": null, | ||
"return_messages": true, | ||
"human_prefix": "Human", | ||
"ai_prefix": "AI", | ||
"memory_key": "chat_history", | ||
"request_meta_data": { | ||
"lambda": "lambda_langchain", | ||
"model": "gpt-3.5-turbo", | ||
"end_point": "ChatCompletion", | ||
"temperature": 0.5, | ||
"max_tokens": 256 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Provide common functions for testing.""" | ||
import json | ||
|
||
|
||
def get_event(filespec): | ||
"""Reads a JSON file and returns the event""" | ||
with open(filespec, "r", encoding="utf-8") as f: # pylint: disable=invalid-name | ||
event = json.load(f) | ||
return event |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
# flake8: noqa: F401 | ||
# pylint: disable=duplicate-code | ||
""" | ||
Test integrity of automated grader class methods. | ||
""" | ||
import pytest # pylint: disable=unused-import | ||
|
||
from ..grader import AutomatedGrader | ||
from .init import get_event | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class TestGrader: | ||
"""Test the OpenAI API via Langchain using the Lambda Layer, 'genai'.""" | ||
|
||
def test_basic_request(self): | ||
"""Test a valid successful submission.""" | ||
assignment = get_event("tests/events/lawrence-mcdaniel-homework1-correct.json") | ||
automated_grader = AutomatedGrader(assignment=assignment) | ||
grade = automated_grader.grade() | ||
|
||
assert isinstance(grade, dict), "The grade is not a dictionary" | ||
assert "grade" in grade, "The dictionary does not contain the key 'grade'" | ||
assert isinstance(grade["grade"], int), "The grade is not an int" | ||
assert grade["grade"] == 100, "The grade is not 100" | ||
|
||
assert "message" in grade, "The dictionary does not contain the key 'message'" | ||
assert isinstance(grade["message"], str), "The message is not a string" | ||
assert grade["message"] == "Great job!", "The message is not 'Great job!'" |