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

Decorator for writing simpler calls as functions instead of classes #312

Closed
willbakst opened this issue Jun 7, 2024 · 1 comment
Closed
Labels
Feature Request New feature or request

Comments

@willbakst
Copy link
Contributor

Description

Originally requested by @jbbakst in the slack community

Original request:

@mirascope.openai_call(model="gpt-4o", max_tokens=4096, temperature=0.3)
def recommend_book(genre: str) -> str:
    """
    SYSTEM:
    You are a literary expert, with both a deep and broad knowledge and love of books, particularly in the {genre} genre.

    USER:
    Recommend a {genre} book. Respond with ONLY the title, no other prose.
    """

book = recommend_book(genre="fantasy")

Initial thoughts:

  • For return type str we can use a standard call, but for anything else we'll need to use an extractor.
  • Ideally we can make this extremely configurable for easy swapping across providers

I'm thinking maybe we want something like this?

from mirascope.openai import openai_fn


@openai_fn(model="gpt-4o", max_tokens=4096, temperature=0.3)
def recommend_book(genre: str) -> str:
    """
    SYSTEM:
    You are a literary expert, with both a deep and broad knowledge and love of books, particularly in the {genre} genre.

    USER:
    Recommend a {genre} book. Respond with ONLY the title, no other prose.
    """

book = recommend_book(genre="fantasy")

We should also make the following work:

from mirascope.openai import openai_fn
from pydantic import BaseModel


class Book(BaseModel):
    title: str
    author: str


@openai_fn(model="gpt-4o", max_tokens=4096, temperature=0.3)
def recommend_book(genre: str) -> Book:
    """Recommend a {genre} book."""

book = recommend_book(genre="fantasy")
assert isinstance(book, Book)

Ideally we could also make the following work (using some other features in progress):

from mirascope.openai import OpenAIStream, openai_fn


@openai_fn(model="gpt-4o", max_tokens=4096, temperature=0.3)
def recommend_book(genre: str) -> OpenAIStream[str]:
    """Recommend a {genre} book. Response with ONLY the title. No other text."""

for chunk_content in recommend_book(genre="fantasy"):
    print(chunk_content, end="", flush=True)

Lastly, a general decorator could be cool:

from mirascope.base import llm_fn
from mirascope.openai import OpenAICallParams


@llm_fn(provider="openai", call_params=OpenAICallParams(...))
def recommend_book(genre: str) -> str:
    """Recommend a {genre} book."""

We could also look into doing something like this lol:

from mirascope.base import llm_fn
from mirascope.openai import OpenAICallParams


@llm_fn(provider="openai", call_params=OpenAICallParams(...))
def recommend_book(genre: str) -> str:
    """Recommend a {formatted_genre} book."""

    def formatted_genre(genre: str) -> str:
        return f"Formatted: {genre}"
@willbakst willbakst added the Feature Request New feature or request label Jun 7, 2024
@willbakst willbakst added this to the v0.18 milestone Jun 11, 2024
@willbakst
Copy link
Contributor Author

Moving work on this to #322

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant