If you have a hosted ChatCompletion-compatible endpoint that works with function calling, you can simply set OPENAI_API_BASE
(export OPENAI_API_BASE=...
) to the IP+port of your endpoint. As of 10/22/2023, most ChatCompletion endpoints do NOT support function calls, so if you want to play with MemGPT and open models, you probably need to follow the instructions below.
- Put your own LLM behind a web server API (e.g. oobabooga web UI)
- Set
OPENAI_API_BASE=YOUR_API_IP_ADDRESS
andBACKEND_TYPE=webui
- Run MemGPT with
python3 main.py --no_verify
, it should now use your LLM instead of OpenAI GPT - If things aren't working, read the full instructions below
When using open LLMs with MemGPT, the main failure case will be your LLM outputting a string that cannot be understood by MemGPT. MemGPT uses function calling to manage memory (eg edit_core_memory(...)
and interact with the user (send_message(...)
), so your LLM needs generate outputs that can be parsed into MemGPT function calls.
To get MemGPT to work with a local LLM, you need to have the LLM running on a server that takes API requests.
For the purposes of this example, we're going to serve (host) the LLMs using oobabooga web UI, but if you want to use something else you can! This also assumes your running web UI locally - if you're running on e.g. Runpod, you'll want to follow Runpod specific instructions (for example use TheBloke's one-click UI and API)
- Install oobabooga web UI using the instructions here
- Once installed, launch the web server with
python server.py
- Navigate to the web app (if local, this is probably
http://127.0.0.1:7860
), select the model you want to use, adjust your GPU and CPU memory settings, and click "load" - If the model was loaded successfully, you should be able to access it via the API (if local, this is probably on port
5000
) - Assuming steps 1-4 went correctly, the LLM is now properly hosted on a port you can point MemGPT to!
WebUI exposes a lot of parameters that can dramatically change LLM outputs, to change these you can modify the WebUI settings file.
Once you have an LLM web server set up, all you need to do to connect it to MemGPT is set two environment variables:
OPENAI_API_BASE
- set this to the IP address of your LLM API - for example, if you're using web UI on a local machine, this will look like
http://127.0.0.1:5000
- set this to the IP address of your LLM API - for example, if you're using web UI on a local machine, this will look like
BACKEND_TYPE
- set this to
webui
- this controls how MemGPT packages the HTTP request to the webserver, see this code
- currently this is set up to work with web UI, but it might work with other backends / web servers too!
- if you'd like to use a different web server and you need a different style of HTTP request, let us know on the discussion page (letta-ai#67) and we'll try to add it ASAP
- set this to
You can change the prompt format and output parser used with the --model
flag. For example:
# this will cause MemGPT to use the airoboros-l2-70b-2.1 parsers, regardless of what model you're hosting on your web server
# you can mix and match parsers + models!
$ python3 main.py --model airoboros-l2-70b-2.1
# assuming we're running a model (eg airoboros) behind a textgen webui server
export OPENAI_API_BASE=127.0.0.1:5000 # change this to your actual API address
export BACKEND_TYPE=webui # if you don't set this, MemGPT will throw an error
# using --no_verify can be helpful if the LLM you're using doesn't output inner monologue properly
$ python3 main.py --no_verify
Running... [exit by typing '/exit']
💭 Bootup sequence complete. Persona activated. Testing messaging functionality.
💭 None
🤖 Welcome! My name is Sam. How can I assist you today?
Enter your message: My name is Brad, not Chad...
💭 None
⚡🧠 [function] updating memory with core_memory_replace:
First name: Chad
→ First name: Brad
edit_core_memory(...)
) and interact with the user (send_message(...)
), so your LLM needs generate outputs that can be parsed into MemGPT function calls.
To support function calling with open LLMs for MemGPT, we utilize "wrapper" code that:
- turns
system
(the MemGPT instructions),messages
(the MemGPT conversation window), andfunctions
(the MemGPT function set) parameters from ChatCompletion into a single unified prompt string for your LLM - turns the output string generated by your LLM back into a MemGPT function call
Different LLMs are trained using different prompt formats (eg #USER:
vs <im_start>user
vs ...), and LLMs that are trained on function calling are often trained using different function call formats, so if you're getting poor performance, try experimenting with different prompt formats! We recommend starting with the prompt format (and function calling format) recommended in the HuggingFace model card, and experimenting from there.
We currently only support a few prompt formats in this repo (located here)! If you write a new parser, please open a PR and we'll merge it in.
To make a new wrapper (for example, because you want to try a different prompt format), you just need to subclass LLMChatCompletionWrapper
. Your new wrapper class needs to implement two functions:
- One to go from ChatCompletion messages/functions schema to a prompt string
- And one to go from raw LLM outputs to a ChatCompletion response
class LLMChatCompletionWrapper(ABC):
@abstractmethod
def chat_completion_to_prompt(self, messages, functions):
"""Go from ChatCompletion to a single prompt string"""
pass
@abstractmethod
def output_to_chat_completion_response(self, raw_llm_output):
"""Turn the LLM output string into a ChatCompletion response"""
pass
You can follow our example wrappers (located here).
Example with Airoboros (llama2 finetune)
To help you get started, we've implemented an example wrapper class for a popular llama2 model finetuned on function calling (Airoboros). We want MemGPT to run well on open models as much as you do, so we'll be actively updating this page with more examples. Additionally, we welcome contributions from the community! If you find an open LLM that works well with MemGPT, please open a PR with a model wrapper and we'll merge it ASAP.
class Airoboros21Wrapper(LLMChatCompletionWrapper):
"""Wrapper for Airoboros 70b v2.1: https://huggingface.co/jondurbin/airoboros-l2-70b-2.1"""
def chat_completion_to_prompt(self, messages, functions):
"""
Examples for how airoboros expects its prompt inputs: https://huggingface.co/jondurbin/airoboros-l2-70b-2.1#prompt-format
Examples for how airoboros expects to see function schemas: https://huggingface.co/jondurbin/airoboros-l2-70b-2.1#agentfunction-calling
"""
def output_to_chat_completion_response(self, raw_llm_output):
"""Turn raw LLM output into a ChatCompletion style response with:
"message" = {
"role": "assistant",
"content": ...,
"function_call": {
"name": ...
"arguments": {
"arg1": val1,
...
}
}
}
"""
See full file here.
MemGPT uses function calling to do memory management. With OpenAI's ChatCompletion API, you can pass in a function schema in the functions
keyword arg, and the API response will include a function_call
field that includes the function name and the function arguments (generated JSON). How this works under the hood is your functions
keyword is combined with the messages
and system
to form one big string input to the transformer, and the output of the transformer is parsed to extract the JSON function call.
In the future, more open LLMs and LLM servers (that can host OpenAI-compatable ChatCompletion endpoints) may start including parsing code to do this automatically as standard practice. However, in the meantime, when you see a model that says it supports “function calling”, like Airoboros, it doesn't mean that you can just load Airoboros into a ChatCompletion-compatable endpoint like WebUI, and then use the same OpenAI API call and it'll just work.
-
When a model page says it supports function calling, they probably mean that the model was finetuned on some function call data (not that you can just use ChatCompletion with functions out-of-the-box). Remember, LLMs are just string-in-string-out, so there are many ways to format the function call data. E.g. Airoboros formats the function schema in YAML style (see https://huggingface.co/jondurbin/airoboros-l2-70b-3.1.2#agentfunction-calling) and the output is in JSON style. To get this to work behind a ChatCompletion API, you still have to do the parsing from
functions
keyword arg (containing the schema) to the model's expected schema style in the prompt (YAML for Airoboros), and you have to run some code to extract the function call (JSON for Airoboros) and package it cleanly as afunction_call
field in the response. -
Partly because of how complex it is to support function calling, most (all?) of the community projects that do OpenAI ChatCompletion endpoints for arbitrary open LLMs do not support function calling, because if they did, they would need to write model-specific parsing code for each one.
Because of the poor state of function calling support in existing ChatCompletion API serving code, we instead provide a light wrapper on top of ChatCompletion that adds parsers to handle function calling support. These parsers need to be specific to the model you're using (or at least specific to the way it was trained on function calling). We hope that our example code will help the community add additional compatability of MemGPT with more function-calling LLMs - we will also add more model support as we test more models and find those that work well enough to run MemGPT's function set.
To run the example of MemGPT with Airoboros, you'll need to host the model behind some LLM web server (for example webui). Then, all you need to do is point MemGPT to this API endpoint by setting the environment variables OPENAI_API_BASE
and BACKEND_TYPE
. Now, instead of calling ChatCompletion on OpenAI's API, MemGPT will use it's own ChatCompletion wrapper that parses the system, messages, and function arguments into a format that Airoboros has been finetuned on, and once Airoboros generates a string output, MemGPT will parse the response to extract a potential function call (knowing what we know about Airoboros expected function call output).
Ask for help on our Discord or post on the GitHub discussion.