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.

vLLM support (#492)

Charles Packerde0ccea18158
diff --git a/memgpt/cli/cli_config.py b/memgpt/cli/cli_config.py
index 6c05bf84d..cea2ab2a5 100644
--- a/memgpt/cli/cli_config.py
+++ b/memgpt/cli/cli_config.py
@@ -59,7 +59,7 @@ def configure_llm_endpoint(config: MemGPTConfig):
model_endpoint_type = "azure"
_, model_endpoint, _, _, _ = get_azure_credentials()
else: # local models
- backend_options = ["webui", "webui-legacy", "llamacpp", "koboldcpp", "ollama", "lmstudio", "openai"]
+ backend_options = ["webui", "webui-legacy", "llamacpp", "koboldcpp", "ollama", "lmstudio", "vllm", "openai"]
default_model_endpoint_type = None
if config.model_endpoint_type in backend_options:
# set from previous config
@@ -116,13 +116,24 @@ def configure_model(config: MemGPTConfig, model_endpoint_type: str):
).ask()
model = None if len(model) == 0 else model
+ # vllm needs huggingface model tag
+ if model_endpoint_type == "vllm":
+ default_model = config.model if config.model and config.model_endpoint_type == "vllm" else ""
+ model = questionary.text(
+ "Enter HuggingFace model tag (e.g. ehartford/dolphin-2.2.1-mistral-7b):",
+ default=default_model,
+ ).ask()
+ model = None if len(model) == 0 else model
+ model_wrapper = None # no model wrapper for vLLM
+
# model wrapper
- available_model_wrappers = builtins.list(get_available_wrappers().keys())
- model_wrapper = questionary.select(
- f"Select default model wrapper (recommended: {DEFAULT_WRAPPER_NAME}):",
- choices=available_model_wrappers,
- default=DEFAULT_WRAPPER_NAME,
- ).ask()
+ if model_endpoint_type != "vllm":
+ available_model_wrappers = builtins.list(get_available_wrappers().keys())
+ model_wrapper = questionary.select(
+ f"Select default model wrapper (recommended: {DEFAULT_WRAPPER_NAME}):",
+ choices=available_model_wrappers,
+ default=DEFAULT_WRAPPER_NAME,
+ ).ask()
# set: context_window
if str(model) not in LLM_MAX_TOKENS:
diff --git a/memgpt/local_llm/chat_completion_proxy.py b/memgpt/local_llm/chat_completion_proxy.py
index 4067bffef..9dda26e4d 100644
--- a/memgpt/local_llm/chat_completion_proxy.py
+++ b/memgpt/local_llm/chat_completion_proxy.py
@@ -10,6 +10,7 @@ from .lmstudio.api import get_lmstudio_completion
from .llamacpp.api import get_llamacpp_completion
from .koboldcpp.api import get_koboldcpp_completion
from .ollama.api import get_ollama_completion
+from .vllm.api import get_vllm_completion
from .llm_chat_completion_wrappers import airoboros, dolphin, zephyr, simple_summary_wrapper
from .constants import DEFAULT_WRAPPER
from .utils import DotDict, get_available_wrappers
@@ -95,6 +96,8 @@ def get_chat_completion(
result = get_koboldcpp_completion(endpoint, prompt, context_window, grammar=grammar_name)
elif endpoint_type == "ollama":
result = get_ollama_completion(endpoint, model, prompt, context_window)
+ elif endpoint_type == "vllm":
+ result = get_vllm_completion(endpoint, model, prompt, context_window)
else:
raise LocalLLMError(
f"BACKEND_TYPE is not set, please set variable depending on your backend (webui, lmstudio, llamacpp, koboldcpp)"
diff --git a/memgpt/local_llm/vllm/api.py b/memgpt/local_llm/vllm/api.py
new file mode 100644
index 000000000..76d638124
--- /dev/null
+++ b/memgpt/local_llm/vllm/api.py
@@ -0,0 +1,52 @@
+import os
+from urllib.parse import urljoin
+import requests
+
+from ..utils import load_grammar_file, count_tokens
+
+WEBUI_API_SUFFIX = "/completions"
+DEBUG = False
+
+
+def get_vllm_completion(endpoint, model, prompt, context_window, settings={}, grammar=None):
+ """https://github.com/vllm-project/vllm/blob/main/examples/api_client.py"""
+ prompt_tokens = count_tokens(prompt)
+ if prompt_tokens > context_window:
+ raise Exception(f"Request exceeds maximum context length ({prompt_tokens} > {context_window} tokens)")
+
+ # Settings for the generation, includes the prompt + stop tokens, max length, etc
+ request = settings
+ request["prompt"] = prompt
+ request["max_tokens"] = int(context_window - prompt_tokens)
+ request["stream"] = False
+
+ # currently hardcoded, since we are only supporting one model with the hosted endpoint
+ request["model"] = model
+
+ # Set grammar
+ if grammar is not None:
+ raise NotImplementedError
+ request["grammar_string"] = load_grammar_file(grammar)
+
+ if not endpoint.startswith(("http://", "https://")):
+ raise ValueError(f"Endpoint ({endpoint}) must begin with http:// or https://")
+
+ try:
+ URI = urljoin(endpoint.strip("/") + "/", WEBUI_API_SUFFIX.strip("/"))
+ response = requests.post(URI, json=request)
+ if response.status_code == 200:
+ result = response.json()
+ result = result["choices"][0]["text"]
+ if DEBUG:
+ print(f"json API response.text: {result}")
+ else:
+ raise Exception(
+ f"API call got non-200 response code (code={response.status_code}, msg={response.text}) for address: {URI}."
+ + f" Make sure that the vLLM server is running and reachable at {URI}."
+ )
+
+ except:
+ # TODO handle gracefully
+ raise
+
+ return result
diff --git a/memgpt/local_llm/webui/api.py b/memgpt/local_llm/webui/api.py
index 710b31883..3fb88f014 100644
--- a/memgpt/local_llm/webui/api.py
+++ b/memgpt/local_llm/webui/api.py
@@ -27,7 +27,7 @@ def get_webui_completion(endpoint, prompt, context_window, settings=SIMPLE, gram
request["grammar_string"] = load_grammar_file(grammar)
if not endpoint.startswith(("http://", "https://")):
- raise ValueError(f"Provided OPENAI_API_BASE value ({endpoint}) must begin with http:// or https://")
+ raise ValueError(f"Endpoint value ({endpoint}) must begin with http:// or https://")
try:
URI = urljoin(endpoint.strip("/") + "/", WEBUI_API_SUFFIX.strip("/"))