Skip to content

Commit 767d06c

Browse files
Restore TeachableAgent tests (microsoft#761)
* Update chat_with_teachable_agent.py to v2. * Update agentchat_teachability.ipynb to v2. * Add test of teachability accuracy. * Update installation instructions. * Add to contrib tests. * pre-commit fixes * Apply reviewer suggestions to test workflows.
1 parent 69de915 commit 767d06c

File tree

7 files changed

+152
-25
lines changed

7 files changed

+152
-25
lines changed

.github/workflows/contrib-openai.yml

+38
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,41 @@ jobs:
138138
with:
139139
file: ./coverage.xml
140140
flags: unittests
141+
TeachableAgent:
142+
strategy:
143+
matrix:
144+
os: [ubuntu-latest]
145+
python-version: ["3.11"]
146+
runs-on: ${{ matrix.os }}
147+
environment: openai1
148+
steps:
149+
# checkout to pr branch
150+
- name: Checkout
151+
uses: actions/checkout@v3
152+
with:
153+
ref: ${{ github.event.pull_request.head.sha }}
154+
- name: Set up Python ${{ matrix.python-version }}
155+
uses: actions/setup-python@v4
156+
with:
157+
python-version: ${{ matrix.python-version }}
158+
- name: Install packages and dependencies
159+
run: |
160+
docker --version
161+
python -m pip install --upgrade pip wheel
162+
pip install -e .[teachable]
163+
python -c "import autogen"
164+
pip install coverage
165+
- name: Coverage
166+
env:
167+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
168+
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}
169+
AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }}
170+
OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }}
171+
run: |
172+
coverage run -a -m pytest test/agentchat/contrib/test_teachable_agent.py
173+
coverage xml
174+
- name: Upload coverage to Codecov
175+
uses: codecov/codecov-action@v3
176+
with:
177+
file: ./coverage.xml
178+
flags: unittests

.github/workflows/contrib-tests.yml

+25-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ jobs:
109109
pip install -e .
110110
pip uninstall -y openai
111111
- name: Test GPTAssistantAgent
112-
if: matrix.python-version != '3.10'
113112
run: |
114113
pytest test/agentchat/contrib/test_gpt_assistant.py
114+
115+
TeachableAgent:
116+
runs-on: ${{ matrix.os }}
117+
strategy:
118+
fail-fast: false
119+
matrix:
120+
os: [ubuntu-latest, macos-latest, windows-2019]
121+
python-version: ["3.8", "3.9", "3.10", "3.11"]
122+
steps:
123+
- uses: actions/checkout@v3
124+
- name: Set up Python ${{ matrix.python-version }}
125+
uses: actions/setup-python@v4
126+
with:
127+
python-version: ${{ matrix.python-version }}
128+
- name: Install packages and dependencies for all tests
129+
run: |
130+
python -m pip install --upgrade pip wheel
131+
pip install pytest
132+
- name: Install packages and dependencies for TeachableAgent
133+
run: |
134+
pip install -e .[teachable]
135+
pip uninstall -y openai
136+
- name: Test TeachableAgent
137+
run: |
138+
pytest test/agentchat/contrib/test_teachable_agent.py

notebook/agentchat_teachability.ipynb

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"\n",
2222
"In making decisions about memo storage and retrieval, `TeachableAgent` 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.\n",
2323
"\n",
24-
"This notebook demonstrates how `TeachableAgent` can learn facts, preferences, and skills from users. To chat with `TeachableAgent` yourself, run [chat_with_teachable_agent.py](../test/agentchat/chat_with_teachable_agent.py).\n",
24+
"This notebook demonstrates how `TeachableAgent` can learn facts, preferences, and skills from users. To chat with `TeachableAgent` yourself, run [chat_with_teachable_agent.py](../test/agentchat/contrib/chat_with_teachable_agent.py).\n",
2525
"\n",
2626
"## Requirements\n",
2727
"\n",
@@ -38,7 +38,7 @@
3838
"outputs": [],
3939
"source": [
4040
"%%capture --no-stderr\n",
41-
"# %pip install \"pyautogen[teachable]"
41+
"# %pip install \"pyautogen[teachable]\""
4242
]
4343
},
4444
{
@@ -142,9 +142,9 @@
142142
"from autogen import UserProxyAgent\n",
143143
"\n",
144144
"llm_config = {\n",
145-
" \"timeout\": 60,\n",
146145
" \"config_list\": config_list,\n",
147-
" \"use_cache\": True, # Use False to explore LLM non-determinism.\n",
146+
" \"timeout\": 60,\n",
147+
" \"cache_seed\": None, # Use an int to seed the response cache. Use None to disable caching.\n",
148148
"}\n",
149149
"\n",
150150
"teach_config={\n",
@@ -157,6 +157,7 @@
157157
"try:\n",
158158
" from termcolor import colored\n",
159159
"except ImportError:\n",
160+
"\n",
160161
" def colored(x, *args, **kwargs):\n",
161162
" return x\n",
162163
" \n",
@@ -170,8 +171,7 @@
170171
" human_input_mode=\"NEVER\",\n",
171172
" is_termination_msg=lambda x: True if \"TERMINATE\" in x.get(\"content\") else False,\n",
172173
" max_consecutive_auto_reply=0,\n",
173-
")\n",
174-
"\n"
174+
")"
175175
]
176176
},
177177
{
@@ -781,7 +781,7 @@
781781
"name": "python",
782782
"nbconvert_exporter": "python",
783783
"pygments_lexer": "ipython3",
784-
"version": "3.8.17"
784+
"version": "3.10.12"
785785
}
786786
},
787787
"nbformat": 4,

test/agentchat/chat_with_teachable_agent.py renamed to test/agentchat/contrib/chat_with_teachable_agent.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from autogen import UserProxyAgent, config_list_from_json
22
from autogen.agentchat.contrib.teachable_agent import TeachableAgent
33

4+
import os
5+
import sys
6+
7+
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
8+
from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402
9+
410

511
try:
612
from termcolor import colored
@@ -12,21 +18,24 @@ def colored(x, *args, **kwargs):
1218

1319
verbosity = 0 # 0 for basic info, 1 to add memory operations, 2 for analyzer messages, 3 for memo lists.
1420
recall_threshold = 1.5 # Higher numbers allow more (but less relevant) memos to be recalled.
15-
use_cache = False # If True, cached LLM calls will be skipped and responses pulled from cache. False exposes LLM non-determinism.
21+
cache_seed = None # Use an int to seed the response cache. Use None to disable caching.
1622

1723
# Specify the model to use. GPT-3.5 is less reliable than GPT-4 at learning from user input.
24+
# filter_dict = {"model": ["gpt-4-0613"]}
25+
# filter_dict = {"model": ["gpt-3.5-turbo-0613"]}
1826
filter_dict = {"model": ["gpt-4"]}
27+
# filter_dict = {"model": ["gpt-35-turbo-16k", "gpt-3.5-turbo-16k"]}
1928

2029

2130
def create_teachable_agent(reset_db=False):
2231
"""Instantiates a TeachableAgent using the settings from the top of this file."""
2332
# Load LLM inference endpoints from an env variable or a file
2433
# See https://microsoft.github.io/autogen/docs/FAQ#set-your-api-endpoints
2534
# and OAI_CONFIG_LIST_sample
26-
config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST", filter_dict=filter_dict)
35+
config_list = config_list_from_json(env_or_file=OAI_CONFIG_LIST, filter_dict=filter_dict, file_location=KEY_LOC)
2736
teachable_agent = TeachableAgent(
2837
name="teachableagent",
29-
llm_config={"config_list": config_list, "timeout": 120, "use_cache": use_cache},
38+
llm_config={"config_list": config_list, "timeout": 120, "cache_seed": cache_seed},
3039
teach_config={
3140
"verbosity": verbosity,
3241
"reset_db": reset_db,

test/agentchat/test_teachable_agent.py renamed to test/agentchat/contrib/test_teachable_agent.py

+56-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import pytest
2+
import os
3+
import sys
4+
from autogen import ConversableAgent, config_list_from_json
5+
6+
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
7+
from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402
8+
19
try:
210
from openai import OpenAI
311
from autogen.agentchat.contrib.teachable_agent import TeachableAgent
@@ -6,11 +14,6 @@
614
else:
715
skip = False
816

9-
import pytest
10-
import sys
11-
from autogen import ConversableAgent, config_list_from_json
12-
from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC
13-
1417
try:
1518
from termcolor import colored
1619
except ImportError:
@@ -25,8 +28,7 @@ def colored(x, *args, **kwargs):
2528

2629
assert_on_error = False # GPT-4 nearly always succeeds on these unit tests, but GPT-3.5 is a bit less reliable.
2730
recall_threshold = 1.5 # Higher numbers allow more (but less relevant) memos to be recalled.
28-
cache_seed = None
29-
# If int, cached LLM calls will be skipped and responses pulled from cache. None exposes LLM non-determinism.
31+
cache_seed = None # Use an int to seed the response cache. Use None to disable caching.
3032

3133
# Specify the model to use by uncommenting one of the following lines.
3234
# filter_dict={"model": ["gpt-4-0613"]}
@@ -139,10 +141,10 @@ def use_task_advice_pair_phrasing():
139141

140142

141143
@pytest.mark.skipif(
142-
skip or not sys.version.startswith("3.11"),
143-
reason="do not run if dependency is not installed or py!=3.11",
144+
skip,
145+
reason="do not run if dependency is not installed",
144146
)
145-
def test_all():
147+
def test_teachability_code_paths():
146148
"""Runs this file's unit tests."""
147149
total_num_errors, total_num_tests = 0, 0
148150

@@ -169,6 +171,49 @@ def test_all():
169171
)
170172

171173

174+
@pytest.mark.skipif(
175+
skip,
176+
reason="do not run if dependency is not installed",
177+
)
178+
def test_teachability_accuracy():
179+
"""A very cheap and fast test of teachability accuracy."""
180+
print(colored("\nTEST TEACHABILITY ACCURACY", "light_cyan"))
181+
182+
num_trials = 10 # The expected probability of failure is about 0.3 on each trial.
183+
for trial in range(num_trials):
184+
teachable_agent = create_teachable_agent(
185+
reset_db=True, verbosity=0
186+
) # For a clean test, clear the agent's memory.
187+
user = ConversableAgent("user", max_consecutive_auto_reply=0, llm_config=False, human_input_mode="NEVER")
188+
189+
# Prepopulate memory with a few arbitrary memos, just to make retrieval less trivial.
190+
teachable_agent.prepopulate_db()
191+
192+
# Tell the teachable agent something it wouldn't already know.
193+
user.initiate_chat(recipient=teachable_agent, message="My favorite color is teal.")
194+
195+
# Let the teachable agent remember things that should be learned from this chat.
196+
teachable_agent.learn_from_user_feedback()
197+
198+
# Now start a new chat to clear the context, and ask the teachable agent about the new information.
199+
print(colored("\nSTARTING A NEW CHAT WITH EMPTY CONTEXT", "light_cyan"))
200+
user.initiate_chat(recipient=teachable_agent, message="What's my favorite color?")
201+
num_errors = check_agent_response(teachable_agent, user, "teal")
202+
203+
print(colored(f"\nTRIAL {trial + 1} OF {num_trials} FINISHED", "light_cyan"))
204+
205+
# Wrap up.
206+
teachable_agent.close_db()
207+
208+
# Exit on the first success.
209+
if num_errors == 0:
210+
return
211+
212+
# All trials failed.
213+
assert False, "test_teachability_accuracy() failed on all {} trials.".format(num_trials)
214+
215+
172216
if __name__ == "__main__":
173217
"""Runs this file's unit tests from the command line."""
174-
test_all()
218+
test_teachability_code_paths()
219+
test_teachability_accuracy()

website/blog/2023-10-26-TeachableAgent/index.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ In order to make effective decisions about memo storage and retrieval, `Teachabl
2323

2424
AutoGen contains three code examples that use `TeachableAgent`.
2525

26-
1. Run [chat_with_teachable_agent.py](https://github.com/microsoft/autogen/blob/main/test/agentchat/chat_with_teachable_agent.py) to converse with `TeachableAgent`.
26+
1. Run [chat_with_teachable_agent.py](https://github.com/microsoft/autogen/blob/main/test/agentchat/contrib/chat_with_teachable_agent.py) to converse with `TeachableAgent`.
2727

2828
2. Use the Jupyter notebook [agentchat_teachability.ipynb](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_teachability.ipynb) to step through examples discussed below.
2929

30-
3. Run [test_teachable_agent.py](https://github.com/microsoft/autogen/blob/main/test/agentchat/test_teachable_agent.py) for quick unit testing of `TeachableAgent`.
30+
3. Run [test_teachable_agent.py](https://github.com/microsoft/autogen/blob/main/test/agentchat/contrib/test_teachable_agent.py) for quick unit testing of `TeachableAgent`.
3131

3232

3333
## Basic Usage of TeachableAgent

website/docs/Installation.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ openai v1 is a total rewrite of the library with many breaking changes. For exam
5555
Therefore, some changes are required for users of `pyautogen<0.2`.
5656

5757
- `api_base` -> `base_url`, `request_timeout` -> `timeout` in `llm_config` and `config_list`. `max_retry_period` and `retry_wait_time` are deprecated. `max_retries` can be set for each client.
58-
- MathChat, TeachableAgent are unsupported until they are tested in future release.
58+
- MathChat is unsupported until it is tested in future release.
5959
- `autogen.Completion` and `autogen.ChatCompletion` are deprecated. The essential functionalities are moved to `autogen.OpenAIWrapper`:
6060
```python
6161
from autogen import OpenAIWrapper
@@ -118,6 +118,17 @@ Example notebooks:
118118
[Automated Code Generation and Question Answering with Qdrant based Retrieval Augmented Agents](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_qdrant_RetrieveChat.ipynb)
119119

120120

121+
- #### TeachableAgent
122+
123+
To use TeachableAgent, please install AutoGen with the [teachable] option.
124+
```bash
125+
pip install "pyautogen[teachable]"
126+
```
127+
128+
Example notebook: [Chatting with TeachableAgent](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_teachability.ipynb)
129+
130+
131+
121132
- #### Large Multimodal Model (LMM) Agents
122133

123134
We offered Multimodal Conversable Agent and LLaVA Agent. Please install with the [lmm] option to use it.

0 commit comments

Comments
 (0)