Agent Chat with custom model loading
In this notebook, we demonstrate how a custom model can be defined and loaded, and what protocol it needs to comply to.
NOTE: Depending on what model you use, you may need to play with the default prompts of the Agent’s
Requirements
Create and configure the custom model
A custom model class can be created in many ways, but needs to adhere to
the ModelClient
protocol and response structure which is defined in
client.py and shown below.
The response protocol has some minimum requirements, but can be extended
to include any additional information that is needed. Message retrieval
therefore can be customized, but needs to return a list of strings or a
list of ModelClientResponseProtocol.Choice.Message
objects.
Example of simple custom client
Following the huggingface example for using Mistral’s Open-Orca
For the response object, python’s SimpleNamespace
is used to create a
simple object that can be used to store the response data, but any
object that follows the ClientResponseProtocol
can be used.
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 an environment variable of a specified name (“OAI_CONFIG_LIST” in this example), which needs to be a valid json string. If that variable is not found, it looks for a json file with the same name. It filters the configs by models (you can filter by other keys as well).
The json looks like the following:
You can set the value of config_list in any way you prefer. Please refer to this notebook for full code examples of the different methods.
Set the config for the custom model
You can add any paramteres that are needed for the custom model loading in the same configuration list.
It is important to add the model_client_cls
field and set it to a
string that corresponds to the class name: "CustomModelClient"
.
Construct Agents
Consturct a simple conversation between a User proxy and an Assistent agent
Register the custom client class to the assistant agent
Register a custom client class with a pre-loaded model
If you want to have more control over when the model gets loaded, you can load the model yourself and pass it as an argument to the CustomClient during registration