Mirascope / mirascope

LLM abstractions that aren't obstructions
https://docs.mirascope.io/
MIT License
659 stars 35 forks source link

How to use **kwargs to dynamically pass in input parameters? #453

Closed stephenleo closed 2 weeks ago

stephenleo commented 3 weeks ago

Question

My prompt could take one of several values, and the function must be flexible in accepting the arguments required by each prompt.

Generally, I'd use **kwargs when the function arguments are not known in advance, but doing so throws an error.

It looks like Mirascope is looking for {"genre": "science fiction"} but using **kwargs makes the attrs {"kwargs": {"genre": "science fiction"}}

Minimal working example.

from mirascope.core import openai, prompt_template
from pydantic import BaseModel

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

prompt = "Recommend a {genre} book"

@openai.call(model="gpt-4o-mini", response_model=Book)
@prompt_template(prompt)
def recommend_book(**kwargs):
    ...

book = recommend_book(genre="science fiction")

Error message:

KeyError: 'genre'
willbakst commented 2 weeks ago

fix is now released in v1.0.5!

thanks for catching this :)

willbakst commented 2 weeks ago

I actually just realized this fix only handled the case when specifically using the name kwargs, but it's possible to name it anything (e.g. **call_kwargs), so I've released an updated fix to handle this in v1.0.6

For example, the following should work now even with the updated ** naming:

from mirascope.core import openai, prompt_template

@openai.call("gpt-4o-mini")
@prompt_template("recommend a {genre} book")
def recommend_book(**whatever_i_want_to_call_this): ...

response = recommend_book(genre="fantasy")
print(response.content)
stephenleo commented 2 weeks ago

It works now. thanks!