Hi, I have a compatibility issue with the transiti...
# ask-for-help
m
Hi, I have a compatibility issue with the transition from older decorated functions and runners API to the new class based API. The old API, e.g.
Copy code
@svc.api(
    input=JSON(pydantic_model=S3BucketAndKeys),
    output=JSON(pydantic_model=ListOfPoseEstimationResults),
)
def func(s3_objects):
...
accepted raw JSON requests, without any HTML form encoding. I used this for inference:
Copy code
<http://requests.post|requests.post>(url, headers={'accept': 'application/json'}, data='{....}')
The new class based API:
Copy code
@bentoml.service()
class Service: 
    @bentoml.api()
    def func(self, s3_objects: S3BucketAndKeys) -> ListOfPoseEstimationResults:
    ...
expects HTML form encoded inputs. The
s3_objects
parameter needs to be explicitly named:
Copy code
<http://requests.post|requests.post>(url, data='{"s3_objects": ...}')
Is it possible to stay compatible with the older API?
c
yes, you can achieve the same behavior, see https://docs.bentoml.com/en/latest/guides/iotypes.html#pydantic
Copy code
from pydantic import BaseModel, Field
import typing as t
import bentoml

class AdsGenerationParams(BaseModel):
    prompt: str = Field(description="The prompt text")
    industry: str = Field(description="The industry the company belongs to")
    target_audience: str = Field(description="Target audience for the advertisement")
    temperature: float = Field(default=0.0, description="A sampling temperature between 0 and 2")

@bentoml.service
class AdsWriter:
    @bentoml.api(input_spec=AdsGenerationParams)
    def generate(self, **params: t.Any) -> str:

        # Access parameters from the request
        prompt = params['prompt']
        industry = params['industry']
        target_audience = params['target_audience']
        temperature = params['temperature']
        # Use the parameters in your Service logic
        # Implementation logic
        ...
m
Thanks! Works as expected.