-
Notifications
You must be signed in to change notification settings - Fork 8.2k
TeachableAgent #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TeachableAgent #278
Changes from all commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
93ee755
Initial commit.
rickyloynd-microsoft 4d2ccf6
Disable LLM response caching.
rickyloynd-microsoft 1c61c73
Merge branch 'main' into ricky
rickyloynd-microsoft fa033c0
Add teachability option to setup.py
rickyloynd-microsoft 165d5ce
Modify test to use OAI_CONFIG_LIST as suggested in the docs.
rickyloynd-microsoft 295f9e0
Expand unit test.
rickyloynd-microsoft 515f2d9
Complete unit test.
rickyloynd-microsoft 07cc364
Merge branch 'main' into ricky
rickyloynd-microsoft 3443e57
Add filter_dict
rickyloynd-microsoft dfe47a5
details
rickyloynd-microsoft c61d69c
AnalysisAgent
rickyloynd-microsoft 748ffcd
details
rickyloynd-microsoft 1e6e889
Merge branch 'main' into ricky
rickyloynd-microsoft 25faf14
More documentation and debug output.
rickyloynd-microsoft 66f4c61
Support retrieval of any number of relevant memos, including zero.
rickyloynd-microsoft e796832
More robust analysis separator.
rickyloynd-microsoft b31141f
cleanup
rickyloynd-microsoft 18387a7
teach_config
rickyloynd-microsoft 0f49f0d
refactoring
rickyloynd-microsoft 09fde25
For robustness, allow more flexibility on memo storage and retrieval.
rickyloynd-microsoft adac582
de-dupe the retrieved memos.
rickyloynd-microsoft 94d8ba0
Simplify AnalysisAgent. The unit tests now pass with gpt-3.5
rickyloynd-microsoft 30503bf
comments
rickyloynd-microsoft 4d2cedd
Add a verbosity level to control analyzer messages.
rickyloynd-microsoft 8a15d0a
refactoring
rickyloynd-microsoft 4806eef
comments
rickyloynd-microsoft 86e9e04
Persist memory on disk.
rickyloynd-microsoft 60bd4c2
cleanup
rickyloynd-microsoft 5f0075b
Merge branch 'main' into ricky
rickyloynd-microsoft b0ee207
Use markdown to format retrieved memos.
rickyloynd-microsoft 260962d
Use markdown in TextAnalyzerAgent
rickyloynd-microsoft 0da73a3
Add another verbosity level.
rickyloynd-microsoft 15bd56b
clean up logging
rickyloynd-microsoft cd4cd8f
notebook
rickyloynd-microsoft ec49ae0
minor edits
rickyloynd-microsoft d16c368
cleanup
rickyloynd-microsoft 0b24af1
Merge branch 'main' into ricky
rickyloynd-microsoft 2467673
linter fixes
rickyloynd-microsoft bfc0bc0
Skip tests that fail to import openai
rickyloynd-microsoft d9c3917
Merge branch 'main' into ricky
thinkall 0974280
Merge branch 'main' into ricky
thinkall 4d90351
Address reviewer feedback.
rickyloynd-microsoft 08c0777
Merge branch 'ricky' of https://github.com/microsoft/autogen into ricky
rickyloynd-microsoft 88d4f95
lint
rickyloynd-microsoft f1248ed
refactoring
rickyloynd-microsoft 29abbba
Merge branch 'main' into ricky
rickyloynd-microsoft ea14315
Improve wording
rickyloynd-microsoft 2f99801
Merge branch 'main' into ricky
rickyloynd-microsoft aef35be
Improve code coverage.
rickyloynd-microsoft a6eda5d
Merge branch 'main' into ricky
rickyloynd-microsoft a47e504
lint
rickyloynd-microsoft a756179
Use llm_config to control caching.
rickyloynd-microsoft 96131a8
lowercase notebook name
rickyloynd-microsoft 08d2000
Sort out the parameters passed through to ConversableAgent, and suppl…
rickyloynd-microsoft 3183c27
lint
rickyloynd-microsoft da25199
Merge branch 'main' into ricky
rickyloynd-microsoft c033af8
Allow TextAnalyzerAgent to be given a different llm_config than Teach…
rickyloynd-microsoft 92dc0f6
documentation
rickyloynd-microsoft 848f969
Merge branch 'ricky' of https://github.com/microsoft/autogen into ricky
rickyloynd-microsoft e13b7cb
Merge branch 'main' into ricky
rickyloynd-microsoft 859acc5
Modifications to run openai workflow.
rickyloynd-microsoft e13d512
Merge branch 'main' into ricky
sonichi 2745276
Test on just python 3.10.
rickyloynd-microsoft de17a4e
Merge branch 'ricky' of https://github.com/microsoft/autogen into ricky
rickyloynd-microsoft ff4a369
Test on python 3.9 instead of 3.10.
rickyloynd-microsoft f962f45
Remove space from name -> teachableagent
rickyloynd-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,3 +164,6 @@ key_openai.txt | |
| key_aoai.txt | ||
| base_aoai.txt | ||
| wolfram.txt | ||
|
|
||
| # DB on disk for TeachableAgent | ||
| tmp/ | ||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| from autogen import oai | ||
| from autogen.agentchat.agent import Agent | ||
| from autogen.agentchat.assistant_agent import ConversableAgent | ||
| from typing import Callable, Dict, Optional, Union, List, Tuple, Any | ||
|
|
||
| system_message = """You are an expert in text analysis. | ||
| The user will give you TEXT to analyze. | ||
| The user will give you analysis INSTRUCTIONS copied twice, at both the beginning and the end. | ||
| You will follow these INSTRUCTIONS in analyzing the TEXT, then give the results of your expert analysis in the format requested.""" | ||
|
|
||
|
|
||
| class TextAnalyzerAgent(ConversableAgent): | ||
| """Text Analysis agent, a subclass of ConversableAgent designed to analyze text as instructed.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| name="analyzer", | ||
| system_message: Optional[str] = system_message, | ||
| human_input_mode: Optional[str] = "NEVER", | ||
| llm_config: Optional[Union[Dict, bool]] = None, | ||
| **kwargs, | ||
| ): | ||
| """ | ||
| Args: | ||
| name (str): name of the agent. | ||
| system_message (str): system message for the ChatCompletion inference. | ||
| human_input_mode (str): This agent should NEVER prompt the human for input. | ||
| llm_config (dict or False): llm inference configuration. | ||
| Please refer to [Completion.create](/docs/reference/oai/completion#create) | ||
| for available options. | ||
| To disable llm-based auto reply, set to False. | ||
| teach_config (dict or None): Additional parameters used by TeachableAgent. | ||
| To use default config, set to None. Otherwise, set to a dictionary with any of the following keys: | ||
| - verbosity (Optional, int): # 0 (default) for basic info, 1 to add memory operations, 2 for analyzer messages, 3 for memo lists. | ||
| - reset_db (Optional, bool): True to clear the DB before starting. Default False. | ||
| - path_to_db_dir (Optional, str): path to the directory where the DB is stored. Default "./tmp/teachable_agent_db" | ||
| - prepopulate (Optional, int): True (default) to prepopulate the DB with a set of input-output pairs. | ||
| - recall_threshold (Optional, float): The maximum distance for retrieved memos, where 0.0 is exact match. Default 1.5. Larger values allow more (but less relevant) memos to be recalled. | ||
| - max_num_retrievals (Optional, int): The maximum number of memos to retrieve from the DB. Default 10. | ||
| **kwargs (dict): other kwargs in [ConversableAgent](../conversable_agent#__init__). | ||
| """ | ||
| super().__init__( | ||
| name=name, | ||
| system_message=system_message, | ||
| human_input_mode=human_input_mode, | ||
| llm_config=llm_config, | ||
| **kwargs, | ||
| ) | ||
| self.register_reply(Agent, TextAnalyzerAgent._analyze_in_reply, 1) | ||
|
|
||
| def _analyze_in_reply( | ||
| self, | ||
| messages: Optional[List[Dict]] = None, | ||
| sender: Optional[Agent] = None, | ||
| config: Optional[Any] = None, | ||
| ) -> Tuple[bool, Union[str, Dict, None]]: | ||
| """Analyzes the given text as instructed, and returns the analysis as a message. | ||
| Assumes exactly two messages containing the text to analyze and the analysis instructions. | ||
| See TeachableAgent.analyze for an example of how to use this method.""" | ||
| if self.llm_config is False: | ||
| raise ValueError("TextAnalyzerAgent requires self.llm_config to be set in its base class.") | ||
| if messages is None: | ||
| messages = self._oai_messages[sender] # In case of a direct call. | ||
| assert len(messages) == 2 | ||
|
|
||
| # Delegate to the analysis method. | ||
| return True, self.analyze_text(messages[0]["content"], messages[1]["content"]) | ||
|
|
||
| def analyze_text(self, text_to_analyze, analysis_instructions): | ||
| """Analyzes the given text as instructed, and returns the analysis.""" | ||
| # Assemble the message. | ||
| text_to_analyze = "# TEXT\n" + text_to_analyze + "\n" | ||
| analysis_instructions = "# INSTRUCTIONS\n" + analysis_instructions + "\n" | ||
| msg_text = "\n".join( | ||
| [analysis_instructions, text_to_analyze, analysis_instructions] | ||
| ) # Repeat the instructions. | ||
| messages = self._oai_system_message + [{"role": "user", "content": msg_text}] | ||
|
|
||
| # Generate and return the analysis string. | ||
| response = oai.ChatCompletion.create(context=None, messages=messages, **self.llm_config) | ||
| output_text = oai.ChatCompletion.extract_text_or_function_call(response)[0] | ||
| return output_text | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.