You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_conversable_agent_update_model.py 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import pytest
  2. from autogen import AssistantAgent, UserProxyAgent
  3. import sys
  4. sys.path.append("samples/tools/finetuning")
  5. from finetuning import update_model # noqa: E402
  6. from typing import Dict # noqa: E402
  7. sys.path.append("test")
  8. TEST_CUSTOM_RESPONSE = "This is a custom response."
  9. TEST_LOCAL_MODEL_NAME = "local_model_name"
  10. def test_custom_model_client():
  11. TEST_LOSS = 0.5
  12. class UpdatableCustomModel:
  13. def __init__(self, config: Dict):
  14. self.model = config["model"]
  15. self.model_name = config["model"]
  16. def create(self, params):
  17. from types import SimpleNamespace
  18. response = SimpleNamespace()
  19. # need to follow Client.ClientResponseProtocol
  20. response.choices = []
  21. choice = SimpleNamespace()
  22. choice.message = SimpleNamespace()
  23. choice.message.content = TEST_CUSTOM_RESPONSE
  24. response.choices.append(choice)
  25. response.model = self.model
  26. return response
  27. def message_retrieval(self, response):
  28. return [response.choices[0].message.content]
  29. def cost(self, response) -> float:
  30. """Calculate the cost of the response."""
  31. response.cost = 0
  32. return 0
  33. @staticmethod
  34. def get_usage(response) -> Dict:
  35. return {}
  36. def update_model(self, preference_data, messages, **kwargs):
  37. return {"loss": TEST_LOSS}
  38. config_list = [{"model": TEST_LOCAL_MODEL_NAME, "model_client_cls": "UpdatableCustomModel"}]
  39. assistant = AssistantAgent(
  40. "assistant",
  41. system_message="You are a helpful assistant.",
  42. human_input_mode="NEVER",
  43. llm_config={"config_list": config_list},
  44. )
  45. assistant.register_model_client(model_client_cls=UpdatableCustomModel)
  46. user_proxy = UserProxyAgent(
  47. "user_proxy",
  48. human_input_mode="NEVER",
  49. max_consecutive_auto_reply=1,
  50. code_execution_config=False,
  51. llm_config=False,
  52. )
  53. res = user_proxy.initiate_chat(assistant, message="2+2=", silent=True)
  54. response_content = res.summary
  55. assert response_content == TEST_CUSTOM_RESPONSE
  56. preference_data = [("this is what the response should have been like", response_content)]
  57. update_model_stats = update_model(assistant, preference_data, user_proxy)
  58. assert update_model_stats["update_stats"]["loss"] == TEST_LOSS
  59. def test_update_model_without_client_raises_error():
  60. assistant = AssistantAgent(
  61. "assistant",
  62. system_message="You are a helpful assistant.",
  63. human_input_mode="NEVER",
  64. max_consecutive_auto_reply=0,
  65. llm_config=False,
  66. code_execution_config=False,
  67. )
  68. user_proxy = UserProxyAgent(
  69. "user_proxy",
  70. human_input_mode="NEVER",
  71. max_consecutive_auto_reply=1,
  72. code_execution_config=False,
  73. llm_config=False,
  74. )
  75. user_proxy.initiate_chat(assistant, message="2+2=", silent=True)
  76. with pytest.raises(ValueError):
  77. update_model(assistant, [], user_proxy)
  78. def test_custom_model_update_func_missing_raises_error():
  79. class UpdatableCustomModel:
  80. def __init__(self, config: Dict):
  81. self.model = config["model"]
  82. self.model_name = config["model"]
  83. def create(self, params):
  84. from types import SimpleNamespace
  85. response = SimpleNamespace()
  86. # need to follow Client.ClientResponseProtocol
  87. response.choices = []
  88. choice = SimpleNamespace()
  89. choice.message = SimpleNamespace()
  90. choice.message.content = TEST_CUSTOM_RESPONSE
  91. response.choices.append(choice)
  92. response.model = self.model
  93. return response
  94. def message_retrieval(self, response):
  95. return [response.choices[0].message.content]
  96. def cost(self, response) -> float:
  97. """Calculate the cost of the response."""
  98. response.cost = 0
  99. return 0
  100. @staticmethod
  101. def get_usage(response) -> Dict:
  102. return {}
  103. config_list = [{"model": TEST_LOCAL_MODEL_NAME, "model_client_cls": "UpdatableCustomModel"}]
  104. assistant = AssistantAgent(
  105. "assistant",
  106. system_message="You are a helpful assistant.",
  107. human_input_mode="NEVER",
  108. llm_config={"config_list": config_list},
  109. )
  110. assistant.register_model_client(model_client_cls=UpdatableCustomModel)
  111. user_proxy = UserProxyAgent(
  112. "user_proxy",
  113. human_input_mode="NEVER",
  114. max_consecutive_auto_reply=1,
  115. code_execution_config=False,
  116. llm_config=False,
  117. )
  118. res = user_proxy.initiate_chat(assistant, message="2+2=", silent=True)
  119. response_content = res.summary
  120. assert response_content == TEST_CUSTOM_RESPONSE
  121. with pytest.raises(NotImplementedError):
  122. update_model(assistant, [], user_proxy)
  123. def test_multiple_model_clients_raises_error():
  124. class UpdatableCustomModel:
  125. def __init__(self, config: Dict):
  126. self.model = config["model"]
  127. self.model_name = config["model"]
  128. def create(self, params):
  129. from types import SimpleNamespace
  130. response = SimpleNamespace()
  131. # need to follow Client.ClientResponseProtocol
  132. response.choices = []
  133. choice = SimpleNamespace()
  134. choice.message = SimpleNamespace()
  135. choice.message.content = TEST_CUSTOM_RESPONSE
  136. response.choices.append(choice)
  137. response.model = self.model
  138. return response
  139. def message_retrieval(self, response):
  140. return [response.choices[0].message.content]
  141. def cost(self, response) -> float:
  142. """Calculate the cost of the response."""
  143. response.cost = 0
  144. return 0
  145. @staticmethod
  146. def get_usage(response) -> Dict:
  147. return {}
  148. def update_model(self, preference_data, messages, **kwargs):
  149. return {}
  150. config_list = [
  151. {"model": TEST_LOCAL_MODEL_NAME, "model_client_cls": "UpdatableCustomModel"},
  152. {"model": TEST_LOCAL_MODEL_NAME, "model_client_cls": "UpdatableCustomModel"},
  153. ]
  154. assistant = AssistantAgent(
  155. "assistant",
  156. system_message="You are a helpful assistant.",
  157. human_input_mode="NEVER",
  158. llm_config={"config_list": config_list},
  159. )
  160. assistant.register_model_client(model_client_cls=UpdatableCustomModel)
  161. assistant.register_model_client(model_client_cls=UpdatableCustomModel)
  162. user_proxy = UserProxyAgent(
  163. "user_proxy",
  164. human_input_mode="NEVER",
  165. max_consecutive_auto_reply=1,
  166. code_execution_config=False,
  167. llm_config=False,
  168. )
  169. user_proxy.initiate_chat(assistant, message="2+2=", silent=True)
  170. with pytest.raises(ValueError):
  171. update_model(assistant, [], user_proxy)