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_agent_logging.py 8.1 kB

Logging (#1146) * WIP:logging * serialize request, response and client * Fixed code formatting. * Updated to use a global package, and added some test cases. Still very-much a draft. * Update work in progress. * adding cost * log new agent * update log_completion test in test_agent_telemetry * tests * fix formatting * Added additional telemetry for wrappers and clients. * WIP: add test for oai client and oai wrapper table * update test_telemetry * fix format * More tests, update doc and clean up * small fix for session id - moved to start_logging and return from start_logging * update start_logging type to return str, add notebook to demonstrate use of telemetry * add ability to get log dataframe * precommit formatting fixes * formatting fix * Remove pandas dependency from telemetry and only use in notebook * formatting fixes * log query exceptions * fix formatting * fix ci * fix comment - add notebook link in doc and fix groupchat serialization * small fix * do not serialize Agent * formatting * wip * fix test * serialization bug fix for soc moderator * fix test and clean up * wip: add version table * fix test * fix test * fix test * make the logging interface more general and fix client model logging * fix format * fix formatting and tests * fix * fix comment * Renaming telemetry to logging * update notebook * update doc * formatting * formatting and clean up * fix doc * fix link and title * fix notebook format and fix comment * format * try fixing agent test and update migration guide * fix link * debug print * debug * format * add back tests * fix tests --------- Co-authored-by: Adam Fourney <adamfo@microsoft.com> Co-authored-by: Victor Dibia <victordibia@microsoft.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
2 years ago
Logging (#1146) * WIP:logging * serialize request, response and client * Fixed code formatting. * Updated to use a global package, and added some test cases. Still very-much a draft. * Update work in progress. * adding cost * log new agent * update log_completion test in test_agent_telemetry * tests * fix formatting * Added additional telemetry for wrappers and clients. * WIP: add test for oai client and oai wrapper table * update test_telemetry * fix format * More tests, update doc and clean up * small fix for session id - moved to start_logging and return from start_logging * update start_logging type to return str, add notebook to demonstrate use of telemetry * add ability to get log dataframe * precommit formatting fixes * formatting fix * Remove pandas dependency from telemetry and only use in notebook * formatting fixes * log query exceptions * fix formatting * fix ci * fix comment - add notebook link in doc and fix groupchat serialization * small fix * do not serialize Agent * formatting * wip * fix test * serialization bug fix for soc moderator * fix test and clean up * wip: add version table * fix test * fix test * fix test * make the logging interface more general and fix client model logging * fix format * fix formatting and tests * fix * fix comment * Renaming telemetry to logging * update notebook * update doc * formatting * formatting and clean up * fix doc * fix link and title * fix notebook format and fix comment * format * try fixing agent test and update migration guide * fix link * debug print * debug * format * add back tests * fix tests --------- Co-authored-by: Adam Fourney <adamfo@microsoft.com> Co-authored-by: Victor Dibia <victordibia@microsoft.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
2 years ago
Logging (#1146) * WIP:logging * serialize request, response and client * Fixed code formatting. * Updated to use a global package, and added some test cases. Still very-much a draft. * Update work in progress. * adding cost * log new agent * update log_completion test in test_agent_telemetry * tests * fix formatting * Added additional telemetry for wrappers and clients. * WIP: add test for oai client and oai wrapper table * update test_telemetry * fix format * More tests, update doc and clean up * small fix for session id - moved to start_logging and return from start_logging * update start_logging type to return str, add notebook to demonstrate use of telemetry * add ability to get log dataframe * precommit formatting fixes * formatting fix * Remove pandas dependency from telemetry and only use in notebook * formatting fixes * log query exceptions * fix formatting * fix ci * fix comment - add notebook link in doc and fix groupchat serialization * small fix * do not serialize Agent * formatting * wip * fix test * serialization bug fix for soc moderator * fix test and clean up * wip: add version table * fix test * fix test * fix test * make the logging interface more general and fix client model logging * fix format * fix formatting and tests * fix * fix comment * Renaming telemetry to logging * update notebook * update doc * formatting * formatting and clean up * fix doc * fix link and title * fix notebook format and fix comment * format * try fixing agent test and update migration guide * fix link * debug print * debug * format * add back tests * fix tests --------- Co-authored-by: Adam Fourney <adamfo@microsoft.com> Co-authored-by: Victor Dibia <victordibia@microsoft.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
2 years ago
Logging (#1146) * WIP:logging * serialize request, response and client * Fixed code formatting. * Updated to use a global package, and added some test cases. Still very-much a draft. * Update work in progress. * adding cost * log new agent * update log_completion test in test_agent_telemetry * tests * fix formatting * Added additional telemetry for wrappers and clients. * WIP: add test for oai client and oai wrapper table * update test_telemetry * fix format * More tests, update doc and clean up * small fix for session id - moved to start_logging and return from start_logging * update start_logging type to return str, add notebook to demonstrate use of telemetry * add ability to get log dataframe * precommit formatting fixes * formatting fix * Remove pandas dependency from telemetry and only use in notebook * formatting fixes * log query exceptions * fix formatting * fix ci * fix comment - add notebook link in doc and fix groupchat serialization * small fix * do not serialize Agent * formatting * wip * fix test * serialization bug fix for soc moderator * fix test and clean up * wip: add version table * fix test * fix test * fix test * make the logging interface more general and fix client model logging * fix format * fix formatting and tests * fix * fix comment * Renaming telemetry to logging * update notebook * update doc * formatting * formatting and clean up * fix doc * fix link and title * fix notebook format and fix comment * format * try fixing agent test and update migration guide * fix link * debug print * debug * format * add back tests * fix tests --------- Co-authored-by: Adam Fourney <adamfo@microsoft.com> Co-authored-by: Victor Dibia <victordibia@microsoft.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
2 years ago
Logging (#1146) * WIP:logging * serialize request, response and client * Fixed code formatting. * Updated to use a global package, and added some test cases. Still very-much a draft. * Update work in progress. * adding cost * log new agent * update log_completion test in test_agent_telemetry * tests * fix formatting * Added additional telemetry for wrappers and clients. * WIP: add test for oai client and oai wrapper table * update test_telemetry * fix format * More tests, update doc and clean up * small fix for session id - moved to start_logging and return from start_logging * update start_logging type to return str, add notebook to demonstrate use of telemetry * add ability to get log dataframe * precommit formatting fixes * formatting fix * Remove pandas dependency from telemetry and only use in notebook * formatting fixes * log query exceptions * fix formatting * fix ci * fix comment - add notebook link in doc and fix groupchat serialization * small fix * do not serialize Agent * formatting * wip * fix test * serialization bug fix for soc moderator * fix test and clean up * wip: add version table * fix test * fix test * fix test * make the logging interface more general and fix client model logging * fix format * fix formatting and tests * fix * fix comment * Renaming telemetry to logging * update notebook * update doc * formatting * formatting and clean up * fix doc * fix link and title * fix notebook format and fix comment * format * try fixing agent test and update migration guide * fix link * debug print * debug * format * add back tests * fix tests --------- Co-authored-by: Adam Fourney <adamfo@microsoft.com> Co-authored-by: Victor Dibia <victordibia@microsoft.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import pytest
  2. import autogen
  3. import autogen.runtime_logging
  4. import json
  5. import sys
  6. import uuid
  7. import sqlite3
  8. from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
  9. from conftest import skip_openai
  10. TEACHER_MESSAGE = """
  11. You are roleplaying a math teacher, and your job is to help your students with linear algebra.
  12. Keep your explanations short.
  13. """
  14. STUDENT_MESSAGE = """
  15. You are roleplaying a high school student strugling with linear algebra.
  16. Regardless how well the teacher explains things to you, you just don't quite get it.
  17. Keep your questions short.
  18. """
  19. CHAT_COMPLETIONS_QUERY = """SELECT id, invocation_id, client_id, wrapper_id, session_id,
  20. request, response, is_cached, cost, start_time, end_time FROM chat_completions;"""
  21. AGENTS_QUERY = "SELECT id, agent_id, wrapper_id, session_id, name, class, init_args, timestamp FROM agents"
  22. OAI_CLIENTS_QUERY = "SELECT id, client_id, wrapper_id, session_id, class, init_args, timestamp FROM oai_clients"
  23. OAI_WRAPPERS_QUERY = "SELECT id, wrapper_id, session_id, init_args, timestamp FROM oai_wrappers"
  24. if not skip_openai:
  25. config_list = autogen.config_list_from_json(
  26. OAI_CONFIG_LIST,
  27. filter_dict={
  28. "model": ["gpt-4", "gpt-4-0314", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
  29. },
  30. file_location=KEY_LOC,
  31. )
  32. llm_config = {"config_list": config_list}
  33. num_of_configs = len(config_list)
  34. ###############################################################
  35. @pytest.fixture(scope="function")
  36. def db_connection():
  37. autogen.runtime_logging.start(config={"dbname": ":memory:"})
  38. con = autogen.runtime_logging.get_connection()
  39. con.row_factory = sqlite3.Row
  40. yield con
  41. autogen.runtime_logging.stop()
  42. @pytest.mark.skipif(
  43. sys.platform in ["darwin", "win32"] or skip_openai,
  44. reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
  45. )
  46. def test_two_agents_logging(db_connection):
  47. cur = db_connection.cursor()
  48. teacher = autogen.AssistantAgent(
  49. "teacher",
  50. system_message=TEACHER_MESSAGE,
  51. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  52. llm_config=llm_config,
  53. max_consecutive_auto_reply=2,
  54. )
  55. student = autogen.AssistantAgent(
  56. "student",
  57. system_message=STUDENT_MESSAGE,
  58. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  59. llm_config=llm_config,
  60. max_consecutive_auto_reply=1,
  61. )
  62. student.initiate_chat(
  63. teacher,
  64. message="Can you explain the difference between eigenvalues and singular values again?",
  65. )
  66. # Verify log completions table
  67. cur.execute(CHAT_COMPLETIONS_QUERY)
  68. rows = cur.fetchall()
  69. assert len(rows) >= 3 # some config may fail
  70. session_id = rows[0]["session_id"]
  71. for idx, row in enumerate(rows):
  72. assert (
  73. row["invocation_id"] and str(uuid.UUID(row["invocation_id"], version=4)) == row["invocation_id"]
  74. ), "invocation id is not valid uuid"
  75. assert row["client_id"], "client id is empty"
  76. assert row["wrapper_id"], "wrapper id is empty"
  77. assert row["session_id"] and row["session_id"] == session_id
  78. request = json.loads(row["request"])
  79. first_request_message = request["messages"][0]["content"]
  80. first_request_role = request["messages"][0]["role"]
  81. if idx == 0 or idx == 2:
  82. assert first_request_message == TEACHER_MESSAGE
  83. elif idx == 1:
  84. assert first_request_message == STUDENT_MESSAGE
  85. assert first_request_role == "system"
  86. response = json.loads(row["response"])
  87. if "response" in response: # config failed or response was empty
  88. assert response["response"] is None or "error_code" in response["response"]
  89. else:
  90. assert "choices" in response and len(response["choices"]) > 0
  91. assert row["cost"] > 0
  92. assert row["start_time"], "start timestamp is empty"
  93. assert row["end_time"], "end timestamp is empty"
  94. # Verify agents table
  95. cur.execute(AGENTS_QUERY)
  96. rows = cur.fetchall()
  97. assert len(rows) == 2
  98. session_id = rows[0]["session_id"]
  99. for idx, row in enumerate(rows):
  100. assert row["wrapper_id"], "wrapper id is empty"
  101. assert row["session_id"] and row["session_id"] == session_id
  102. agent = json.loads(row["init_args"])
  103. if idx == 0:
  104. assert row["name"] == "teacher"
  105. assert agent["name"] == "teacher"
  106. agent["system_message"] == TEACHER_MESSAGE
  107. elif idx == 1:
  108. assert row["name"] == "student"
  109. assert agent["name"] == "student"
  110. agent["system_message"] = STUDENT_MESSAGE
  111. assert "api_key" not in row["init_args"]
  112. assert row["timestamp"], "timestamp is empty"
  113. # Verify oai client table
  114. cur.execute(OAI_CLIENTS_QUERY)
  115. rows = cur.fetchall()
  116. assert len(rows) == num_of_configs * 2 # two agents
  117. session_id = rows[0]["session_id"]
  118. for row in rows:
  119. assert row["client_id"], "client id is empty"
  120. assert row["wrapper_id"], "wrapper id is empty"
  121. assert row["session_id"] and row["session_id"] == session_id
  122. assert row["class"] in ["AzureOpenAI", "OpenAI"]
  123. init_args = json.loads(row["init_args"])
  124. if row["class"] == "AzureOpenAI":
  125. assert "api_version" in init_args
  126. assert row["timestamp"], "timestamp is empty"
  127. # Verify oai wrapper table
  128. cur.execute(OAI_WRAPPERS_QUERY)
  129. rows = cur.fetchall()
  130. session_id = rows[0]["session_id"]
  131. for row in rows:
  132. assert row["wrapper_id"], "wrapper id is empty"
  133. assert row["session_id"] and row["session_id"] == session_id
  134. init_args = json.loads(row["init_args"])
  135. assert "config_list" in init_args
  136. assert len(init_args["config_list"]) > 0
  137. assert row["timestamp"], "timestamp is empty"
  138. @pytest.mark.skipif(
  139. sys.platform in ["darwin", "win32"] or skip_openai,
  140. reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
  141. )
  142. def test_groupchat_logging(db_connection):
  143. cur = db_connection.cursor()
  144. teacher = autogen.AssistantAgent(
  145. "teacher",
  146. system_message=TEACHER_MESSAGE,
  147. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  148. llm_config=llm_config,
  149. max_consecutive_auto_reply=2,
  150. )
  151. student = autogen.AssistantAgent(
  152. "student",
  153. system_message=STUDENT_MESSAGE,
  154. is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
  155. llm_config=llm_config,
  156. max_consecutive_auto_reply=1,
  157. )
  158. groupchat = autogen.GroupChat(
  159. agents=[teacher, student], messages=[], max_round=3, speaker_selection_method="round_robin"
  160. )
  161. group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
  162. student.initiate_chat(
  163. group_chat_manager,
  164. message="Can you explain the difference between eigenvalues and singular values again?",
  165. )
  166. # Verify chat_completions message
  167. cur.execute(CHAT_COMPLETIONS_QUERY)
  168. rows = cur.fetchall()
  169. assert len(rows) >= 2 # some config may fail
  170. # Verify group chat manager agent
  171. cur.execute(AGENTS_QUERY)
  172. rows = cur.fetchall()
  173. assert len(rows) == 3
  174. chat_manager_query = "SELECT agent_id, name, class, init_args FROM agents WHERE name = 'chat_manager'"
  175. cur.execute(chat_manager_query)
  176. rows = cur.fetchall()
  177. assert len(rows) == 1
  178. # Verify oai clients
  179. cur.execute(OAI_CLIENTS_QUERY)
  180. rows = cur.fetchall()
  181. assert len(rows) == num_of_configs * 3 # three agents
  182. # Verify oai wrappers
  183. cur.execute(OAI_WRAPPERS_QUERY)
  184. rows = cur.fetchall()
  185. assert len(rows) == 3
  186. # Verify schema version
  187. version_query = "SELECT id, version_number from version"
  188. cur.execute(version_query)
  189. rows = cur.fetchall()
  190. assert len(rows) == 1
  191. assert rows[0]["id"] == 1 and rows[0]["version_number"] == 1