ML6-Gemini-Demo / src /tools /functions.py
GLorr's picture
Upload folder using huggingface_hub
6c09f76 verified
raw
history blame contribute delete
3.05 kB
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"},
},
},
}