Skip to content

Fix the AzureOpenAI #83

@harshalmore31

Description

@harshalmore31

It doesn't record the conversation automatically properly, it only records the starting first conversation !

#!/usr/bin/env python3
import os
from dotenv import load_dotenv

from openai import AzureOpenAI
from memori import Memori
from memori.core.providers import ProviderConfig

# --- Load secrets from .env ---
load_dotenv()

# --- Configure Memori with Azure OpenAI ---
azure_provider = ProviderConfig.from_azure(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
    api_version=os.environ["AZURE_OPENAI_API_VERSION"],
    model=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],  # usually same as deployment
)

print("🧠 Initializing Memori memory system...")

memory_system = Memori(
    database_connect="sqlite:///azure_openai_memory.db",
    conscious_ingest=True,
    auto_ingest=True,
    verbose=True,
    provider_config=azure_provider,
    namespace="azure_openai_example",
)
memory_system.enable()

# --- Azure OpenAI client ---
client = AzureOpenAI(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_version=os.environ["AZURE_OPENAI_API_VERSION"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)

def chat():
    print("\n✅ Setup complete! Chat with your memory-enhanced assistant.")
    print("Type 'quit' or 'exit' to end.\n")

    conversation_count = 0

    while True:
        try:
            user_input = input("You: ").strip()
            if user_input.lower() in ["quit", "exit", "bye"]:
                print("\nAI: Goodbye! I'll remember our conversation. 🤖✨")
                break
            if not user_input:
                continue

            conversation_count += 1
            print(f"\nAI (thinking... #{conversation_count})")

            # --- Call Azure OpenAI directly ---
            response = client.chat.completions.create(
                model=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
                messages=[{"role": "user", "content": user_input}],
            )

            ai_output = response.choices[0].message.content
            print(f"AI: {ai_output}\n")

        except KeyboardInterrupt:
            print("\n\nAI: Goodbye! I'll remember this chat. 🤖✨")
            break
        except Exception as e:
            print(f"\nError: {str(e)}\nPlease try again.\n")

    print("\n📊 Session Summary:")
    print(f"- Conversations processed: {conversation_count}")
    print("- Memory database: azure_openai_memory.db")
    print("- Namespace: azure_openai_example")

if __name__ == "__main__":
    chat()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions