Spaces:
Runtime error
Runtime error
File size: 3,052 Bytes
6c09f76 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import logging
import helpers.datastore as datastore
logger = logging.getLogger(__name__)
def validate_answer(
question_id: int, answer: str, answer_type: str | int | list
) -> str:
"""Validate the user's answer against an expected answer type.
question_id (int): The identifier of the question being validated
answer (str): The user's provided answer to validate
answer_type (type): The expected python type that the answer should match (e.g. str, int, list)
str: Returns "Answer is valid" if answer matches expected type, raises ValueError otherwise
Raises:
ValueError: If the answer's type does not match the expected answer_type
Example:
>>> validate_answer(1, "42", str)
True
>>> validate_answer(1, 42, str)
ValueError: Invalid answer type
"""
logging.info(
{
"question_id": question_id,
"answer": answer,
"answer_type": answer_type,
}
)
if type(answer) is answer_type:
raise ValueError("Invalid answer type")
datastore.DATA_STORE["answers"].append(
{"question_id": question_id, "answer": answer}
)
return "Answer is valid"
validate_answer_tool = {
"name": "validate_answer",
"description": "Validate the user's answer against an expected answer type",
"parameters": {
"type": "OBJECT",
"properties": {
"question_id": {
"type": "INTEGER",
"description": "The identifier of the question being validated",
},
"answer": {
"type": "STRING",
"description": "The user's provided answer to validate",
},
"answer_type": {
"type": "STRING",
"description": "The expected python type that the answer should match (e.g. str, int, list)",
},
},
"required": ["question_id", "answer", "answer_type"],
},
}
def store_input(role: str, input: str) -> str:
"""Store conversation input in a JSON file.
Args:
role (str): The role of the speaker (user or assistant)
input (str): The text input to store
Returns:
str: Confirmation message
"""
print(datastore.DATA_STORE)
conversation = datastore.DATA_STORE.get("conversation")
if conversation is None:
datastore.DATA_STORE["conversation"] = [{"role": role, "input": input}]
else:
datastore.DATA_STORE["conversation"].append({"role": role, "input": input})
return "Input stored successfully"
store_input_tool = {
"name": "store_input",
"description": "Store user input in conversation history",
"parameters": {
"type": "OBJECT",
"properties": {
"role": {
"type": "STRING",
"description": "The role of the speaker (user or assistant)",
},
"input": {"type": "STRING", "description": "The text input to store"},
},
},
}
|