mem0ai / mem0

The Memory layer for your AI apps
https://mem0.ai
Apache License 2.0
22.41k stars 2.06k forks source link

Azure Openai Deployment throwing error #1852

Closed uahmad235 closed 1 month ago

uahmad235 commented 1 month ago

🐛 Describe the bug

I am trying to get this example working from docs but getting error. The deployment works if i directly use litellm sdk.

Here's my code:

import os
from mem0 import Memory

os.environ["AZURE_API_KEY"] = AZURE_OPENAI_API_KEY

# Needed to use custom models
os.environ["AZURE_API_BASE"] = AZURE_OPENAI_ENDPOINT
os.environ["AZURE_API_VERSION"] = AZURE_API_VERSION

config = {
    "llm": {
        "provider": "litellm",
        "config": {
            "model": "azure_ai/gpt4o-custom deployment", 
            "temperature": 0.1,
            "max_tokens": 2000,
        }
    }
}

m = Memory.from_config(config)
m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})

Full Error traceback:

Exception                                 Traceback (most recent call last)
/tmp/ipykernel_238238/3704740466.py in ?()
     33     }
     34 }
     35 
     36 m = Memory.from_config(config)
---> 37 m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})

~/anaconda3/envs/venv_azra_final/lib/python3.11/site-packages/mem0/memory/main.py in ?(self, messages, user_id, agent_id, run_id, metadata, filters, prompt)
    110             future2 = executor.submit(self._add_to_graph, messages, filters)
    111 
    112             concurrent.futures.wait([future1, future2])
    113 
--> 114             vector_store_result = future1.result()
    115             graph_result = future2.result()
    116 
    117         if self.version == "v1.1":

~/anaconda3/envs/venv_azra_final/lib/python3.11/concurrent/futures/_base.py in ?(self, timeout)
    457                 else:
    458                     raise TimeoutError()
    459         finally:
    460             # Break a reference cycle with the exception in self._exception
--> 461             self = None

~/anaconda3/envs/venv_azra_final/lib/python3.11/concurrent/futures/_base.py in ?(self)
    400             try:
    401                 raise self._exception
    402             finally:
    403                 # Break a reference cycle with the exception in self._exception
--> 404                 self = None
    405         else:
    406             return self._result

~/anaconda3/envs/venv_azra_final/lib/python3.11/concurrent/futures/thread.py in ?(self)
     58             result = self.fn(*self.args, **self.kwargs)
     59         except BaseException as exc:
     60             self.future.set_exception(exc)
     61             # Break a reference cycle with the exception 'exc'
---> 62             self = None
     63         else:
     64             self.future.set_result(result)

~/anaconda3/envs/venv_azra_final/lib/python3.11/site-packages/mem0/memory/main.py in ?(self, messages, metadata, filters)
    138             user_prompt=f"Input: {parsed_messages}"
    139         else:
    140             system_prompt, user_prompt = get_fact_retrieval_messages(parsed_messages)
    141 
--> 142         response = self.llm.generate_response(
    143             messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}],
    144             response_format={"type": "json_object"},
    145         )

~/anaconda3/envs/venv_azra_final/lib/python3.11/site-packages/mem0/llms/litellm.py in ?(self, messages, response_format, tools, tool_choice)
     65 
     66         Returns:
     67             str: The generated response.
     68         """
---> 69         if not litellm.supports_function_calling(self.config.model):
     70             raise ValueError(
     71                 f"Model '{self.config.model}' in litellm does not support function calling."
     72             )

~/anaconda3/envs/venv_azra_final/lib/python3.11/site-packages/litellm/utils.py in ?(model)
   2173         if model_info.get("supports_function_calling", False) is True:
   2174             return True
   2175         return False
   2176     else:
-> 2177         raise Exception(
   2178             f"Model not supports function calling. You passed model={model}."
   2179         )

Exception: Model not supports function calling. You passed model=azure_ai/gpt4o-custom deployment.
ketangangal commented 1 month ago

Hey @uahmad235 , Looking into it will get back to you.

uahmad235 commented 1 month ago

Were you able to reproduce it? @ketangangal

uahmad235 commented 1 month ago

Finally got it working. Took me a couple of hours before i could figure it out. Docs literally need an update! Here's the solution, you have to initialize embedder as well otherwise it looks for openai embedder by default:



config = {
    "llm": {
        "provider": "azure_openai",
        "config": {
            "model": '<deployment-name-in-azure>',
            "temperature": 0.1,
            "max_tokens": 2000,
             "azure_kwargs" : {
                  "azure_deployment" : DEPLOYMENT_NAME,
                  "api_version" : AZURE_API_VERSION,
                  "azure_endpoint" : AZURE_OPENAI_ENDPOINT,
                  "api_key" : AZURE_OPENAI_API_KEY
              }
        }
    },
      "embedder": {
        "provider": "azure_openai",
        "config": {
            "model": "text-embedding-3-large",   # this is "model" name and not azure deployment name
            "azure_kwargs" : {
                  "api_version" :AZURE_API_VERSION,
                  "azure_deployment" : "<your-embeddings-deployment-in-azure-openai>",
                  "azure_endpoint" : AZURE_OPENAI_ENDPOINT,
                  "api_key": AZURE_OPENAI_API_KEY
              }
        }
    }
}