Making OpenAI Assistants Teachable
Conversational assistants based on LLMs can remember the current chat with the user, and can even demonstrate in-context learning of things that the user teaches the assistant during the chat. But these memories and learnings are lost once the chat is over, or when a single chat grows too long for the LLM to handle effectively. In subsequent chats, the user is forced to repeat any necessary instructions over and over.
The optional agent capability called Teachability
addresses these
limitations by persisting user teachings across chat boundaries in
long-term memory (a vector database). Memories (called memos) are
created and saved to disk throughout a conversation, then loaded from
disk later. Instead of copying all the memos into the context window,
which would eat up valuable space, individual memos are retrieved into
context only as needed. This allows the user to teach many facts,
preferences and skills to the teachable agent just once, and have it
remember them in later chats.
In making decisions about memo storage and retrieval, Teachability
calls an instance of TextAnalyzerAgent
to analyze pieces of text in
several different ways. This adds extra LLM calls involving a relatively
small number of tokens. These calls can add a few seconds to the time a
user waits for a response.
This notebook demonstrates how Teachability
can be added to instances
of GPTAssistantAgent
so that they can learn facts, preferences, and
skills from users. As explained
here,
each instance of GPTAssistantAgent
wraps an OpenAI Assistant that can
be given a set of tools including functions, code interpreter, and
retrieval. Assistants with these tools are demonstrated in separate
standalone sections below, which can be run independently.
Requirements
AutoGen requires Python>=3.9
. To run this notebook example, please
install the [teachable] option.
Set your API Endpoint
The
config_list_from_json
function loads a list of configurations from an environment variable or
a json file.
It first looks for environment variable “OAI_CONFIG_LIST” which needs to be a valid json string. If that variable is not found, it then looks for a json file named “OAI_CONFIG_LIST”. It filters the configs by models (you can filter by other keys as well). After application of the filter shown above, only the gpt-4 models are considered.
The config list may look like the following:
If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose “upload file” icon.
You can set the value of config_list in other ways if you prefer, e.g., loading from a YAML file.
Teachable OpenAI Assistant with Functions
This section is based on agentchat_oai_assistant_function_call.ipynb, which demonstrates an assistant accomplishing a task through function calling.
Function schema and implementation
This example leverages OSS Insight (Open Source Software Insight) for advanced GitHub data analysis.
Create the OpenAI Assistant with function calling as a tool
Give the assistant a task involving function calls
Make the assistant teachable
We can make any ConversableAgent
teachable by adding a Teachability
object to it.
Test the teachable assistant
This time let’s teach the assistant a more specific way of formatting lists.
Finally, let’s clear the chat history to see whether the assistant can remember to use our preferred formatting in a new chat without having to be told again.
Delete the teachable assistant
All OpenAI Assistants can be created, viewed and deleted manually
through OpenAI’s website. They
can also be deleted through the GPTAssistantAgent
instance.
Teachable OpenAI Assistant with Code Interpreter
This section is based on agentchat_oai_code_interpreter.ipynb, which demonstrates an assistant accomplishing a task by writing code and executing it in a sandbox.
Create the OpenAI Assistant with code interpreter as a tool
Give the assistant a task involving code execution
Make the assistant teachable
We can make any ConversableAgent
teachable by adding a Teachability
object to it.
Test the teachable assistant
This time let’s teach the assistant to show its work.
Finally, let’s clear the chat history to see whether the assistant can remember to show its work in a new chat without having to be told again.
Delete the teachable assistant
All OpenAI Assistants can be created, viewed and deleted manually
through OpenAI’s website. They
can also be deleted through the GPTAssistantAgent
instance.
Teachable OpenAI Assistant with Retrieval
This section is based on agentchat_oai_assistant_retrieval.ipynb, which demonstrates an assistant accomplishing a task through retrieval augmented generation (RAG).
Create the OpenAI Assistant with retrieval as a tool
For this example, first upload the
conversable_agent.py
file to your OpenAI API account. This can be done manually through the
website. Then find the
uploaded File ID on the Files page,
and paste that ID into the file_ids
list in the code below.
Give the assistant a task involving file retrieval
When prompted, type “Does the file contain any bugs?”. On the next prompt, type “exit” to end the chat.
Make the assistant teachable
We can make any ConversableAgent
teachable by adding a Teachability
object to it.
Test the teachable assistant
This time let’s teach the assistant to report its confidence.
Finally, let’s clear the chat history to see whether the assistant can remember to report its confidence in a new chat without having to be told again.
Delete the teachable assistant
All OpenAI Assistants can be created, viewed and deleted manually
through OpenAI’s website. They
can also be deleted through the GPTAssistantAgent
instance.