PrefectHQ / marvin

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

response_model functionality not working with azure openai #507

Open TheodorosGalanos opened 1 year ago

TheodorosGalanos commented 1 year ago

First check

Bug summary

I am trying to use the new functionality with marvin.openai and response_model. However, it seems like marvin is looking for an openai api_key (as an input to the marvin.openai.ChatCompletion) even though I've passed my azure_openai.api_key in marvin.settings.

Reproduction

class QnA(BaseModel):
    answer: str

def marvin_response(system, user, assistant, response_model):
    return openai.ChatCompletion.create(
        messages = [
                {"role": "system", "content": system},
                {"role": "user", "content": user},
                {"role": "assistant", "content": assistant}
            ],
        response_model = response_model,
        temperature=0,
        max_tokens=600,
        #api_key = os.environ["AZURE_OPENAI_API_KEY"] passing this didn't help.
    ).to_model()

system = "..."
user = "..."
assistant = "..."
response = marvin_response(system, user, assistant, response_model=QnA)

Error

Traceback (most recent call last):
  File "C:\Users\.....py", line 113, in <module>
    response = marvin_response(system, prompt, assistant, response_model=QnA)
  File "C:\Users\...\marvin_utils.py", line 54, in marvin_response
    return openai.ChatCompletion.create(
  File "C:\Users\...\Python310\site-packages\marvin\openai\ChatCompletion\__init__.py", line 55, in create
    config = getattr(cls, "__config__", ChatCompletionConfig())
  File "pydantic\env_settings.py", line 40, in pydantic.env_settings.BaseSettings.__init__
  File "pydantic\main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for ChatCompletionConfig
api_key
  none is not an allowed value (type=type_error.none.not_allowed)

Versions

1.2.1

Additional context

No response

aaazzam commented 1 year ago

Thanks @TheodorosGalanos! @zzstoatzz and I are looking into this

dcsan commented 11 months ago

hi @TheodorosGalanos can you explain what the response_model is for exactly? it seems to automagically format responses but I can't find any docs on openAI's site. is this a fastAPI feature, to basically conform a response to a model?

I came across the usage here too: https://github.com/yoheinakajima/instagraph/blob/main/main.py#L144C18-L144C18