Skip to content

Commit 16f4eaa

Browse files
authored
add doc about effects for capabilities (microsoft#1842)
* add doc about effects for capabilities * remove unnecessary imports * improve doc * test * test location * polish * improve import of colored * termcolor * termcolor * use pull request for openai test
1 parent ff0dbb5 commit 16f4eaa

16 files changed

+50
-119
lines changed

.github/workflows/contrib-openai.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
name: OpenAI4ContribTests
55

66
on:
7-
pull_request_target:
7+
pull_request:
88
branches: ['main']
99
paths:
1010
- 'autogen/**'
@@ -173,7 +173,7 @@ jobs:
173173
AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }}
174174
OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }}
175175
run: |
176-
coverage run -a -m pytest test/agentchat/contrib/test_teachable_agent.py
176+
coverage run -a -m pytest test/agentchat/contrib/capabilities/test_teachable_agent.py
177177
coverage xml
178178
- name: Upload coverage to Codecov
179179
uses: codecov/codecov-action@v3

.github/workflows/contrib-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172
- name: Coverage
173173
run: |
174174
pip install coverage>=5.3
175-
coverage run -a -m pytest test/agentchat/contrib/test_teachable_agent.py --skip-openai
175+
coverage run -a -m pytest test/agentchat/contrib/capabilities/test_teachable_agent.py --skip-openai
176176
coverage xml
177177
- name: Upload coverage to Codecov
178178
uses: codecov/codecov-action@v3

.github/workflows/deploy-website.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- name: pydoc-markdown install
3838
run: |
3939
python -m pip install --upgrade pip
40-
pip install pydoc-markdown pyyaml colored
40+
pip install pydoc-markdown pyyaml termcolor
4141
- name: pydoc-markdown run
4242
run: |
4343
pydoc-markdown
@@ -83,7 +83,7 @@ jobs:
8383
- name: pydoc-markdown install
8484
run: |
8585
python -m pip install --upgrade pip
86-
pip install pydoc-markdown pyyaml colored
86+
pip install pydoc-markdown pyyaml termcolor
8787
- name: pydoc-markdown run
8888
run: |
8989
pydoc-markdown

.github/workflows/openai.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
name: OpenAI
55

66
on:
7-
pull_request_target:
7+
pull_request:
88
branches: ["main"]
99
paths:
1010
- "autogen/**"

autogen/agentchat/chat.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
from collections import defaultdict
44
from typing import Dict, List, Any, Set, Tuple
55
from dataclasses import dataclass
6-
from .utils import consolidate_chat_info
76
import warnings
8-
9-
try:
10-
from termcolor import colored
11-
except ImportError:
12-
13-
def colored(x, *args, **kwargs):
14-
return x
7+
from termcolor import colored
8+
from .utils import consolidate_chat_info
159

1610

1711
logger = logging.getLogger(__name__)

autogen/agentchat/contrib/capabilities/context_handling.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ class TransformChatHistory:
2525
2. Second, it limits the number of message to keep
2626
3. Third, it limits the total number of tokens in the chat history
2727
28-
Args:
29-
max_tokens_per_message (Optional[int]): Maximum number of tokens to keep in each message.
30-
max_messages (Optional[int]): Maximum number of messages to keep in the context.
31-
max_tokens (Optional[int]): Maximum number of tokens to keep in the context.
28+
When adding this capability to an agent, the following are modified:
29+
- A hook is added to the hookable method `process_all_messages_before_reply` to transform the received messages for possible truncation.
30+
Not modifying the stored message history.
3231
"""
3332

3433
def __init__(
@@ -38,6 +37,12 @@ def __init__(
3837
max_messages: Optional[int] = None,
3938
max_tokens: Optional[int] = None,
4039
):
40+
"""
41+
Args:
42+
max_tokens_per_message (Optional[int]): Maximum number of tokens to keep in each message.
43+
max_messages (Optional[int]): Maximum number of messages to keep in the context.
44+
max_tokens (Optional[int]): Maximum number of tokens to keep in the context.
45+
"""
4146
self.max_tokens_per_message = max_tokens_per_message if max_tokens_per_message else sys.maxsize
4247
self.max_messages = max_messages if max_messages else sys.maxsize
4348
self.max_tokens = max_tokens if max_tokens else sys.maxsize

autogen/agentchat/contrib/capabilities/teachability.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import os
2-
from autogen.agentchat.assistant_agent import ConversableAgent
3-
from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability
4-
from autogen.agentchat.contrib.text_analyzer_agent import TextAnalyzerAgent
5-
from typing import Dict, Optional, Union, List, Tuple, Any
2+
from typing import Dict, Optional, Union
63
import chromadb
74
from chromadb.config import Settings
85
import pickle
9-
10-
try:
11-
from termcolor import colored
12-
except ImportError:
13-
14-
def colored(x, *args, **kwargs):
15-
return x
6+
from autogen.agentchat.assistant_agent import ConversableAgent
7+
from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability
8+
from autogen.agentchat.contrib.text_analyzer_agent import TextAnalyzerAgent
9+
from autogen.agentchat.conversable_agent import colored
1610

1711

1812
class Teachability(AgentCapability):
@@ -23,6 +17,13 @@ class Teachability(AgentCapability):
2317
To make any conversable agent teachable, instantiate both the agent and the Teachability class,
2418
then pass the agent to teachability.add_to_agent(agent).
2519
Note that teachable agents in a group chat must be given unique path_to_db_dir values.
20+
21+
When adding Teachability to an agent, the following are modified:
22+
- The agent's system message is appended with a note about the agent's new ability.
23+
- A hook is added to the agent's `process_last_received_message` hookable method,
24+
and the hook potentially modifies the last of the received messages to include earlier teachings related to the message.
25+
Added teachings do not propagate into the stored message history.
26+
If new user teachings are detected, they are added to new memos in the vector database.
2627
"""
2728

