PrefectHQ / marvin

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

`@ai_classifier` Azure Configuration Appears to Have No Effect #606

Open JSv4 opened 11 months ago

JSv4 commented 11 months ago

First check

Bug summary

I have been having some issues using the @ai_classifier decorators with an Azure Open AI. It looks like the classifier keeps trying to ping openai for the classification even though I've specified that it should use our Azure OpenAI deployment.

I tried to configure marvin with both runtime and env file variables. I've also tried adding and dropping the ai_classifier model argument.

No matter what I do, I keep getting the error below about my api key (for azure) being invalid (for platform.openai.com). Seems like the model configuration I configured for marvin is not actually making it through to the ai_classifier I defined.

Reproduction

marvin.settings.llm_model = 'azure_openai/gpt-4'
marvin.settings.azure_openai.api_key = api_key
marvin.settings.azure_openai.api_base = api_base
marvin.settings.azure_openai.deployment_name = AZURE_CS_OPENAI_ENGINE
marvin.settings.azure_openai.api_type = "azure"

...

@ai_classifier(model='azure_openai/gpt-4')
class DocumentType(Enum):
   """
   This is extremely simplistic and meant for sharing purposes... we're doing something less... stupid
   ""
    K = "Contract"
    NOT_K = "Non-Contract"

doc_type = DocumentType("Hey, Bob, can you remember to bring my stapler back? It's the red swingline one. Thanks, Milton.")

### Error

```python3
ValueError: "Hey, Bob, can you remember to bring my stapler back? It's the red swingline one. Thanks, Milton." is not a valid DocumentType

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jsv4/PycharmProjects/TikaTest/DocClassifier.py", line 74, in <module>
    doc_type = DocumentType("Hey, Bob, can you remember to bring my stapler back? It's the red swingline one. Thanks, Milton.")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/marvin/components/ai_classifier.py", line 120, in __call__
    return super().__call__(
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/enum.py", line 711, in __call__
    return cls.__new__(cls, value)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/enum.py", line 1136, in __new__
    raise exc
  File "/usr/lib/python3.11/enum.py", line 1113, in __new__
    result = cls._missing_(value)
             ^^^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/marvin/components/ai_classifier.py", line 141, in _missing_
    response: int = cls.call(value)
                    ^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/marvin/components/ai_classifier.py", line 250, in call
    return getattr(chat_completion.create().to_model(), "index")  # type: ignore
                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/marvin/core/ChatCompletion/abstract.py", line 156, in create
    response_data = self._send_request(**serialized_request)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/marvin/core/ChatCompletion/providers/openai.py", line 99, in _send_request
    response = openai.ChatCompletion.create(**serialized_request)  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/openai/api_resources/chat_completion.py", line 25, in create
    return super().create(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 155, in create
    response, _, api_key = requestor.request(
                           ^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/openai/api_requestor.py", line 299, in request
    resp, got_stream = self._interpret_response(result, stream)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/openai/api_requestor.py", line 710, in _interpret_response
    self._interpret_response_line(
  File "/home/jsv4/PycharmProjects/TikaTest/venv/lib/python3.11/site-packages/openai/api_requestor.py", line 775, in _interpret_response_line
    raise self.handle_error_response(
openai.error.AuthenticationError: Incorrect API key provided: 64f525a0********************XXXX. You can find your API key at https://platform.openai.com/account/api-keys.

### Versions

```Text
Tried versions 1.5.0 and 1.5.1

Additional context

No response

aaazzam commented 11 months ago

hey @JSv4

I can reproduce the bug. PR'd a fix, waiting for test suite. Will tag here when done.

Fad-ai commented 11 months ago

also having same issue

JSv4 commented 10 months ago

Hey @aaazzam, any updates on this? I would love to experiment with Marvin but using Azure Open AI is pretty much a requirement for me.

aaazzam commented 10 months ago

Hey! That’s my bad - I think this was addressed in a recent minor release (1.5.2) but I didn’t do you the courtesy of tagging this issue or closing it.

If you update your version of Marvin I suspect that’ll do the trick!

On Mon, Oct 30, 2023 at 5:39 PM JSIV @.***> wrote:

Hey @aaazzam https://github.com/aaazzam, any updates on this? I would love to experiment with Marvin but using Azure Open AI is pretty much a requirement for me.

— Reply to this email directly, view it on GitHub https://github.com/PrefectHQ/marvin/issues/606#issuecomment-1786090060, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH4DG2NU2JWMPOP5S2EDLA3YCANAZAVCNFSM6AAAAAA5QAP53WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBWGA4TAMBWGA . You are receiving this because you were mentioned.Message ID: @.***>

JSv4 commented 9 months ago

Cool. Thanks @aaazzam !