From b8b2bb2ea3cff450bc36dbb7318f022e8d37ec81 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Thu, 1 Feb 2024 14:14:55 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=91=EF=B8=8F=20Remove=20pydantic=5Fval?= =?UTF-8?q?idation.md=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/features/pydantic_validation.md | 123 --------------------------- 1 file changed, 123 deletions(-) delete mode 100644 docs/features/pydantic_validation.md diff --git a/docs/features/pydantic_validation.md b/docs/features/pydantic_validation.md deleted file mode 100644 index f1d2458..0000000 --- a/docs/features/pydantic_validation.md +++ /dev/null @@ -1,123 +0,0 @@ - -# Task Creation with Validated Fields - -!!! Example - [pydantic_validation.py](https://github.com/shroominic/funcchain/blob/main/examples/pydantic_validation.py) - - You can adapt this for your own usage. - This serves as an example of how to implement data validation and task creation using pydantic for data models and funcchain for processing natural language input. - - The main functionality is to parse a user description, validate the task details, and create a new Task object with unique keywords and a difficulty level within a specified range. - -## Full Code Example - -

-```python
-from funcchain import chain, settings
-from pydantic import BaseModel, field_validator
-
-# settings.llm = "ollama/openchat"
-settings.console_stream = True
-
-class Task(BaseModel):
-    name: str
-    difficulty: int
-    keywords: list[str]
-
-    @field_validator("keywords")
-    def keywords_must_be_unique(cls, v: list[str]) -> list[str]:
-        if len(v) != len(set(v)):
-            raise ValueError("keywords must be unique")
-        return v
-
-    @field_validator("difficulty")
-    def difficulty_must_be_between_1_and_10(cls, v: int) -> int:
-        if v < 10 or v > 100:
-            raise ValueError("difficulty must be between 10 and 100")
-        return v
-
-def gather_infos(user_description: str) -> Task:
-    """
-    Based on the user description,
-    create a new task to put on the todo list.
-    """
-    return chain()
-
-if __name__ == "__main__":
-    task = gather_infos("cleanup the kitchen")
-    print(f"{task=}")
-```
-
- -Demo - -
- ```python - User: - $ cleanup the kitchen - - task=Task - name='cleanup', - difficulty=30, - keywords=['kitchen', 'cleanup'] - ``` - -
- -## Instructions - -!!! Step-by-Step - **Necessary Imports** - - ```python - from funcchain import chain, settings - from pydantic import BaseModel, field_validator - ``` - - **Define the Task Model with Validators** - The `Task` class is a Pydantic model with fields: `name`, `difficulty`, and `keywords`. Validators ensure data integrity: - - - `keywords_must_be_unique`: Checks that all keywords are distinct. - - `difficulty_must_be_between_1_and_10`: Ensures difficulty is within 10 to 100. - - ```python - class Task(BaseModel): - name: str # Task name. - difficulty: int # Difficulty level (10-100). - keywords: list[str] # Unique keywords. - - @field_validator("keywords") - def keywords_must_be_unique(cls, v: list[str]) -> list[str]: - # Ensure keyword uniqueness. - if len(v) != len(set(v)): - raise ValueError("keywords must be unique") - return v - - @field_validator("difficulty") - def difficulty_must_be_between_1_and_10(cls, v: int) -> int: - # Validate difficulty range. - if v < 10 or v > 100: - raise ValueError("difficulty must be between 10 and 100") - return v - ``` - - **Implement the Information Gathering Function** - The gather_infos function is designed to take a user description and use the chain function to process and validate the input, returning a new Task object. - Adjust the string description to match your purposes when changing the code above. - - ```python - def gather_infos(user_description: str) -> Task: - """ - Based on the user description, - create a new task to put on the todo list. - """ - return chain() - ``` - - **Execute the Script** - Runs gather_infos with a sample and prints the Task. - ```python - if __name__ == "__main__": - task = gather_infos("cleanup the kitchen") - print(f"{task=}") - ```