This project has been created as part of the 42 curriculum by lupalomi
Call me Maybe is an algorithm that uses an embedded AI (Qwen) to generate a JSON output based on a provided prompt, a function and its parameters. The AI can choose a function from the “--functions_definition” file and then select its parameters.
The Parser class takes the parameters "--functions_definition", "--input" and "--output" from the execution command and checks whether the values provided are valid. Examples of valid values are given in the Parser – Default Values section. If the values are invalid, an OSError exception will be raised, stopping the entire program.
The JSONLoader class reads data from a specified source and checks whether the content is valid or not. Valid JSON must be in one of these formats, depending on the specified flag:
- For "--input" flag:
[
{
"prompt": "What is the sum of 2 and 3?"
},
{
"prompt": "What is the sum of 265 and 345?"
},
{
"prompt": "Greet shrek"
},
{
"prompt": "Greet john"
},
{
"prompt": "Reverse the string 'hello'"
},
...
]- For "--functions_definition" flag:
[
{
"name": "fn_add_numbers",
"description": "Add two numbers together and return their sum.",
"parameters": {
"a": {
"type": "number"
},
"b": {
"type": "number"
}
},
"returns": {
"type": "number"
}
},
{
"name": "fn_greet",
"description": "Generate a greeting message for a person by name.",
"parameters": {
"name": {
"type": "string"
}
},
"returns": {
"type": "string"
}
},
{
"name": "fn_reverse_string",
"description": "Reverse a string and return the reversed result.",
"parameters": {
"s": {
"type": "string"
}
},
"returns": {
"type": "string"
}
},
...
]The main algorithm generates two JSONLoader classes, one for each flag. If the file doesn't exists or doesn't have the right permissions, a ValueError exception will be launch stopping the program.
The SolutionGenerator class takes both loaders, "--functions_definition" and "--input" flags, and uses the method gen_solution to generate a valid JSON Output. The output has the next format:
[
{
"prompt": "What is the sum of 2 and 3?",
"name": "fn_add_numbers",
"parameters": {
"a": 2.0,
"b": 3.0
}
},
{
"prompt": "What is the sum of 265 and 345?",
"name": "fn_add_numbers",
"parameters": {
"a": 265.0,
"b": 345.0
}
},
{
"prompt": "Greet shrek",
"name": "fn_greet",
"parameters": {
"name": "shrek"
}
},
...
]To generate a solution, it's necessary use the gen_solution method of this class.
Classes - Use of Pydantic
Pydantic is mainly used to validate the recieved data from each class. The @model_validator(mode="after") decorator is used to validate the routes, JSON data, permissions, ...
The Parser class implements argparse to collect the flag values provided. This method is much more easier than create my own argument parser.
class Parser(BaseModel):
functions_definition_file: str = Field(default="")
input_file: str = Field(default="")
output_file: str = Field(default="")
@model_validator(mode="after")
def validate_parser(self) -> "Parser":
...
@classmethod
def get_args(cls) -> "Parser":
...| Flag command | Default value |
|---|---|
| --functions_definition | data/input/functions_definition.json |
| --input | data/input/function_calling_tests.json |
| --output | data/output/function_calling_results.json |
This class implements two @staticmethod and one @classmethod alongside the method search_entry. The reasons behind this are:
-
@staticmethod: Use the methods validate_functions_definition and validate_input wihtout the need of create aJSONLoaderclass. -
@classmethod: The generate_loader method generates aJSONLoaderclass easily. -
search_entry: This method searchs an entry inside the json data that contains the provided route.
class JSONLoader(BaseModel):
json_route: str = ""
flag_type: str = ""
json_data: list[dict[str, Any]] = []
@model_validator(mode="after")
def validate_data(self) -> "JSONLoader":
...
def search_entry(self, mode: int, value: str) -> dict[str, Any]:
...
@classmethod
def generate_loader(cls, route: str, flag: str) -> "JSONLoader":
...
@staticmethod
def validate_functions_definition(data: list[dict[str, Any]]) -> bool:
...
@staticmethod
def validate_input(data: list[dict[str, Any]]) -> bool:
...To avoid resource problems, this class uses the same model for every request.
class SolutionGenerator(BaseModel):
functions_loader: JSONLoader
input_loader: JSONLoader
model: Any
output_file: str
def gen_solution(self) -> None:
...The usual performance of this project is between 90% and 100%. It can be improve by changing the AI model or by changing the given prompt in SolutionGenerator class.
The main challenge I have faced in this project is using the large language model (LLM) provided within the llm_sdk. For example: what should I code? When? How can I check if the model is working? etc.
However, this challenge has proved very rewarding, as I now have a clear understanding of how a LLM's works at the code level.
You can test the usage of this project by changing the provided files from /data/input directory to generate different outputs or exceptions.
In addition, you can use the make test method, which runs many tests to check that the three classes are working correctly. For more information, see the ‘Instructions’ section.
This project require, at least, 6 GB of memory, since you are installing a local AI inside the repository. Also a Python 3.10 version or greater will be required to run the program.
-
Install dependencies:
make install
-
Run the project:
make run
You can also run the project by executing:
uv run python -m src
This method is more practical since you can change easier the provided flags, like this:
uv run python -m src --functions_definition="data/input/functions_definition.json" --input="data/input/function_calling_tests.json" --output="data/output/function_calling_results.json"
-
Run in debug environment:
make debug
Again, you can also run this mode like:
uv run python -m pdb -m src
Where you can also change the flags:
uv run python -m pdb -m src --functions_definition="data/input/functions_definition.json" --input="data/input/function_calling_tests.json" --output="data/output/function_calling_results.json"
-
Run tests:
make test
-
Clean __pycache__ , .mypy_cache and .pytest_cache directories:
make clean
-
Run mypy and flake8 linters:
make lint
-
Run mypy and flake8 in strict mode:
make lint-strict