Skip to content

Commit

Permalink
📚 Update documentation and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Jan 28, 2024
1 parent 32a08e2 commit 9676d59
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Version](https://badge.fury.io/py/funcchain.svg)](https://badge.fury.io/py/funcchain)
[![code-check](https://github.com/shroominic/funcchain/actions/workflows/code-check.yml/badge.svg)](https://github.com/shroominic/funcchain/actions/workflows/code-check.yml)
![Downloads](https://img.shields.io/pypi/dm/funcchain)
[![Discord](https://img.shields.io/discord/1192334452110659664?label=discord)](https://discord.gg/TrwWWMXdtR)
![License](https://img.shields.io/pypi/l/funcchain)
![PyVersion](https://img.shields.io/pypi/pyversions/funcchain)

Expand Down Expand Up @@ -39,28 +40,72 @@ export OPENAI_API_KEY=sk-**********
from funcchain import chain

def hello() -> str:
"""Say hello in 3 languages"""
"""
Say hello in 3 languages.
"""
return chain()

print(hello()) # -> Hello, Bonjour, Hola
print(hello()) # -> "Hallo, Bonjour, Hola"
```

This will call the OpenAI API and return the response.
Its using OpenAI since we did not specify a model and it will use the default model from the global settings of funcchain.

The `chain` function extracts the docstring as the prompt and the return type for parsing the response.
The underlying chat will look like this:

## Contributing
- User: "Say hello in 3 languages."
- AI: "Hallo, Bonjour, Hola"

To contribute, clone the repo and run:
The `chain()` function does all the magic in the background. It extracts the docstring, input arguments and return type of the function and compiles everything into a langchain prompt.

```bash
./dev_setup.sh
```
## Complex Example

This will install pre-commit hooks, dependencies and set up the environment.
Here a more complex example of what is possible. We create nested pydantic models and use union types to let the model choose the best shape to parse your given list into.

```python
from pydantic import BaseModel, Field
from funcchain import chain

To activate the virtual environment managed by poetry, you can use the following command:
# define nested models
class Item(BaseModel):
name: str = Field(description="Name of the item")
description: str = Field(description="Description of the item")
keywords: list[str] = Field(description="Keywords for the item")

class ShoppingList(BaseModel):
items: list[Item]
store: str = Field(description="The store to buy the items from")

class TodoList(BaseModel):
todos: list[Item]
urgency: int = Field(description="The urgency of all tasks (1-10)")

# support for union types
def extract_list(user_input: str) -> TodoList | ShoppingList:
"""
The user input is either a shopping List or a todo list.
"""
return chain()

```bash
poetry shell
# the model will choose the output type automatically
lst = extract_list(
input("Enter your list: ")
)

# custom handler based on type
match lst:
case ShoppingList(items=items, store=store):
print("Here is your Shopping List: ")
for item in items:
print(f"{item.name}: {item.description}")
print(f"You need to go to: {store}")

case TodoList(todos=todos, urgency=urgency):
print("Here is your Todo List: ")
for item in todos:
print(f"{item.name}: {item.description}")
print(f"Urgency: {urgency}")
```

The pydantic models force the language model to output only in the specified format. The actual ouput is a json string which is parsed into the pydantic model. This allows for a seamless integration of the language model into your app.
The union type selection works by listing every pydantic model as seperate function call to the model. So the LLM will select the best fitting pydantic model based on the prompt and inputs.

0 comments on commit 9676d59

Please sign in to comment.