Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
move webui to new openai completions endpoint, but also provide existing functionality via webui-legacy backend (#468)
Charles Packer576795ffdbce
diff --git a/memgpt/cli/cli_config.py b/memgpt/cli/cli_config.pyindex cd8d08118..c57a40024 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", "llamacpp", "koboldcpp", "ollama", "lmstudio", "openai"]+ backend_options = ["webui", "webui-legacy", "llamacpp", "koboldcpp", "ollama", "lmstudio", "openai"]default_model_endpoint_type = Noneif config.model_endpoint_type in backend_options:# set from previous configdiff --git a/memgpt/local_llm/chat_completion_proxy.py b/memgpt/local_llm/chat_completion_proxy.pyindex 63a0b540d..4067bffef 100644--- a/memgpt/local_llm/chat_completion_proxy.py+++ b/memgpt/local_llm/chat_completion_proxy.py@@ -5,6 +5,7 @@ import requestsimport jsonfrom .webui.api import get_webui_completion+from .webui.legacy_api import get_webui_completion as get_webui_completion_legacyfrom .lmstudio.api import get_lmstudio_completionfrom .llamacpp.api import get_llamacpp_completionfrom .koboldcpp.api import get_koboldcpp_completion@@ -84,6 +85,8 @@ def get_chat_completion(try:if endpoint_type == "webui":result = get_webui_completion(endpoint, prompt, context_window, grammar=grammar_name)+ elif endpoint_type == "webui-legacy":+ result = get_webui_completion_legacy(endpoint, prompt, context_window, grammar=grammar_name)elif endpoint_type == "lmstudio":result = get_lmstudio_completion(endpoint, prompt, context_window)elif endpoint_type == "llamacpp":diff --git a/memgpt/local_llm/constants.py b/memgpt/local_llm/constants.pyindex ecb1e6374..f310abd82 100644--- a/memgpt/local_llm/constants.py+++ b/memgpt/local_llm/constants.py@@ -5,6 +5,7 @@ DEFAULT_ENDPOINTS = {"llamacpp": "http://localhost:8080","lmstudio": "http://localhost:1234","ollama": "http://localhost:11434",+ "webui-legacy": "http://localhost:5000","webui": "http://localhost:5000",}diff --git a/memgpt/local_llm/webui/api.py b/memgpt/local_llm/webui/api.pyindex b447cf8e5..29a859969 100644--- a/memgpt/local_llm/webui/api.py+++ b/memgpt/local_llm/webui/api.py@@ -5,12 +5,12 @@ import requestsfrom .settings import SIMPLEfrom ..utils import load_grammar_file, count_tokens-WEBUI_API_SUFFIX = "/api/v1/generate"+WEBUI_API_SUFFIX = "/v1/completions"DEBUG = Falsedef get_webui_completion(endpoint, prompt, context_window, settings=SIMPLE, grammar=None):- """See https://github.com/oobabooga/text-generation-webui for instructions on how to run the LLM web server"""+ """Compatibility for the new OpenAI API: https://github.com/oobabooga/text-generation-webui/wiki/12-%E2%80%90-OpenAI-API#examples"""prompt_tokens = count_tokens(prompt)if prompt_tokens > context_window:raise Exception(f"Request exceeds maximum context length ({prompt_tokens} > {context_window} tokens)")@@ -18,7 +18,8 @@ def get_webui_completion(endpoint, prompt, context_window, settings=SIMPLE, gram# Settings for the generation, includes the prompt + stop tokens, max length, etcrequest = settingsrequest["prompt"] = prompt- request["truncation_length"] = context_window # assuming mistral 7b+ request["max_tokens"] = context_window+ # request["truncation_length"] = context_window# Set grammarif grammar is not None:@@ -32,7 +33,7 @@ def get_webui_completion(endpoint, prompt, context_window, settings=SIMPLE, gramresponse = requests.post(URI, json=request)if response.status_code == 200:result = response.json()- result = result["results"][0]["text"]+ result = result["choices"][0]["text"]if DEBUG:print(f"json API response.text: {result}")else:diff --git a/memgpt/local_llm/webui/legacy_api.py b/memgpt/local_llm/webui/legacy_api.pynew file mode 100644index 000000000..c84c2e7b2--- /dev/null+++ b/memgpt/local_llm/webui/legacy_api.py@@ -0,0 +1,48 @@+import os+from urllib.parse import urljoin+import requests++from .legacy_settings import SIMPLE+from ..utils import load_grammar_file, count_tokens++WEBUI_API_SUFFIX = "/api/v1/generate"+DEBUG = False+++def get_webui_completion(endpoint, prompt, context_window, settings=SIMPLE, grammar=None):+ """See https://github.com/oobabooga/text-generation-webui for instructions on how to run the LLM web server"""+ 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["truncation_length"] = context_window # assuming mistral 7b++ # Set grammar+ if grammar is not None:+ 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://")++ 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["results"][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 web UI server is running and reachable at {URI}."+ )++ except:+ # TODO handle gracefully+ raise++ return resultdiff --git a/memgpt/local_llm/webui/legacy_settings.py b/memgpt/local_llm/webui/legacy_settings.pynew file mode 100644index 000000000..cdccc67be--- /dev/null+++ b/memgpt/local_llm/webui/legacy_settings.py@@ -0,0 +1,25 @@+from ...constants import LLM_MAX_TOKENS++SIMPLE = {+ "stopping_strings": [+ "\nUSER:",+ "\nASSISTANT:",+ "\nFUNCTION RETURN:",+ "\nUSER",+ "\nASSISTANT",+ "\nFUNCTION RETURN",+ "\nFUNCTION",+ "\nFUNC",+ "<|im_start|>",+ "<|im_end|>",+ "<|im_sep|>",+ # '\n' ++ # '</s>',+ # '<|',+ # '\n#',+ # '\n\n\n',+ ],+ "max_new_tokens": 3072,+ # "truncation_length": 4096, # assuming llama2 models+ # "truncation_length": LLM_MAX_TOKENS, # assuming mistral 7b+}diff --git a/memgpt/local_llm/webui/settings.py b/memgpt/local_llm/webui/settings.pyindex cdccc67be..806540744 100644--- a/memgpt/local_llm/webui/settings.py+++ b/memgpt/local_llm/webui/settings.py@@ -19,7 +19,7 @@ SIMPLE = {# '\n#',# '\n\n\n',],- "max_new_tokens": 3072,+ # "max_tokens": 3072,# "truncation_length": 4096, # assuming llama2 models# "truncation_length": LLM_MAX_TOKENS, # assuming mistral 7b}