Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
SlapDrone committed Aug 12, 2023
1 parent 701b907 commit 684ad57
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ print(generate_backwards_car_names.description)
generate_backwards_car_names(person=Person(name='bob', age=53, cars=[Car(model='ford', speed=200.0), Car(model='renault', speed=210.0)])) -> [Car(model='drof', speed=200.0), Car(model='tluaner', speed=210.0)]


In the example above, the `generate_backwards_car_names` function is an AI function defined using Marvin's `ai_fn` decorator. The function's body is empty, as the actual implementation is provided by the AI. The `few_shot` decorator enriches the function's docstring with examples of its usage, formatted as JSON.
In the example above, the `generate_backwards_car_names` function is an AI function defined using Marvin's `ai_fn` decorator. The function's body is empty, as the actual implementation is provided by the AI. The `few_shot` decorator enriches the function's docstring with examples of its usage.

```python
clara = Person(name="clara", age=38, cars=[Car(model="delorean", speed=88.), Car(model="T", speed=20.)])
Expand Down Expand Up @@ -90,6 +90,39 @@ The main feature of Few_Shot is the `few_shot` decorator. This decorator takes a
- `join_str`: A string used to join multiple example strings in the docstring. Default is "\n".
- `default_format`: A string that is prepended to the examples in the docstring. Default is "\nExamples:\n".

## Customisation

You can tweak some parameters to change the way the examples are displayed from the default by placing the `{examples}` key yourself and playing with the decorator arguments:

```python
@marvin.ai_fn
@few_shot(
examples=[
((Person(name="alice", age=22, cars=[Car(model="mini", speed = 180)]),), [Car(model='inim', speed=180.0)]),
((Person(name="bob", age=53, cars=[Car(model="ford", speed=200), Car(model="renault", speed=210)]),), [Car(model='drof', speed=200.0), Car(model='tluaner', speed=210.0)]),
],
example_formatter=JsonFormatter(template="Inputs:\n{inputs}\nOutputs:\n{outputs}"),
join_str = "\n\n",
)
def generate_backwards_car_names(person: Person) -> list[Car]:
"""
Given a person, return a list of their cars with the model names backwards.
Here are some examples:
{examples}
"""
```

If you want to, you can implement your own `Formatter`. The only requirement is that it
follow the `FormatterProtocol`, by exposing one method with signature:

```python
class YourFormatter:
...
def format(self, example: Example, sig: inspect.Signature, func_name: str) -> str:
...
```

## Testing

Expand Down
8 changes: 4 additions & 4 deletions few_shot/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def format(self, example: Example, sig: inspect.Signature, func_name: str) -> st
...


class JsonFormatter:
template = "{inputs} -> {outputs}"
class JsonFormatter(BaseModel):
template: str = "{inputs} -> {outputs}"

def format(self, example: Example, sig: inspect.Signature, func_name: str) -> str:
try:
Expand Down Expand Up @@ -48,8 +48,8 @@ def _serialize_value(self, value: Any) -> Any:
return value


class CleanFormatter:
template = "{name}({inputs}) -> {outputs}"
class CleanFormatter(BaseModel):
template: str = "{name}({inputs}) -> {outputs}"

def format(self, example: Example, sig: inspect.Signature, func_name: str) -> str:
# Bind the arguments and keyword arguments to the signature's parameters
Expand Down

0 comments on commit 684ad57

Please sign in to comment.