Skip to content

Conversation

V-Silpin
Copy link
Contributor

@V-Silpin V-Silpin commented Jun 10, 2025

Description

This PR fixes the issue of content policy trigger bug in Azure OpenAI LLM. The actual cause of this trigger was a keyword "assistant". So I have added some code which replaces the keyword to "secretary " in the user prompt in azure_openai.py and azure_openai_structured.py . By changing the keyword the Memory.add function is working properly, hence fixing the bug.

Fixes #2636

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test
  • Test Script (please provide)

Test Script

from dotenv import load_dotenv
import os

from mem0.memory.main import Memory

load_dotenv()

llm_provider = os.getenv("AZURE_OPENAI_PROVIDER")
llm_model = os.getenv("AZURE_OPENAI_MODEL")
llm_temperature = float(os.getenv("AZURE_OPENAI_TEMPERATURE"))
llm_max_tokens = int(os.getenv("AZURE_OPENAI_MAX_TOKENS"))
llm_api_version = os.getenv("AZURE_OPENAI_API_VERSION")
llm_azure_deployment = os.getenv("AZURE_OPENAI_DEPLOYMENT")
llm_azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
llm_api_key = os.getenv("AZURE_OPENAI_API_KEY")

vs_provider = os.getenv("AZURE_VECTOR_STORE_PROVIDER")
vs_service_name = os.getenv("AZURE_VECTOR_STORE_SERVICE_NAME")
vs_api_key = os.getenv("AZURE_VECTOR_STORE_API_KEY")
vs_collection_name = os.getenv("AZURE_VECTOR_STORE_COLLECTION_NAME_MEM")
vs_embedding_model_dims = int(os.getenv("AZURE_VECTOR_STORE_EMBEDDING_MODEL_DIMS"))

em_provider = os.getenv("EMBEDDING_AZURE_PROVIDER")
em_model = os.getenv("EMBEDDING_AZURE_MODEL")
em_api_version = os.getenv("EMBEDDING_AZURE_API_VERSION")
em_azure_deployment = os.getenv("EMBEDDING_AZURE_DEPLOYMENT")
em_azure_endpoint = os.getenv("EMBEDDING_AZURE_ENDPOINT")
em_api_key = os.getenv("EMBEDDING_AZURE_API_KEY")

config = {
    "llm": {
        "provider": llm_provider,
        "config": {
            "model": llm_model,
            "temperature": llm_temperature,
            "max_tokens": llm_max_tokens,
            "azure_kwargs": {
                  "azure_deployment": llm_azure_deployment,
                  "api_version": llm_api_version,
                  "azure_endpoint": llm_azure_endpoint,
                  "api_key": llm_api_key,
              }
        }
    },
    "vector_store": {
        "provider": vs_provider,
        "config": {
            "service_name": vs_service_name,
            "api_key": vs_api_key,
            "collection_name": vs_collection_name, 
            "embedding_model_dims": vs_embedding_model_dims,
        }
    },
    "embedder": {
        "provider": em_provider,
        "config": {
            "model": em_model,
            "api_key": em_api_key,
            "azure_kwargs": {
                  "api_version": em_api_version,
                  "azure_deployment": em_azure_deployment,
                  "azure_endpoint": em_azure_endpoint,
                  "api_key": em_api_key,
              }
        }
    }
}

os.environ["OPENAI_API_KEY"] = llm_api_key

print("Before creating memory")
mem = Memory.from_config(config_dict=config)
print("After creating memory")

messages = [
    {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."},
    {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."}
]

print(vars(mem))

print("Storing inferred memories")
result = mem.add(messages, user_id="alice", metadata={"category": "book_recommendations"})
print("Result of storing inferred memories")
print(result)

all_memories = mem.get_all(user_id="alice")
print("Mem from azure")
print(all_memories)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

@CLAassistant
Copy link

CLAassistant commented Jun 10, 2025

CLA assistant check
All committers have signed the CLA.

@V-Silpin
Copy link
Contributor Author

@prateekchhikara Can you pls review this PR which solves the issue #2636

@Dev-Khant
Copy link
Contributor

Hey @V-Silpin Can you please sign the CLA contract?

@V-Silpin
Copy link
Contributor Author

@Dev-Khant I have completed signing the CLA contract

@V-Silpin
Copy link
Contributor Author

@Dev-Khant can you pls review & merge the code

@V-Silpin
Copy link
Contributor Author

@Dev-Khant can you pls merge this PR, all the corrections are done.

Merging this PR will resolve this issue #2636

@prateekchhikara
Copy link
Collaborator

@V-Silpin, thanks for raising the PR. Could you please update the PR description to include the issue and its fix?

@V-Silpin
Copy link
Contributor Author

@prateekchhikara I have updated the PR description, mentioned the issue & the fix

Kindly merge this PR

@V-Silpin
Copy link
Contributor Author

@prateekchhikara any issues with the PR?

@V-Silpin
Copy link
Contributor Author

V-Silpin commented Jul 2, 2025

@prateekchhikara @Dev-Khant

Pls merge this PR, as we need this issue #2636 to be solved urgently

Copy link
Contributor

@Dev-Khant Dev-Khant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, can you please rebase with main, add a test for this and also run the formatting with pre-commit? Once the CI tests pass we can merge this.

@V-Silpin V-Silpin requested a review from Dev-Khant July 3, 2025 14:29
@V-Silpin
Copy link
Contributor Author

V-Silpin commented Jul 3, 2025

@Dev-Khant I have added a testcase, done rebase & pre-commit. Kindly review it.

@Dev-Khant
Copy link
Contributor

Hey @V-Silpin There are few merge conflicts can you please resolve it?

@V-Silpin
Copy link
Contributor Author

V-Silpin commented Jul 4, 2025

@Dev-Khant Merge Conflict Fixed

@V-Silpin V-Silpin changed the title Changed keyword from assisstant to secretary Fix: Changed keyword from assisstant to secretary Jul 4, 2025
Copy link
Contributor

@Dev-Khant Dev-Khant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, looks good to me.

@Dev-Khant Dev-Khant merged commit aae5989 into mem0ai:main Jul 8, 2025
5 of 6 checks passed
Gradonhf pushed a commit to Gradonhf/mem0 that referenced this pull request Jul 11, 2025
brockshanson pushed a commit to brockshanson/mem0 that referenced this pull request Jul 30, 2025
thestumonkey pushed a commit to thestumonkey/mem0 that referenced this pull request Sep 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory.add triggering Azure OpenAI's content management policy
4 participants