Skip to content

Commit

Permalink
Refactor code to add new API module for utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
chandralegend committed Sep 23, 2024
1 parent 0c8bbc1 commit 95008aa
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions docs/api/semantix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions docs/api/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ 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]`.

### 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

Expand All @@ -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]`.
Expand All @@ -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

Expand Down
63 changes: 63 additions & 0 deletions docs/api/utils.md
Original file line number Diff line number Diff line change
@@ -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.")
```
2 changes: 2 additions & 0 deletions docs/building-blocks/large-language-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions semantix/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
57 changes: 47 additions & 10 deletions try.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Initialize the LLM"
]
},
{
"cell_type": "code",
"execution_count": 1,
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: ..."
]
},
Expand Down

0 comments on commit 95008aa

Please sign in to comment.