From 34072cc43728ce1464322094492d74e6c294c0e7 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Sun, 28 Jan 2024 19:06:02 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=91=EF=B8=8F=20Remove=20literals.md=20?= =?UTF-8?q?file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/examples/literals.md | 88 --------------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 docs/examples/literals.md diff --git a/docs/examples/literals.md b/docs/examples/literals.md deleted file mode 100644 index 4cf668f..0000000 --- a/docs/examples/literals.md +++ /dev/null @@ -1,88 +0,0 @@ -#Literal Type Enforcement in Funcchain - -!!! Example - literals.py [Example](https://github.com/shroominic/funcchain/blob/main/examples/literals.py) - - This is a useful task for scenarios where you want to ensure that certain outputs strictly conform to a predefined set of values. - This serves as an example of how to implement strict type checks on outputs using the Literal type from the typing module and the funcchain library. - - You can adapt this for your own usage. - -##Full Code Example - -```python -from typing import Literal -from funcchain import chain -from pydantic import BaseModel - -class Ranking(BaseModel): - chain_of_thought: str - score: Literal[11, 22, 33, 44, 55] - error: Literal["no_input", "all_good", "invalid"] - -def rank_output(output: str) -> Ranking: - """ - Analyze and rank the output. - """ - return chain() - -if __name__ == "__main__": - rank = rank_output("The quick brown fox jumps over the lazy dog.") - print(rank) -``` - -Demo -
-```python -$ rank = rank_output("The quick brown fox jumps over the lazy dog.") -$ ........ -Ranking(chain_of_thought='...', score=33, error='all_good') -``` -
- -##Instructions - -!!! Step-by-Step - - **Necessary Imports** - ```python - from typing import Literal - from funcchain import chain - from pydantic import BaseModel - ``` - - - **Define the Ranking Model** - The Ranking class is a Pydantic model that uses the Literal type to ensure that the score and error fields can only contain certain predefined values. - So experiment with changing those but keeping this structure of the class. - The LLM will be forced to deliver one of the defined output. - - ```python - class Ranking(BaseModel): - chain_of_thought: str - score: Literal[11, 22, 33, 44, 55] - error: Literal["no_input", "all_good", "invalid"] - ``` - - **Implement the Ranking Function** - Use `chain()` to process a user input, which must be a string. - Adjust the content based on your above defined class. - - ```python - def rank_output(output: str) -> Ranking: - """ - Analyze and rank the output. - """ - return chain() - ``` - - **Execute the Ranking System** - This block is used to execute the ranking function and print the results when the script is run directly. - ```python - if __name__ == "__main__": - rank = rank_output("The quick brown fox jumps over the lazy dog.") - print(rank) - ``` - - -