Skip to content

Commit

Permalink
Patch azure embeddings + handle azure deployments properly (#594)
Browse files Browse the repository at this point in the history
* Fix bug where embeddings endpoint was getting set to deployment, upgraded pinned llama-index to use new version that has azure endpoint

* updated documentation

* added memgpt example for openai

* change wording to match configure
  • Loading branch information
cpacker authored Dec 9, 2023
1 parent 4aae923 commit b6f3ecd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 36 deletions.
56 changes: 49 additions & 7 deletions docs/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 25 additions & 18 deletions memgpt/cli/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@


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():
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions memgpt/embeddings.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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":
Expand Down
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b6f3ecd

Please sign in to comment.