Skip to content

Commit

Permalink
add long context handling notebook (microsoft#1618)
Browse files Browse the repository at this point in the history
* add long context handling notebook

* remove compression notebook
  • Loading branch information
sonichi authored Feb 11, 2024
1 parent 8089058 commit e83de5d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 33 deletions.
20 changes: 10 additions & 10 deletions autogen/coding/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@


class CodeBlock(BaseModel):
"""A class that represents a code block."""
"""(Experimental) A class that represents a code block."""

code: str = Field(description="The code to execute.")

language: str = Field(description="The language of the code.")


class CodeResult(BaseModel):
"""A class that represents the result of a code execution."""
"""(Experimental) A class that represents the result of a code execution."""

exit_code: int = Field(description="The exit code of the code execution.")

output: str = Field(description="The output of the code execution.")


class CodeExtractor(Protocol):
"""A code extractor class that extracts code blocks from a message."""
"""(Experimental) A code extractor class that extracts code blocks from a message."""

def extract_code_blocks(self, message: Union[str, List[Dict[str, Any]], None]) -> List[CodeBlock]:
"""Extract code blocks from a message.
"""(Experimental) Extract code blocks from a message.
Args:
message (str): The message to extract code blocks from.
Expand All @@ -40,17 +40,17 @@ def extract_code_blocks(self, message: Union[str, List[Dict[str, Any]], None]) -

@runtime_checkable
class CodeExecutor(Protocol):
"""A code executor class that executes code blocks and returns the result."""
"""(Experimental) A code executor class that executes code blocks and returns the result."""

class UserCapability(Protocol):
"""An AgentCapability class that gives agent ability use this code executor."""
"""(Experimental) An AgentCapability class that gives agent ability use this code executor."""

def add_to_agent(self, agent: LLMAgent) -> None:
... # pragma: no cover

@property
def user_capability(self) -> "CodeExecutor.UserCapability":
"""Capability to use this code executor.
"""(Experimental) Capability to use this code executor.
The exported capability can be added to an agent to allow it to use this
code executor:
Expand All @@ -68,11 +68,11 @@ def user_capability(self) -> "CodeExecutor.UserCapability":

@property
def code_extractor(self) -> CodeExtractor:
"""The code extractor used by this code executor."""
"""(Experimental) The code extractor used by this code executor."""
... # pragma: no cover

def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CodeResult:
"""Execute code blocks and return the result.
"""(Experimental) Execute code blocks and return the result.
This method should be implemented by the code executor.
Expand All @@ -85,7 +85,7 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CodeResult:
... # pragma: no cover

def restart(self) -> None:
"""Restart the code executor.
"""(Experimental) Restart the code executor.
This method should be implemented by the code executor.
Expand Down
14 changes: 7 additions & 7 deletions autogen/coding/embedded_ipython_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class IPythonCodeResult(CodeResult):
"""A code result class for IPython code executor."""
"""(Experimental) A code result class for IPython code executor."""

output_files: List[str] = Field(
default_factory=list,
Expand All @@ -27,7 +27,7 @@ class IPythonCodeResult(CodeResult):


class EmbeddedIPythonCodeExecutor(BaseModel):
"""A code executor class that executes code statefully using an embedded
"""(Experimental) A code executor class that executes code statefully using an embedded
IPython kernel managed by this class.
**This will execute LLM generated code on the local machine.**
Expand Down Expand Up @@ -85,7 +85,7 @@ class EmbeddedIPythonCodeExecutor(BaseModel):
)

class UserCapability:
"""An AgentCapability class that gives agent ability use a stateful
"""(Experimental) An AgentCapability class that gives agent ability use a stateful
IPython code executor. This capability can be added to an agent using
the `add_to_agent` method which append a system message update to the
agent's system message."""
Expand Down Expand Up @@ -129,17 +129,17 @@ def __init__(self, **kwargs: Any):

@property
def user_capability(self) -> "EmbeddedIPythonCodeExecutor.UserCapability":
"""Export a user capability for this executor that can be added to
"""(Experimental) Export a user capability for this executor that can be added to
an agent using the `add_to_agent` method."""
return EmbeddedIPythonCodeExecutor.UserCapability(self.system_message_update)

@property
def code_extractor(self) -> CodeExtractor:
"""Export a code extractor that can be used by an agent."""
"""(Experimental) Export a code extractor that can be used by an agent."""
return MarkdownCodeExtractor()

def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> IPythonCodeResult:
"""Execute a list of code blocks and return the result.
"""(Experimental) Execute a list of code blocks and return the result.
This method executes a list of code blocks as cells in an IPython kernel
managed by this class.
Expand Down Expand Up @@ -204,7 +204,7 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> IPythonCodeResult
)

def restart(self) -> None:
"""Restart a new session."""
"""(Experimental) Restart a new session."""
self._kernel_client.stop_channels()
self._kernel_manager.shutdown_kernel()
self._kernel_manager = KernelManager(kernel_name=self.kernel_name)
Expand Down
4 changes: 2 additions & 2 deletions autogen/coding/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


class CodeExecutorFactory:
"""A factory class for creating code executors."""
"""(Experimental) A factory class for creating code executors."""

@staticmethod
def create(code_execution_config: Dict[str, Any]) -> CodeExecutor:
"""Get a code executor based on the code execution config.
"""(Experimental) Get a code executor based on the code execution config.
Args:
code_execution_config (Dict): The code execution config,
Expand Down
10 changes: 5 additions & 5 deletions autogen/coding/local_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def colored(x: Any, *args: Any, **kwargs: Any) -> str: # type: ignore[misc]


class CommandlineCodeResult(CodeResult):
"""A code result class for command line code executor."""
"""(Experimental) A code result class for command line code executor."""

code_file: Optional[str] = Field(
default=None,
Expand All @@ -34,7 +34,7 @@ class CommandlineCodeResult(CodeResult):


class LocalCommandlineCodeExecutor(BaseModel):
"""A code executor class that executes code through a local command line
"""(Experimental) A code executor class that executes code through a local command line
environment.
**This will execute LLM generated code on the local machine.**
Expand Down Expand Up @@ -105,11 +105,11 @@ def user_capability(self) -> "LocalCommandlineCodeExecutor.UserCapability":

@property
def code_extractor(self) -> CodeExtractor:
"""Export a code extractor that can be used by an agent."""
"""(Experimental) Export a code extractor that can be used by an agent."""
return MarkdownCodeExtractor()

def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandlineCodeResult:
"""Execute the code blocks and return the result.
"""(Experimental) Execute the code blocks and return the result.
Args:
code_blocks (List[CodeBlock]): The code blocks to execute.
Expand Down Expand Up @@ -158,5 +158,5 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandlineCodeRe
return CommandlineCodeResult(exit_code=exitcode, output=logs_all, code_file=code_filename)

def restart(self) -> None:
"""Restart the code executor."""
"""(Experimental) Restart the code executor."""
warnings.warn("Restarting local command line code executor is not supported. No action is taken.")
4 changes: 2 additions & 2 deletions autogen/coding/markdown_code_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


class MarkdownCodeExtractor:
"""A class that extracts code blocks from a message using Markdown syntax."""
"""(Experimental) A class that extracts code blocks from a message using Markdown syntax."""

def extract_code_blocks(self, message: Union[str, List[Dict[str, Any]], None]) -> List[CodeBlock]:
"""Extract code blocks from a message. If no code blocks are found,
"""(Experimental) Extract code blocks from a message. If no code blocks are found,
return an empty list.
Args:
Expand Down
6 changes: 1 addition & 5 deletions autogen/oai/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import os
import sys
from typing import Any, List, Optional, Dict, Callable, Tuple, Union
import logging
Expand All @@ -11,11 +10,8 @@
from typing import Protocol

from autogen.cache.cache import Cache
from autogen.oai import completion

from autogen.oai.openai_utils import DEFAULT_AZURE_API_VERSION, get_key, OAI_PRICE1K
from autogen.oai.openai_utils import get_key, OAI_PRICE1K
from autogen.token_count_utils import count_token
from autogen._pydantic import model_dump

TOOL_ENABLED = False
try:
Expand Down
2 changes: 1 addition & 1 deletion autogen/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.12"
__version__ = "0.2.13"
3 changes: 2 additions & 1 deletion website/docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Links to notebook examples:

1. **Long Context Handling**

- Conversations with Chat History Compression Enabled - [View Notebook](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_compression.ipynb)
<!-- - Conversations with Chat History Compression Enabled - [View Notebook](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_compression.ipynb) -->
- Long Context Handling as A Capability - [View Notebook](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_capability_long_context_handling.ipynb)

1. **Evaluation and Assessment**

Expand Down

0 comments on commit e83de5d

Please sign in to comment.