-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fbbb49
commit 709fc08
Showing
15 changed files
with
674 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
The main functionality is to take a string of text and attempt to extract user information, such as name and email, and return a User object. If the information is insufficient, an Error is returned instead. | ||
|
||
##Full Code Example | ||
|
||
<pre><code id="codeblock"> | ||
```python | ||
from funcchain import BaseModel, Error, chain | ||
from rich import print | ||
|
@@ -32,6 +32,7 @@ if __name__ == "__main__": | |
print(extract_user_info("I'm John and my mail is [email protected]")) # returns a User object | ||
|
||
``` | ||
</code></pre> | ||
|
||
Demo | ||
<div class="termy"> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#Different LLMs with funcchain EASY TO USE | ||
|
||
!!! Example | ||
See [ollama.py](https://github.com/shroominic/funcchain/blob/main/examples/ollama.py) | ||
Also see supported [MODELS.md](https://github.com/shroominic/funcchain/blob/main/MODELS.md) | ||
|
||
In this example, we will use the funcchain library to perform sentiment analysis on a piece of text. This showcases how funcchain can seamlessly utilize different Language Models (LLMs), such as ollama, without many unnececary code changes.. | ||
|
||
This is particularly useful for developers looking to integrate different models in a single application or just experimenting with different models. | ||
|
||
##Full Code Example | ||
<pre><code id="codeblock"> | ||
```python | ||
from funcchain import chain, settings | ||
from pydantic import BaseModel, Field | ||
from rich import print | ||
|
||
# define your model | ||
class SentimentAnalysis(BaseModel): | ||
analysis: str = Field(description="A description of the analysis") | ||
sentiment: bool = Field(description="True for Happy, False for Sad") | ||
|
||
# define your prompt | ||
def analyze(text: str) -> SentimentAnalysis: | ||
""" | ||
Determines the sentiment of the text. | ||
""" | ||
return chain() | ||
|
||
if __name__ == "__main__": | ||
# set global llm | ||
settings.llm = "ollama/wizardcoder:34b-python-q3_K_M" | ||
# log tokens as stream to console | ||
settings.console_stream = True | ||
|
||
# run prompt | ||
poem = analyze("I really like when my dog does a trick!") | ||
|
||
# show final parsed output | ||
print(poem) | ||
``` | ||
</code></pre> | ||
|
||
#Demo | ||
<div class="termy"> | ||
``` | ||
poem = analyze("I really like when my dog does a trick!") | ||
|
||
$ .................. | ||
|
||
Add demo | ||
|
||
``` | ||
</div> | ||
|
||
##Instructions | ||
!!! Step-by-Step | ||
|
||
**Necessary Imports** | ||
```python | ||
from funcchain import chain, settings | ||
from pydantic import BaseModel, Field | ||
from rich import print | ||
``` | ||
|
||
**Define the Data Model** | ||
Here, we define a `SentimentAnalysis` model with a description of the sentiment analysis and a boolean field indicating the sentiment. | ||
```python | ||
class SentimentAnalysis(BaseModel): | ||
analysis: str = Field(description="A description of the analysis") | ||
sentiment: bool = Field(description="True for Happy, False for Sad") | ||
``` | ||
|
||
**Create the Analysis Function** | ||
This 'analyze' function takes a string as input and is expected to return a `SentimentAnalysis` object by calling the `chain()` function from the `funcchain` library. | ||
```python | ||
def analyze(text: str) -> SentimentAnalysis: | ||
""" | ||
Determines the sentiment of the text. | ||
""" | ||
return chain() | ||
``` | ||
|
||
**Execution Configuration** | ||
In the main block, configure the global settings to set the preferred LLM, enable console streaming, and run the `analyze` function with sample text. The result is printed using the `rich` library. | ||
```python | ||
if __name__ == "__main__": | ||
settings.llm = "ollama/wizardcoder:34b-python-q3_K_M" | ||
settings.console_stream = True | ||
poem = analyze("I really like when my dog does a trick!") | ||
print(poem) | ||
``` | ||
|
||
!!!Important | ||
We need to note here is that `settings.llm` can be adjusted to any model mentioned in [MODELS.md](https://github.com/shroominic/funcchain/blob/main/MODELS.md) and your funcchain code will still work and `chain()` does everything in the background for you. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#JSON structured Output using Funcchain with OenAI | ||
|
||
!!! Example | ||
See [openai_json_mode.py](https://github.com/shroominic/funcchain/blob/main/examples/openai_json_mode.py) | ||
|
||
This example will showcase how funcchain enables OpenAI to output even the type `int` as JSON. | ||
|
||
This example demonstrates using the funcchain library and pydantic to create a FruitSalad model, sum its contents, and output the total in a Result model as an integer. | ||
|
||
##Full Code Example | ||
<pre><code id="codeblock"> | ||
```python | ||
from funcchain import chain, settings | ||
from pydantic import BaseModel | ||
|
||
settings.console_stream = True | ||
|
||
class FruitSalad(BaseModel): | ||
bananas: int = 0 | ||
apples: int = 0 | ||
|
||
class Result(BaseModel): | ||
sum: int | ||
|
||
def sum_fruits(fruit_salad: FruitSalad) -> Result: | ||
""" | ||
Sum the number of fruits in a fruit salad. | ||
""" | ||
return chain() | ||
|
||
if __name__ == "__main__": | ||
fruit_salad = FruitSalad(bananas=3, apples=5) | ||
assert sum_fruits(fruit_salad) == 8 | ||
``` | ||
</code></pre> | ||
|
||
Demo | ||
|
||
<div class="termy"> | ||
```python | ||
fruit_salad = FruitSalad(bananas=3, apples=5) | ||
assert sum_fruits(fruit_salad) == 8 | ||
``` | ||
</div> | ||
|
||
Instructions | ||
!!! Step-by-Step | ||
|
||
**Necessary Imports** | ||
`funcchain` for chaining functionality, and `pydantic` for the data models. | ||
```python | ||
from funcchain import chain, settings | ||
from pydantic import BaseModel | ||
``` | ||
|
||
**Defining the Data Models** | ||
We define two Pydantic models: `FruitSalad` with integer fields for the number of bananas and apples, and `Result`, which will hold the sum of the fruits. | ||
Of course feel free to change those classes according to your needs but use of `pydantic` is required. | ||
```python | ||
class FruitSalad(BaseModel): | ||
bananas: int = 0 | ||
apples: int = 0 | ||
|
||
class Result(BaseModel): | ||
sum: int | ||
``` | ||
|
||
|
||
|
||
**Summing Function** | ||
The `sum_fruits` function is intended to take a `FruitSalad` object and use `chain()` for solving this task with an LLM. The result is returned as a `Result` object. | ||
```python | ||
def sum_fruits(fruit_salad: FruitSalad) -> Result: | ||
""" | ||
Sum the number of fruits in a fruit salad. | ||
""" | ||
return chain() | ||
``` | ||
|
||
|
||
**Execution Block** | ||
```python | ||
if __name__ == "__main__": | ||
fruit_salad = FruitSalad(bananas=3, apples=5) | ||
assert sum_fruits(fruit_salad) == 8 | ||
``` | ||
|
||
In the primary execution section of the script, we instantiate a `FruitSalad` object with predefined quantities of bananas and apples. We then verify that the `sum_fruits` function accurately calculates the total count of fruits, which should be 8 in this case. |
Oops, something went wrong.