2829
def __init__(

autogen/agentchat/contrib/llava_agent.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
import json
22
import logging
3-
import os
4-
import pdb
5-
import re
6-
from typing import Any, Dict, List, Optional, Tuple, Union
7-
3+
from typing import List, Optional, Tuple
84
import replicate
95
import requests
10-
from regex import R
116

127
from autogen.agentchat.agent import Agent
138
from autogen.agentchat.contrib.img_utils import get_image_data, llava_formatter
149
from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent
1510
from autogen.code_utils import content_str
16-
17-
try:
18-
from termcolor import colored
19-
except ImportError:
20-
21-
def colored(x, *args, **kwargs):
22-
return x
11+
from autogen.agentchat.conversable_agent import colored
2312

2413

2514
logger = logging.getLogger(__name__)

autogen/agentchat/contrib/multimodal_conversable_agent.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
import copy
2-
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
2+
from typing import Dict, List, Optional, Tuple, Union
33

44
from autogen import OpenAIWrapper
55
from autogen.agentchat import Agent, ConversableAgent
66
from autogen.agentchat.contrib.img_utils import (
7-
convert_base64_to_data_uri,
87
gpt4v_formatter,
98
message_formatter_pil_to_b64,
10-
pil_to_data_uri,
119
)
10+
from autogen.code_utils import content_str
1211

1312
from ..._pydantic import model_dump
1413

15-
try:
16-
from termcolor import colored
17-
except ImportError:
18-
19-
def colored(x, *args, **kwargs):
20-
return x
21-
22-
23-
from autogen.code_utils import content_str
2414

2515
DEFAULT_LMM_SYS_MSG = """You are a helpful AI assistant."""
2616
DEFAULT_MODEL = "gpt-4-vision-preview"

autogen/agentchat/contrib/retrieve_user_proxy_agent.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import re
2+
from typing import Callable, Dict, Optional, Union, List, Tuple, Any
3+
from IPython import get_ipython
24

35
try:
46
import chromadb
@@ -10,16 +12,7 @@
1012
from autogen.token_count_utils import count_token
1113
from autogen.code_utils import extract_code
1214
from autogen import logger
13-
14-
from typing import Callable, Dict, Optional, Union, List, Tuple, Any
15-
from IPython import get_ipython
16-
17-
try:
18-
from termcolor import colored
19-
except ImportError:
20-
21-
def colored(x, *args, **kwargs):
22-
return x
15+
from autogen.agentchat.conversable_agent import colored
2316

2417

2518
PROMPT_DEFAULT = """You're a retrieve augmented chatbot. You answer user's questions based on your own knowledge and the

autogen/coding/local_commandline_code_executor.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33
import uuid
44
import warnings
55
from typing import Any, ClassVar, List, Optional
6-
76
from pydantic import BaseModel, Field, field_validator
87

8+
from autogen.agentchat.conversable_agent import colored
99
from ..agentchat.agent import LLMAgent
1010
from ..code_utils import execute_code
1111
from .base import CodeBlock, CodeExtractor, CodeResult
1212
from .markdown_code_extractor import MarkdownCodeExtractor
1313

14-
try:
15-
from termcolor import colored
16-
except ImportError:
17-
18-
def colored(x: Any, *args: Any, **kwargs: Any) -> str: # type: ignore[misc]
19-
return x # type: ignore[no-any-return]
20-
2114

2215
__all__ = (
2316
"LocalCommandlineCodeExecutor",

notebook/agentchat_teachability.ipynb

-8
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,6 @@
161161
"# Now add the Teachability capability to the agent.\n",
162162
"teachability.add_to_agent(teachable_agent)\n",
163163
"\n",
164-
"try:\n",
165-
" from termcolor import colored\n",
166-
"except ImportError:\n",
167-
"\n",
168-
" def colored(x, *args, **kwargs):\n",
169-
" return x\n",
170-
"\n",
171-
"\n",
172164
"# Instantiate a UserProxyAgent to represent the user. But in this notebook, all user input will be simulated.\n",
173165
"user = UserProxyAgent(\n",
174166
" name=\"user\",\n",

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

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
#!/usr/bin/env python3 -m pytest
22

3+
import os
4+
import sys
5+
from termcolor import colored
36
from autogen import UserProxyAgent, config_list_from_json
47
from autogen.agentchat.contrib.capabilities.teachability import Teachability
58
from autogen import ConversableAgent
69

7-
import os
8-
import sys
9-
10-
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
10+
sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))
1111
from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402
1212

1313

14-
try:
15-
from termcolor import colored
16-
except ImportError:
17-
18-
def colored(x, *args, **kwargs):
19-
return x
20-
21-
2214
# Specify the model to use. GPT-3.5 is less reliable than GPT-4 at learning from user input.
2315
filter_dict = {"model": ["gpt-4-0125-preview"]}
2416
# filter_dict = {"model": ["gpt-3.5-turbo-1106"]}

test/agentchat/contrib/capabilities/test_context_handling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST
1212

13-
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
13+
sys.path.append(os.path.join(os.path.dirname(__file__), "../../.."))
1414
from conftest import skip_openai # noqa: E402
1515

1616
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))

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

+6-13
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,21 @@
33
import pytest
44
import os
55
import sys
6+
from termcolor import colored
67
from autogen import ConversableAgent, config_list_from_json
78

8-
sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))
9+
sys.path.append(os.path.join(os.path.dirname(__file__), "../../.."))
910
from conftest import skip_openai # noqa: E402
1011

11-
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
12+
sys.path.append(os.path.join(os.path.dirname(__file__), "../.."))
1213
from test_assistant_agent import OAI_CONFIG_LIST, KEY_LOC # noqa: E402
1314

1415
try:
15-
from openai import OpenAI
1616
from autogen.agentchat.contrib.capabilities.teachability import Teachability
1717
except ImportError:
1818
skip = True
1919
else:
20-
skip = False or skip_openai
21-
22-
try:
23-
from termcolor import colored
24-
except ImportError:
25-
26-
def colored(x, *args, **kwargs):
27-
return x
20+
skip = skip_openai
2821

2922

3023
# Specify the model to use by uncommenting one of the following lines.
@@ -141,7 +134,7 @@ def use_task_advice_pair_phrasing():
141134

142135
@pytest.mark.skipif(
143136
skip,
144-
reason="do not run if dependency is not installed",
137+
reason="do not run if dependency is not installed or requested to skip",
145138
)
146139
def test_teachability_code_paths():
147140
"""Runs this file's unit tests."""
@@ -172,7 +165,7 @@ def test_teachability_code_paths():
172165

173166
@pytest.mark.skipif(
174167
skip,
175-
reason="do not run if dependency is not installed",
168+
reason="do not run if dependency is not installed or requested to skip",
176169
)
177170
def test_teachability_accuracy():
178171
"""A very cheap and fast test of teachability accuracy."""

website/process_notebooks.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@
1414
import typing
1515
import concurrent.futures
1616
import os
17-
18-
from typing import Any, Dict, Optional, Tuple, Union
17+
from typing import Dict, Optional, Tuple, Union
1918
from dataclasses import dataclass
20-
21-
2219
from multiprocessing import current_process
20+
from termcolor import colored
2321

2422
try:
2523
import yaml
2624
except ImportError:
2725
print("pyyaml not found.\n\nPlease install pyyaml:\n\tpip install pyyaml\n")
2826
sys.exit(1)
2927

30-
3128
try:
3229
import nbclient
3330
from nbclient.client import (
@@ -49,14 +46,6 @@
4946
print("test won't work without nbclient")
5047

5148

52-
try:
53-
from termcolor import colored
54-
except ImportError:
55-
56-
def colored(x, *args, **kwargs):
57-
return x
58-
59-
6049
class Result:
6150
def __init__(self, returncode: int, stdout: str, stderr: str):
6251
self.returncode = returncode

0 commit comments

Comments
 (0)