Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added experimental retrieve_by_pydantic functionality. #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

keell0renz
Copy link

Hi!

Added retrieve_by_pydantic experimental function. Essentially, you can define a Pydantic model and use it as a query itself.

Advantages over traditional approach:

  1. Interface, just define Pydantic object and receive it filled at the end.
  2. Provides more opportunity to explain what kind of data you need, define constraints, examples, description. Pydantic model dump works as more detailed query text, which can reduce errors related to model not understanding what people want from it.
  3. Output data validation. If validation failed -- it outputs exception OR sends a retry request with exception data. It is not a solution to every problem, because the model itself is kinda stupid, and we have to teach it to work with these queries later, but in some cases it indeed helps (especially in context of that this is just a wrapper of existing /retrieval backend)

Later, if feature will receive positive internal feedback -- I plan to develop specific /retrieve_by_pydantic endpoint and refactor experimental SDK function into production-grade function, and later maybe MultiOn team improves the model by finetuning it.

@keell0renz
Copy link
Author

Jupyter Notebook (example usage)

from multion.client import MultiOn

client = MultiOn(api_key="53be6ee21e394198875523aa2b3ac8c1")
from pydantic import BaseModel, Field
from typing import Optional


class Socials(BaseModel):
    twitter: str
    github: str
    linkedin: str
    telegram: str
    youtube: Optional[str] = Field(
        description="The url of the youtube channel, if you find it. If you cant -- just leave blank or output None"
    )


class PortfolioData(BaseModel):
    socials: Socials
    email: str = Field(
        description="The email of the portfolio website owner, WITHOUT the mailto: prefix. Remove mailto: prefix.",
        examples=["[email protected]"],
        pattern=r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",
    )
    name: str = Field(description="The name of the portfolio website owner.")
    description: str = Field(
        description="A short description of the portfolio website owner."
    )
    university: str = Field(description="The university the portfolio website owner is affiliated with.")
portfolio_data = client.retrieve_by_pydantic(model=PortfolioData, url="https://keellorenz.com", attempts=2)
portfolio_data

@keell0renz
Copy link
Author

Also, I will add this function to async version later

Copy link
Contributor

@NamanGarg20 NamanGarg20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants