ModelClient
protocol defined in client.py
. The new model client class should implement these methods:
create()
: Returns a response object that implements the ModelClientResponseProtocol
(more details in the Protocol section).message_retrieval()
: Processes the response object and returns a list of strings or a list of message objects (more details in the Protocol section).cost()
: Returns the cost of the response.get_usage()
: Returns a dictionary with keys from RESPONSE_USAGE_KEYS = ["prompt_tokens", "completion_tokens", "total_tokens", "cost", "model"]
.model_client_cls
to the name of the new class (as a string) "model_client_cls":"CustomModelClient"
. Any other fields will be forwarded to the class constructor, so you have full control over what parameters to specify and how to use them. E.g.:
"model_client_cls":"<class name>"
has been added to an Agent’s config list, then the corresponding model with the desired class must be registered after the agent is created and before the conversation is initialized:
model_client_cls=CustomModelClient
arg matches the one specified in the OAI_CONFIG_LIST
and CustomModelClient
is the class that adheres to the ModelClient
protocol (more details on the protocol below).
If the new model client is in the config list but not registered by the time the chat is initialized, then an error will be raised.
ModelClient
protocol and response structure which is defined in client.py
and shown below.
The response protocol is currently using the minimum required fields from the autogen codebase that match the OpenAI response structure. Any response protocol that matches the OpenAI response structure will probably be more resilient to future changes, but we are starting off with minimum requirements to make adpotion of this feature easier.
create()
method: ModelClientResponseProtocol
must be followed when returning an inference response during create
call.message_retrieval()
method: returns a list of strings or a list of message objects. If a list of message objects is returned, they currently must contain the fields of OpenAI’s ChatCompletion Message object, since that is expected for function or tool calling in the rest of the codebase at the moment, unless a custom agent is being used.cost()
method: returns an integer, and if you don’t care about cost tracking you can just return 0
.get_usage()
: returns a dictionary, and if you don’t care about usage tracking you can just return an empty dictionary {}
.OAI_CONFIG_LIST
and that that entry has the "model_client_cls":"<custom-model-class-name>"
field.agent.register_model_client(model_client_cls=<class-of-custom-model>, [other optional args])
OAI_CONFIG_LIST
have been registered.