-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ShoggothAI/autogen-integration
AutoGen integration
- Loading branch information
Showing
11 changed files
with
1,243 additions
and
36 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
from .tool import MotleyTool | ||
from .llm_tool import LLMTool | ||
|
||
from .autogen_chat_tool import AutoGenChatTool | ||
from .image_generation import DallEImageGeneratorTool | ||
from .llm_tool import LLMTool | ||
from .mermaid_evaluator_tool import MermaidEvaluatorTool | ||
from .python_repl import PythonREPLTool |
This file contains 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,79 @@ | ||
from typing import Optional, Type, Callable, Any | ||
|
||
from langchain_core.tools import StructuredTool | ||
from langchain_core.prompts import PromptTemplate | ||
from langchain_core.prompts.base import BasePromptTemplate | ||
from langchain_core.pydantic_v1 import BaseModel, Field, create_model | ||
|
||
try: | ||
from autogen import ConversableAgent, ChatResult | ||
except ImportError: | ||
ConversableAgent = None | ||
ChatResult = None | ||
|
||
from motleycrew.tools import MotleyTool | ||
from motleycrew.common.utils import ensure_module_is_installed | ||
|
||
|
||
def get_last_message(chat_result: ChatResult) -> str: | ||
for message in reversed(chat_result.chat_history): | ||
if message.get("content") and "TERMINATE" not in message["content"]: | ||
return message["content"] | ||
|
||
|
||
class AutoGenChatTool(MotleyTool): | ||
def __init__( | ||
self, | ||
name: str, | ||
description: str, | ||
prompt: str | BasePromptTemplate, | ||
initiator: ConversableAgent, | ||
recipient: ConversableAgent, | ||
result_extractor: Callable[[ChatResult], Any] = get_last_message, | ||
input_schema: Optional[Type[BaseModel]] = None, | ||
): | ||
ensure_module_is_installed("autogen") | ||
langchain_tool = create_autogen_chat_tool( | ||
name=name, | ||
description=description, | ||
prompt=prompt, | ||
initiator=initiator, | ||
recipient=recipient, | ||
result_extractor=result_extractor, | ||
input_schema=input_schema, | ||
) | ||
super().__init__(langchain_tool) | ||
|
||
|
||
def create_autogen_chat_tool( | ||
name: str, | ||
description: str, | ||
prompt: str | BasePromptTemplate, | ||
initiator: ConversableAgent, | ||
recipient: ConversableAgent, | ||
result_extractor: Callable[[ChatResult], Any], | ||
input_schema: Optional[Type[BaseModel]] = None, | ||
): | ||
if not isinstance(prompt, BasePromptTemplate): | ||
prompt = PromptTemplate.from_template(prompt) | ||
|
||
if input_schema is None: | ||
fields = { | ||
var: (str, Field(description=f"Input {var} for the tool.")) | ||
for var in prompt.input_variables | ||
} | ||
|
||
# Create the AutoGenChatToolInput class dynamically | ||
input_schema = create_model("AutoGenChatToolInput", **fields) | ||
|
||
def run_autogen_chat(**kwargs) -> Any: | ||
message = prompt.format(**kwargs) | ||
chat_result = initiator.initiate_chat(recipient, message=message) | ||
return result_extractor(chat_result) | ||
|
||
return StructuredTool.from_function( | ||
func=run_autogen_chat, | ||
name=name, | ||
description=description, | ||
args_schema=input_schema, | ||
) |
This file contains 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 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
Oops, something went wrong.