Skip to content

Commit

Permalink
Merge pull request #597 from PrefectHQ/serialize-on-call
Browse files Browse the repository at this point in the history
monkey-patch (serialize_on_call flag)
  • Loading branch information
aaazzam authored Sep 22, 2023
2 parents c7fe0d0 + 38a3801 commit f37ad5b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
24 changes: 12 additions & 12 deletions docs/prompting/prompt_function.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ This is the easiest way to use Azure / OpenAI's function calling API.
def list_fruits(n: int, color: str = 'red') -> list[str]:
'''Generates a list of {{n}} {{color}} fruits'''

list_fruits(3, color = 'blue').serialize()
list_fruits(3, color = 'blue')

```
This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "Click to see results: ```list_fruits(3, color = 'blue').serialize()```"
??? success "Click to see results: ```list_fruits(3, color = 'blue')```"
```python
{
"messages": [
Expand Down Expand Up @@ -84,12 +84,12 @@ This is the easiest way to use Azure / OpenAI's function calling API.
def list_fruits(n: int, color: str = 'red') -> list[Fruit]:
'''Generates a list of {{n}} {{color}} fruits'''

list_fruits(3, color = 'blue').serialize()
list_fruits(3, color = 'blue')

```
This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "Click to see results: ```list_fruits(3, color = 'blue').serialize()```"
??? success "Click to see results: ```list_fruits(3, color = 'blue')```"
```python
{
"messages": [
Expand Down Expand Up @@ -168,12 +168,12 @@ This is the easiest way to use Azure / OpenAI's function calling API.
def list_fruits(n: int, color: str = 'red') -> list[Fruit]:
'''Generates a list of {{n}} {{color}} fruits'''
list_fruits(3, color = 'blue').serialize()
list_fruits(3, color = 'blue')

```
This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "Click to see results: ```list_fruits(3, color = 'blue').serialize()```"
??? success "Click to see results: ```list_fruits(3, color = 'blue')```"
```python
{
"messages": [
Expand Down Expand Up @@ -246,12 +246,12 @@ This is the easiest way to use Azure / OpenAI's function calling API.
def list_fruits(n: int, color: str = 'red') -> list[Fruit]:
'''Generates a list of {{n}} {{color}} {{'{{ response_model.__name__.lower() }}'}}'''
list_fruits(3, color = 'blue').serialize()
list_fruits(3, color = 'blue')

```
This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "Click to see results: ```list_fruits(3, color = 'blue').serialize()```"
??? success "Click to see results: ```list_fruits(3, color = 'blue')```"
```python
{
"messages": [
Expand Down Expand Up @@ -324,7 +324,7 @@ This is the easiest way to use Azure / OpenAI's function calling API.

This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "Click to see results: ```list_fruits(3, color = 'blue').serialize()```"
??? success "Click to see results: ```list_fruits(3, color = 'blue')```"
```python
{
"messages": [
Expand Down Expand Up @@ -385,7 +385,7 @@ This is the easiest way to use Azure / OpenAI's function calling API.

This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "Click to see results: ```list_fruits(3, color = 'blue').serialize()```"
??? success "Click to see results: ```list_fruits(3, color = 'blue')```"
```python
{
"messages": [
Expand Down Expand Up @@ -456,7 +456,7 @@ This is the easiest way to use Azure / OpenAI's function calling API.

This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "Click to see results: ```classify_fruits('tomato').serialize()```"
??? success "Click to see results: ```classify_fruits('tomato')```"
```python
{
"messages": [
Expand Down Expand Up @@ -525,7 +525,7 @@ This is the easiest way to use Azure / OpenAI's function calling API.

This function can now be run and serialized to an Azure / OpenAI Function Calling payload.

??? success "```extract_fruits('There are red apples and yellow bananas.').serialize()```"
??? success "```extract_fruits('There are red apples and yellow bananas.')```"
```python
{
"messages": [
Expand Down
3 changes: 2 additions & 1 deletion src/marvin/components/ai_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def ai_classifier_prompt(
response_model_name="Index",
response_model_description="The index of the most likely class.",
response_model_field_name="index",
serialize_on_call=False,
**kwargs,
)

Expand Down Expand Up @@ -151,7 +152,7 @@ def get_prompt(
ctx = ctx or cls.__metadata__.ctx or {}
instructions = instructions or cls.__metadata__.instructions
ctx["instructions"] = instructions or ctx.get("instructions", None)
return ai_classifier_prompt(cls, ctx=ctx, **kwargs) # type: ignore
return ai_classifier_prompt(cls, ctx=ctx, **kwargs) # type: ignore # noqa

@classmethod
def as_prompt(
Expand Down
1 change: 1 addition & 0 deletions src/marvin/components/ai_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def ai_fn_prompt(
@prompt_fn(
ctx={"ctx": ctx or {}, "func": func, "inspect": inspect},
response_model=return_annotation,
serialize_on_call=False,
**kwargs,
)
def prompt_wrapper(*args: P.args, **kwargs: P.kwargs) -> None: # type: ignore # noqa
Expand Down
1 change: 1 addition & 0 deletions src/marvin/components/ai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def ai_model_prompt(
response_model=cls,
response_model_name="FormatResponse",
response_model_description=description,
serialize_on_call=False,
)
def prompt_wrapper(text: str) -> None: # type: ignore # noqa
"""
Expand Down
3 changes: 3 additions & 0 deletions src/marvin/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def as_decorator(
response_model_name: Optional[str] = None,
response_model_description: Optional[str] = None,
response_model_field_name: Optional[str] = None,
serialize_on_call: bool = True,
) -> Union[
Callable[[Callable[P, None]], Callable[P, None]],
Callable[[Callable[P, None]], Callable[P, Self]],
Expand All @@ -289,6 +290,8 @@ def wrapper(func: Callable[P, Any], *args: P.args, **kwargs: P.kwargs) -> Self:
response_model_field_name=response_model_field_name,
)
response.__doc__ = func.__doc__
if serialize_on_call:
return response.serialize()
return response

if func is not None:
Expand Down

0 comments on commit f37ad5b

Please sign in to comment.