Skip to content

Commit

Permalink
Merge pull request #573 from PrefectHQ/docs-+-ai_fn-QOL
Browse files Browse the repository at this point in the history
docs fix + QOL parsing stuff (rm parse raw for json.loads)
  • Loading branch information
aaazzam authored Sep 12, 2023
2 parents 14acab5 + f4e3de6 commit 620c637
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
19 changes: 8 additions & 11 deletions docs/examples/classification_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,14 @@ In this example, we'll show how to

```python
from marvin import ai_fn, settings
from enum import Enum

class Category(Enum):
SPORTS = "sports"
POLITICS = "politics"
TECHNOLOGY = "technology"
# Add more categories as needed
from typing import Literal

settings.openai.api_key = 'API_KEY'

@ai_fn
def classify_text(text: str) -> Category:
def classify_text(text: str) -> Literal['sports', 'politics', 'technology']:
'''
Classifies the passed `text` into one of the predefined categories.
Correctly classifies the passed `text` into one of the predefined categories.
'''

```
Expand All @@ -61,16 +55,19 @@ In the following example, we will demonstrate how to deploy the AI function we j
```python
from fastapi import FastAPI
from marvin import ai_fn, settings
from typing import Literal

settings.openai.api_key = 'API_KEY'

app = FastAPI()

settings.openai.api_key = 'API_KEY'

@app.post("/classify_text/")
@ai_fn
def classify_text(text: str) -> Category:
def classify_text(text: str) -> Literal['sports', 'politics', 'technology']:
'''
Classifies the passed `text` into one of the predefined categories.
Correctly classifies the passed `text` into one of the predefined categories.
'''
```

Expand Down
9 changes: 5 additions & 4 deletions docs/examples/extraction_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ In this example, we'll show how to

```python
from marvin import ai_fn, settings
from typing import Any

settings.openai.api_key = 'API_KEY'

@ai_fn
def extract_person(text: str) -> dict[str, Any]:
def extract_person(text: str) -> dict:
'''
Extracts a persons `birth_year`, `first_name` and `last_name`
Correctly infers a persons `birth_year`, `first_name` and `last_name`
from the passed `text`.
'''

Expand Down Expand Up @@ -63,9 +64,9 @@ In the following example, we will demonstrate how to deploy the AI function we j

@app.post("/extract_person/")
@ai_fn
def extract_person(text: str) -> dict[str, Any]:
def extract_person(text: str) -> dict:
'''
Extracts a persons `birth_year`, `first_name` and `last_name`
Correctly infers a persons `birth_year`, `first_name` and `last_name`
from the passed `text`.
'''
Expand Down
6 changes: 4 additions & 2 deletions src/marvin/core/ChatCompletion/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ def call_function(self, as_message=True):
"""
name, raw_arguments = itemgetter("name", "arguments")(self.function_call())
function = self.callable_registry.get(name)
arguments = function.model.parse_raw(raw_arguments)
value = function(**arguments.dict(exclude_none=True))
try:
value = function(**json.loads(raw_arguments))
except Exception:
value = function(data=json.loads(raw_arguments))
if as_message:
return {"role": "function", "name": name, "content": str(value)}
else:
Expand Down

0 comments on commit 620c637

Please sign in to comment.