diff --git a/docs/_sidebar.md b/docs/_sidebar.md index c19e2ae..bd81429 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -16,6 +16,7 @@ - [semantix](api/semantix.md) - [llms](api/llms.md) - [types](api/types.md) + - [utils](api/utils.md) - Community - [Projects](community/projects.md) - [Contributors](community/contributors.md) diff --git a/docs/api/semantix.md b/docs/api/semantix.md index cdf9c0c..df292c8 100644 --- a/docs/api/semantix.md +++ b/docs/api/semantix.md @@ -40,7 +40,7 @@ class Person: ## enhance ```python -@enhance(meaning: str, llm: BaseLLM, method: str = "Normal", tools: List[Callable] = [], retries=2, **kwargs) +@enhance(meaning: str, llm: BaseLLM, method: str = "Normal", tools: List[Callable] = [], retries=2, return_additional_info=False **kwargs) ``` A decorator to enhance the function with LLM capabilities. @@ -53,11 +53,13 @@ A decorator to enhance the function with LLM capabilities. - The Large Language Model to use. - `method` : str, optional - The method to use for the enhancement. Default is `"Normal"`. - - Options are `"Normal"`, `"Reason"`, `"Chain-of-Thoughts"`, `"ReAct"`, `"Reflection"`. + - Options are `"Normal"`, `"Reason"`, `"CoT"`, `"ReAct"`, `"Reflection"`. - `tools` : List[Callable | Tool], optional - List of tools/functions to be used by the LLM. Default is `[]`. - `retries` : int, optional - The number of retries to use. Default is `2`. +- `return_additional_info` : bool, optional + - Whether to return additional information in the form of `Output` Object. Default is `False`. - `**kwargs` - Additional keyword arguments to pass to the LLM. - For example, `temperature`, `max_tokens`, etc. The list of arguments depends on the LLM. diff --git a/docs/api/types.md b/docs/api/types.md index fe8653d..3637859 100644 --- a/docs/api/types.md +++ b/docs/api/types.md @@ -4,6 +4,12 @@ The `semantix.types` module provides a set of classes and functions to work with ## Image +```python +class Image: + file_path: str + quality: str = "low" +``` + The `Image` class is used to represent an image in the library. `Image` class converts the image into a base64 encoded string to be used in the Large Language Model. !> `pillow` library is required to use the `Image` class. If you don't have it installed, you can install it using the `pip install pillow` or `pip install semantix[image]`. @@ -11,6 +17,7 @@ The `Image` class is used to represent an image in the library. `Image` class co ### Parameters - `file_path` (str): The path to the image file. +- `quality` (str): The quality of the image. Default is `"low"`. Options are `"low"`, `"medium"`, `"high"`. ### Example @@ -25,6 +32,13 @@ def get_person(img: Semantic[Image, "Image of the Person"]) -> Person: ## Video +```python +class Video: + file_path: str + seconds_per_frame: int = 2 + quality: str = "low" +``` + The `Video` class is used to represent a video in the library. `Video` class converts the video into a base64 encoded list of frames to be used in the Large Language Model. !> `opencv-python` library is required to use the `Video` class. If you don't have it installed, you can install it using the `pip install opencv-python-headless` or `pip install semantix[video]`. @@ -33,6 +47,7 @@ The `Video` class is used to represent a video in the library. `Video` class con - `file_path` (str): The path to the video file. - `seconds_per_frame` (int): The number of seconds per frame to extract from the video. Default is 2. +- `quality` (str): The quality of the video. Default is `"low"`. Options are `"low"`, `"medium"`, `"high"`. ### Example diff --git a/docs/api/utils.md b/docs/api/utils.md new file mode 100644 index 0000000..1e3e38c --- /dev/null +++ b/docs/api/utils.md @@ -0,0 +1,63 @@ +# Utilities API Reference + +The `semantix.utils` module provides a set of utility functions. + +## create_class + +```python +create_class( + classname: str, fields: Dict[str, Tuple[Type, Any]], desc: str = "" +) -> Any +``` + +Helper function to create a new dataclass with the given fields. This is helpful when creating classes with a large number of parameters. + +### Parameters + +- `classname` (str): The name of the class to be created. +- `fields` (Dict[str, Tuple[Type, Any]): A dictionary containing the fields of the class, where the keys are the field names and the values are tuples containing the field type and default value. +- `desc` (str, optional): Description of the class. Defaults to "". + +## Example + +```python +from semantix.utils import create_class + +fields = { + "name": (str, ""), + "age": (int, 0), + "height": (float, 0.0), +} + +Person = create_class("Person", fields, "A class to represent a person.") +``` + +## create_enum + +```python +create_enum( + classname: str, fields: Dict[str, Any], desc: str = "" +) -> Enum +``` + +Helper function to create a new Enum with the given fields. This is helpful when creating enums with a large number of values. + +### Parameters + +- `classname` (str): The name of the Enum class. +- `fields` (Dict[str, Any]): A dictionary containing the field names and values for the Enum. +- `desc` (str, optional): A description for the Enum. Defaults to "". + +## Example + +```python +from semantix.utils import create_enum + +colors = { + "RED": 1, + "GREEN": 2, + "BLUE": 3, +} + +Color = create_enum("Color", colors, "An Enum to represent colors.") +``` diff --git a/docs/building-blocks/large-language-model.md b/docs/building-blocks/large-language-model.md index 254ee5f..00533cd 100644 --- a/docs/building-blocks/large-language-model.md +++ b/docs/building-blocks/large-language-model.md @@ -80,6 +80,8 @@ To check the supported parameters for the LLMs, you can refer to the documentati ## How to create a custom LLM? +!> If your model follows a different message template, its little tricky. Check one of the in-built LLMs to understand the implementation. + Each and every in-built LLM classes in Semantix are subclasses of the `BaseLLM` class. You can create your own custom LLM by subclassing the `BaseLLM` class and implementing the necessary methods. For example, to create a custom LLM that always returns a name of the model, you can do the following: diff --git a/semantix/decorators.py b/semantix/decorators.py index e250cad..3309df4 100644 --- a/semantix/decorators.py +++ b/semantix/decorators.py @@ -34,7 +34,7 @@ def enhance( method (str, optional): The enhancement method to be applied. Defaults to "Normal". Options are: "Normal", "Reason", "CoT", "ReAct", "Reflection". tools (List[Union[Callable, Tool]], optional): A list of functions or Tool objects that the LLM can use. Defaults to []. retries (int, optional): The number of retry attempts for LLM operations. Defaults to 2. - return_output_obj (bool, optional): Whether to return the output and additional information. Defaults to False. + return_additional_info (bool, optional): Whether to return the output and additional information. Defaults to False. **kwargs (dict): Additional keyword arguments to be passed to the LLM. Returns: @@ -49,7 +49,7 @@ def enhance( @enhance( meaning="Summarize text", model=my_llm_instance, - method="Chain-of-Thoughts", + method="CoT", temperature=0.7 ) def summarize_text(text: str) -> str: diff --git a/try.ipynb b/try.ipynb index 4041e56..e8172bf 100644 --- a/try.ipynb +++ b/try.ipynb @@ -18,6 +18,13 @@ "" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Initialize the LLM" + ] + }, { "cell_type": "code", "execution_count": 1, @@ -45,6 +52,26 @@ "# os.environ['ANTHROPIC_API_KEY'] = getpass('Enter your Anthropic API key: ') # Uncomment this line if you want to use the Anthropic API" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantix.llms import OpenAI\n", + "# from semantix.llms import Anthropic # Uncomment this line if you want to use the Anthropic API\n", + "\n", + "llm = OpenAI()\n", + "# llm = Anthropic() # Uncomment this line if you want to use the Anthropic API" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Define the Structured Classes, Enums and Functions" + ] + }, { "cell_type": "code", "execution_count": 4, @@ -58,11 +85,6 @@ "from typing import List\n", "\n", "from semantix import Semantic, enhance\n", - "from semantix.llms import OpenAI\n", - "# from semantix.llms import Anthropic # Uncomment this line if you want to use the Anthropic API\n", - "\n", - "llm = OpenAI()\n", - "# llm = Anthropic() # Uncomment this line if you want to use the Anthropic API\n", "\n", "\n", "class Personality(Enum):\n", @@ -84,10 +106,11 @@ "\n", "@dataclass\n", "class Person:\n", + " \"\"\"Person Information\"\"\"\n", " full_name: str\n", - " yod: Semantic[int, \"Year of Death\"]\n", - " personality: Semantic[Personality, \"Personality of the Person\"]\n", - " life_works: Semantic[List[LifeWork], \"Life's Works of the Person\"]\n", + " yod: Semantic[int, \"Year of Death\"] # type: ignore\n", + " personality: Semantic[Personality, \"Personality of the Person\"] # type: ignore\n", + " life_works: Semantic[List[LifeWork], \"Life's Works of the Person\"] # type: ignore\n", "\n", " def __repr__(self) -> str:\n", " repr_str = (\n", @@ -101,11 +124,18 @@ "\n", "@enhance(\"Get Person Information use common knowledge\", llm)\n", "def get_person_info(\n", - " name: Semantic[str, \"Name of the Person\"],\n", + " name: Semantic[str, \"Name of the Person\"], # type: ignore\n", ") -> Person:\n", " ..." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run the functions as you would normally" + ] + }, { "cell_type": "code", "execution_count": 5, @@ -200,6 +230,13 @@ "get_person_info(name=\"Martin Luther King Jr.\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Multimodal Support" + ] + }, { "cell_type": "code", "execution_count": 7, @@ -277,7 +314,7 @@ "\n", "@enhance(\"Get Person Information use common knowledge\", llm, method=\"Reason\")\n", "def get_person_info(\n", - " img: Semantic[Image, \"Image of a Person\"],\n", + " img: Semantic[Image, \"Image of a Person\"], # type: ignore\n", ") -> Person: ..." ] },