Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

Patch azure embeddings + handle azure deployments properly (#594)

Charles Packerb2e88da27718
diff --git a/docs/endpoints.md b/docs/endpoints.md
index 409ac57f0..7d0750410 100644
--- a/docs/endpoints.md
+++ b/docs/endpoints.md
@@ -8,25 +8,67 @@ set OPENAI_API_KEY=YOUR_API_KEY # on Windows
$Env:OPENAI_API_KEY = "YOUR_API_KEY" # on Windows (PowerShell)
```
+When you run `memgpt configure`, make sure to select `openai` for both the LLM inference provider and embedding provider, for example:
+```
+$ memgpt configure
+? Select LLM inference provider: openai
+? Override default endpoint: https://api.openai.com/v1
+? Select default model (recommended: gpt-4): gpt-4
+? Select embedding provider: openai
+? Select default preset: memgpt_chat
+? Select default persona: sam_pov
+? Select default human: cs_phd
+? Select storage backend for archival data: local
+```
+
#### OpenAI Proxies
To use custom OpenAI endpoints, specify a proxy URL when running `memgpt configure` to set the custom endpoint as the default endpoint.
-
-## Azure
+## Azure OpenAI
To use MemGPT with Azure, expore the following variables and then re-run `memgpt configure`:
```sh
# see https://github.com/openai/openai-python#microsoft-azure-endpoints
-export AZURE_OPENAI_KEY = ...
-export AZURE_OPENAI_ENDPOINT = ...
-export AZURE_OPENAI_VERSION = ...
+export AZURE_OPENAI_KEY=...
+export AZURE_OPENAI_ENDPOINT=...
+export AZURE_OPENAI_VERSION=...
# set the below if you are using deployment ids
-export AZURE_OPENAI_DEPLOYMENT = ...
-export AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT = ...
+export AZURE_OPENAI_DEPLOYMENT=...
+export AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT=...
+```
+
+For example, if your endpoint is `customproject.openai.azure.com` (for both your GPT model and your embeddings model), you would set the following:
+```sh
+# change AZURE_OPENAI_VERSION to the latest version
+export AZURE_OPENAI_KEY="YOUR_AZURE_KEY"
+export AZURE_OPENAI_VERSION="2023-08-01-preview"
+export AZURE_OPENAI_ENDPOINT="https://customproject.openai.azure.com"
+export AZURE_OPENAI_EMBEDDING_ENDPOINT="https://customproject.openai.azure.com"
+```
+
+If you named your deployments names other than their defaults, you would also set the following:
+```sh
+# assume you called the gpt-4 (1106-Preview) deployment "personal-gpt-4-turbo"
+export AZURE_OPENAI_DEPLOYMENT="personal-gpt-4-turbo"
+
+# assume you called the text-embedding-ada-002 deployment "personal-embeddings"
+export AZURE_OPENAI_EMBEDDING_DEPLOYMENT="personal-embeddings"
```
Replace `export` with `set` or `$Env:` if you are on Windows (see the OpenAI example).
+When you run `memgpt configure`, make sure to select `azure` for both the LLM inference provider and embedding provider, for example:
+```
+$ memgpt configure
+? Select LLM inference provider: azure
+? Select default model (recommended: gpt-4): gpt-4-1106-preview
+? Select embedding provider: azure
+? Select default preset: memgpt_chat
+? Select default persona: sam_pov
+? Select default human: cs_phd
+? Select storage backend for archival data: local
+```
+
Note: **your Azure endpoint must support functions** or you will get an error. See [this GitHub issue](https://github.com/cpacker/MemGPT/issues/91) for more information.
## Local Models & Custom Endpoints
diff --git a/memgpt/cli/cli_config.py b/memgpt/cli/cli_config.py
index 1d07e6e1c..787d6bb59 100644
--- a/memgpt/cli/cli_config.py
+++ b/memgpt/cli/cli_config.py
@@ -19,12 +19,17 @@ app = typer.Typer()
def get_azure_credentials():
- azure_key = os.getenv("AZURE_OPENAI_KEY")
- azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
- azure_version = os.getenv("AZURE_OPENAI_VERSION")
- azure_deployment = os.getenv("AZURE_OPENAI_DEPLOYMENT")
- azure_embedding_deployment = os.getenv("AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT")
- return azure_key, azure_endpoint, azure_version, azure_deployment, azure_embedding_deployment
+ creds = dict(
+ azure_key=os.getenv("AZURE_OPENAI_KEY"),
+ azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
+ azure_version=os.getenv("AZURE_OPENAI_VERSION"),
+ azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
+ azure_embedding_deployment=os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT"),
+ )
+ # embedding endpoint and version default to non-embedding
+ creds["azure_embedding_endpoint"] = os.getenv("AZURE_OPENAI_EMBEDDING_ENDPOINT", creds["azure_endpoint"])
+ creds["azure_embedding_version"] = os.getenv("AZURE_OPENAI_EMBEDDING_VERSION", creds["azure_version"])
+ return creds
def get_openai_credentials():
@@ -53,7 +58,7 @@ def configure_llm_endpoint(config: MemGPTConfig):
provider = "openai"
elif provider == "azure":
model_endpoint_type = "azure"
- _, model_endpoint, _, _, _ = get_azure_credentials()
+ model_endpoint = get_azure_credentials()["azure_endpoint"]
else: # local models
backend_options = ["webui", "webui-legacy", "llamacpp", "koboldcpp", "ollama", "lmstudio", "vllm", "openai"]
default_model_endpoint_type = None
@@ -179,7 +184,7 @@ def configure_embedding_endpoint(config: MemGPTConfig):
embedding_dim = 1536
elif embedding_provider == "azure":
embedding_endpoint_type = "azure"
- _, _, _, _, embedding_endpoint = get_azure_credentials()
+ embedding_endpoint = get_azure_credentials()["azure_embedding_endpoint"]
embedding_dim = 1536
elif embedding_provider == "hugging-face":
# configure hugging face embedding endpoint (https://github.com/huggingface/text-embeddings-inference)
@@ -294,13 +299,15 @@ def configure():
archival_storage_type, archival_storage_uri, archival_storage_path = configure_archival_storage(config)
# check credentials
- azure_key, azure_endpoint, azure_version, azure_deployment, azure_embedding_deployment = get_azure_credentials()
+ azure_creds = get_azure_credentials()
+ # azure_key, azure_endpoint, azure_version, azure_deployment, azure_embedding_deployment = get_azure_credentials()
openai_key = get_openai_credentials()
if model_endpoint_type == "azure" or embedding_endpoint_type == "azure":
- if all([azure_key, azure_endpoint, azure_version]):
- print(f"Using Microsoft endpoint {azure_endpoint}.")
- if all([azure_deployment, azure_embedding_deployment]):
- print(f"Using deployment id {azure_deployment}")
+ if all([azure_creds["azure_key"], azure_creds["azure_endpoint"], azure_creds["azure_version"]]):
+ print(f"Using Microsoft endpoint {azure_creds['azure_endpoint']}.")
+ # Deployment can optionally customize your endpoint model name
+ if all([azure_creds["azure_deployment"], azure_creds["azure_embedding_deployment"]]):
+ print(f"Using deployment id {azure_creds['azure_deployment']}")
else:
raise ValueError(
"Missing environment variables for Azure (see https://memgpt.readthedocs.io/en/latest/endpoints/#azure). Please set then run `memgpt configure` again."
@@ -330,11 +337,11 @@ def configure():
agent=default_agent,
# credentials
openai_key=openai_key,
- azure_key=azure_key,
- azure_endpoint=azure_endpoint,
- azure_version=azure_version,
- azure_deployment=azure_deployment,
- azure_embedding_deployment=azure_embedding_deployment,
+ azure_key=azure_creds["azure_key"],
+ azure_endpoint=azure_creds["azure_endpoint"],
+ azure_version=azure_creds["azure_version"],
+ azure_deployment=azure_creds["azure_deployment"], # OK if None
+ azure_embedding_deployment=azure_creds["azure_embedding_deployment"], # OK if None
# storage
archival_storage_type=archival_storage_type,
archival_storage_uri=archival_storage_uri,
diff --git a/memgpt/embeddings.py b/memgpt/embeddings.py
index 4dacab840..fc3023d43 100644
--- a/memgpt/embeddings.py
+++ b/memgpt/embeddings.py
@@ -1,7 +1,7 @@
import typer
from typing import Optional, List
import os
-from llama_index.embeddings import OpenAIEmbedding
+from llama_index.embeddings import OpenAIEmbedding, AzureOpenAIEmbedding
from llama_index.embeddings import TextEmbeddingsInference
from llama_index.bridge.pydantic import PrivateAttr
@@ -108,12 +108,11 @@ def embedding_model():
# https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#embeddings
model = "text-embedding-ada-002"
deployment = config.azure_embedding_deployment if config.azure_embedding_deployment is not None else model
- return OpenAIEmbedding(
+ return AzureOpenAIEmbedding(
model=model,
deployment_name=deployment,
api_key=config.azure_key,
- api_base=config.azure_endpoint,
- api_type="azure",
+ azure_endpoint=config.azure_endpoint,
api_version=config.azure_version,
)
elif endpoint == "hugging-face":
diff --git a/poetry.lock b/poetry.lock
index d2b727e71..80d529ad0 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -1,4 +1,4 @@
-# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
+# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand.
[[package]]
name = "aiohttp"
@@ -1381,13 +1381,13 @@ tests = ["pandas (>=1.4)", "pytest", "pytest-asyncio", "pytest-mock", "requests"
[[package]]
name = "llama-index"
-version = "0.9.10"
+version = "0.9.13"
description = "Interface between LLMs and your data"
optional = false
-python-versions = ">=3.8.1,<3.12"
+python-versions = ">=3.8.1,<4.0"
files = [
- {file = "llama_index-0.9.10-py3-none-any.whl", hash = "sha256:475678eea433b2e209a4faee768c67f1e7a58ba3ffd441a82c3387585e79b24e"},
- {file = "llama_index-0.9.10.tar.gz", hash = "sha256:d42f035caa206f3110c5c8e908f3c6e2dd3a1bd59c8ba5afe5466d338d230109"},
+ {file = "llama_index-0.9.13-py3-none-any.whl", hash = "sha256:1fba9039e6b223f7940d2bde7a587638af886d220d2074780660b010fb0149f5"},
+ {file = "llama_index-0.9.13.tar.gz", hash = "sha256:dd12003395e4e92e8b70879f3845e4bdd4614027870b0979253ba9291bae4c35"},
]
[package.dependencies]
@@ -4783,4 +4783,4 @@ postgres = ["pg8000", "pgvector", "psycopg", "psycopg-binary", "psycopg2-binary"
[metadata]
lock-version = "2.0"
python-versions = "<3.12,>=3.9"
-content-hash = "116de4dd474a05ec89ca9d4f03c1a1d7e3e01e6e870e44c05b1a9dafe9575456"
+content-hash = "bd58ac750b851d171a31210aca3ed4a7a627f06722043383fc52b6f3b360c463"
diff --git a/pyproject.toml b/pyproject.toml
index 918d80bec..3e2e024be 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -26,7 +26,7 @@ pytz = "^2023.3.post1"
tqdm = "^4.66.1"
black = { version = "^23.10.1", optional = true }
pytest = { version = "^7.4.3", optional = true }
-llama-index = "0.9.10"
+llama-index = "0.9.13"
setuptools = "^68.2.2"
datasets = { version = "^2.14.6", optional = true}
prettytable = "^3.9.0"