PrefectHQ / marvin

✨ Build AI interfaces that spark joy
https://askmarvin.ai
Apache License 2.0
5.34k stars 348 forks source link

audio.speak: unexpected keyword argument 'openai_api_key' #882

Closed lostmygithubaccount closed 8 months ago

lostmygithubaccount commented 8 months ago

First check

Bug summary

trying either example on the docs here: https://www.askmarvin.ai/docs/audio/speech

leads to a traceback:

    332 async def generate_speech(
    333     self,
    334     input: str,
    335     file: Optional[Path] = None,
    336     **kwargs: Any,
    337 ) -> Optional["HttpxBinaryResponseContent"]:
--> 338     response = await self.client.audio.speech.create(
    339         input=input, **settings.openai.audio.speech.model_dump() | kwargs
    340     )
    341     if file:
    342         response.stream_to_file(file)

TypeError: AsyncSpeech.create() got an unexpected keyword argument 'openai_api_key'

Reproduction

import marvin

audio = marvin.speak("I sure like being inside this fancy computer!")

Error

TypeError: AsyncSpeech.create() got an unexpected keyword argument 'openai_api_key'

Versions

Version:                2.3.0
Python version:         3.11.8
OS/Arch:                darwin/arm64

Additional context

No response

lostmygithubaccount commented 8 months ago

this might already be fixed on main?

lostmygithubaccount commented 8 months ago

or rather it's an openai version issue...

lostmygithubaccount commented 8 months ago

if you just pass kwargs through on this line, it works: https://github.com/openai/openai-python/blob/main/src/openai/resources/audio/speech.py#L127

which seems to be how it was on older versions of openai. but if I upgrade and try to use marvin I'm hitting other issues

lostmygithubaccount commented 8 months ago

so if I go back to openai==1.13.4, I get an error: ImportError: cannot import name 'AssistantEventHandler' from 'openai'

if I go above openai==1.14.0 I get the above error

lostmygithubaccount commented 8 months ago

this is very annoying (on OpenAI Python SDK's part, not Marvin) but I'm unblocked I suppose by just patching openai to add (back?) the **kwargs input, not sure how y'all would want to handle all these breaking changes they seem to make in minor versions :)

zzstoatzz commented 8 months ago

hey @lostmygithubaccount - thanks for the issue

hmmm - I almost wonder if this is a settings prefix issue again

if you just open ipython where you see the error and do something like

import marvin

marvin.settings.openai.audio.speech.model_dump()

do you have a setting in there named openai_api_key?

lostmygithubaccount commented 8 months ago

I have both marvin_openai_api_key and openai_api_key set (to the same value)

zzstoatzz commented 8 months ago

woof, yeah. if you're willing to try a little surgery, I bet if you go here and make this

class OpenAISettings(MarvinSettings):
    model_config = SettingsConfigDict(
        env_prefix="marvin_openai_",
        extra="ignore" # add this line
    )
    ...

that this would work

lostmygithubaccount commented 8 months ago

undoing my openai hack and adding that, I still get:

TypeError: AsyncSpeech.create() got an unexpected keyword argument 'openai_api_key'

zzstoatzz commented 8 months ago

hmm, so I can repro, do you have your api key set in some .env file? or where is it coming from

zzstoatzz commented 8 months ago

oops hold on, speech settings redefines its own SettingsConfigDict so it doesnt inherit from OpenAISettings

try

class SpeechSettings(MarvinSettings):
    """Settings for OpenAI's speech API.

    Attributes:
        model: The default speech model to use, defaults to `tts-1-hd`.
        voice: The default voice to use, defaults to `echo`.
        response_format: The default response format to use, defaults to `mp3`.
        speed: The default speed to use, defaults to `1.0`.
    """

    model_config = SettingsConfigDict(
        env_prefix="marvin_speech_", extra="ignore",
    )

    model: str = Field(
        default="tts-1-hd",
        description="The default model to use.",
    )
    voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"] = Field(
        default="echo",
    )
    response_format: Literal["mp3", "opus", "aac", "flac"] = Field(default="mp3")
    speed: float = Field(default=1.0)

I reproduced and this solves for me

zzstoatzz commented 8 months ago

@lostmygithubaccount if you can confirm this works for you, I can get this released momentarily

lostmygithubaccount commented 8 months ago

@zzstoatzz that fixed it, appreciate the quick turnaround! installed from the PR and re-ran

lostmygithubaccount commented 8 months ago

oh and I'm setting environment variables in ~/.marvin/.env

zzstoatzz commented 8 months ago

thanks for the report and context @lostmygithubaccount - v2.3.1 is out with the fix