Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove openai package #534

Merged
merged 12 commits into from
Nov 30, 2023
6 changes: 3 additions & 3 deletions memgpt/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from memgpt.config import AgentConfig, MemGPTConfig
from .system import get_login_event, package_function_response, package_summarize_message, get_initial_boot_messages
from .memory import CoreMemory as Memory, summarize_messages
from .openai_tools import completions_with_backoff as create
from .openai_tools import completions_with_backoff as create, is_context_overflow_error
from memgpt.openai_tools import chat_completion_with_backoff
from .utils import get_local_time, parse_json, united_diff, printd, count_tokens, get_schema_diff
from .constants import (
Expand Down Expand Up @@ -649,14 +649,14 @@ def step(self, user_message, first_message=False, first_message_retry_limit=FIRS
printd(f"step() failed\nuser_message = {user_message}\nerror = {e}")

# If we got a context alert, try trimming the messages length, then try again
if "maximum context length" in str(e):
if is_context_overflow_error(e):
# A separate API call to run a summarizer
self.summarize_messages_inplace()

# Try step again
return self.step(user_message, first_message=first_message)
else:
printd(f"step() failed with openai.InvalidRequestError, but didn't recognize the error message: '{str(e)}'")
printd(f"step() failed with an unrecognized exception: '{str(e)}'")
raise e

def summarize_messages_inplace(self, cutoff=None, preserve_last_N_messages=True):
Expand Down
1 change: 0 additions & 1 deletion memgpt/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
from prettytable import PrettyTable
import questionary
import openai

from llama_index import set_global_service_context
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
Expand Down
1 change: 0 additions & 1 deletion memgpt/cli/cli_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import builtins
import questionary
import openai
from prettytable import PrettyTable
import typer
import os
Expand Down
7 changes: 5 additions & 2 deletions memgpt/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ def embedding_model():
)
return model
elif endpoint == "azure":
# 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(
model="text-embedding-ada-002",
deployment_name=config.azure_embedding_deployment,
model=model,
deployment_name=deployment,
sarahwooders marked this conversation as resolved.
Show resolved Hide resolved
api_key=config.azure_key,
api_base=config.azure_endpoint,
api_type="azure",
Expand Down
50 changes: 24 additions & 26 deletions memgpt/local_llm/chat_completion_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import requests
import json

from .webui.api import get_webui_completion
from .webui.legacy_api import get_webui_completion as get_webui_completion_legacy
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
from ..prompts.gpt_summarize import SYSTEM as SUMMARIZE_SYSTEM_MESSAGE
from ..errors import LocalLLMConnectionError, LocalLLMError
from box import Box

from memgpt.local_llm.webui.api import get_webui_completion
from memgpt.local_llm.webui.legacy_api import get_webui_completion as get_webui_completion_legacy
from memgpt.local_llm.lmstudio.api import get_lmstudio_completion
from memgpt.local_llm.llamacpp.api import get_llamacpp_completion
from memgpt.local_llm.koboldcpp.api import get_koboldcpp_completion
from memgpt.local_llm.ollama.api import get_ollama_completion
from memgpt.local_llm.vllm.api import get_vllm_completion
from memgpt.local_llm.llm_chat_completion_wrappers import simple_summary_wrapper
from memgpt.local_llm.constants import DEFAULT_WRAPPER
from memgpt.local_llm.utils import get_available_wrappers
from memgpt.prompts.gpt_summarize import SYSTEM as SUMMARIZE_SYSTEM_MESSAGE
from memgpt.errors import LocalLLMConnectionError, LocalLLMError

endpoint = os.getenv("OPENAI_API_BASE")
endpoint_type = os.getenv("BACKEND_TYPE") # default None == ChatCompletion
Expand Down Expand Up @@ -119,25 +121,21 @@ def get_chat_completion(
raise LocalLLMError(f"Failed to parse JSON from local LLM response - error: {str(e)}")

# unpack with response.choices[0].message.content
response = DotDict(
response = Box(
{
"model": model,
"choices": [
DotDict(
{
"message": DotDict(chat_completion_result),
"finish_reason": "stop", # TODO vary based on backend response
}
)
],
"usage": DotDict(
{
# TODO fix, actually use real info
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0,
"message": chat_completion_result,
"finish_reason": "stop", # TODO vary based on backend response
}
),
],
"usage": {
# TODO fix, actually use real info
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0,
},
}
)
return response
17 changes: 0 additions & 17 deletions memgpt/local_llm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@
import memgpt.local_llm.llm_chat_completion_wrappers.zephyr as zephyr


class DotDict(dict):
"""Allow dot access on properties similar to OpenAI response object"""

def __getattr__(self, attr):
return self.get(attr)

def __setattr__(self, key, value):
self[key] = value

# following methods necessary for pickling
def __getstate__(self):
return vars(self)

def __setstate__(self, state):
vars(self).update(state)


def load_grammar_file(grammar):
# Set grammar
grammar_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "grammars", f"{grammar}.gbnf")
Expand Down
5 changes: 5 additions & 0 deletions memgpt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@ def process_agent_step(user_message, no_verify):
with console.status("[bold cyan]Thinking...") as status:
new_messages, user_message, skip_next_user_input = process_agent_step(user_message, no_verify)
break
except KeyboardInterrupt:
print("User interrupt occured.")
retry = questionary.confirm("Retry agent.step()?").ask()
if not retry:
break
except Exception as e:
print("An exception ocurred when running agent.step(): ")
traceback.print_exc()
Expand Down
Loading
Loading