Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyun-wu authored Oct 21, 2023
2 parents 61509b8 + df48825 commit 3e25013
Show file tree
Hide file tree
Showing 15 changed files with 1,585 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/openai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ jobs:
if: matrix.python-version == '3.9'
run: |
pip install -e .[retrievechat]
- name: Install packages for Teachable when needed
run: |
pip install -e .[teachable]
- name: Coverage
if: matrix.python-version == '3.9'
env:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,6 @@ key_openai.txt
key_aoai.txt
base_aoai.txt
wolfram.txt

# DB on disk for TeachableAgent
tmp/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![PyPI version](https://badge.fury.io/py/pyautogen.svg)](https://badge.fury.io/py/pyautogen)
[![Build](https://github.com/microsoft/autogen/actions/workflows/python-package.yml/badge.svg)](https://github.com/microsoft/autogen/actions/workflows/python-package.yml)
![Python Version](https://img.shields.io/badge/3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-blue)
[![Downloads](https://static.pepy.tech/badge/pyautogen/week)](https://pepy.tech/project/pyautogen)
[![](https://img.shields.io/discord/1153072414184452236?logo=discord&style=flat)](https://discord.gg/pAbnFJrkgZ)

This project is a spinoff from [FLAML](https://github.com/microsoft/FLAML).
Expand Down
425 changes: 425 additions & 0 deletions autogen/agentchat/contrib/teachable_agent.py

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions autogen/agentchat/contrib/text_analyzer_agent.py
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
2 changes: 1 addition & 1 deletion autogen/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.12"
__version__ = "0.1.13"
2 changes: 1 addition & 1 deletion notebook/agentchat_langchain.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
"id": "11cc4e60",
"metadata": {},
"source": [
"# A PySpark Examle"
"# A PySpark Example"
]
},
{
Expand Down
Loading

0 comments on commit 3e25013

Please sign in to comment.