Skip to content

Commit

Permalink
Merge pull request #15 from shoutsid/shoutsid/issue14
Browse files Browse the repository at this point in the history
Shoutsid/issue14
  • Loading branch information
shoutsid authored Oct 18, 2023
2 parents 47dcc29 + c3dafe7 commit c7a377f
Show file tree
Hide file tree
Showing 25 changed files with 87 additions and 107 deletions.
8 changes: 5 additions & 3 deletions agents/buddy.py → app/agents/buddy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
GPT3_5_TURBO_0613,
CONFIG_LIST,
)
from .user_agent import UserAgent
from app.agents.user_agent import UserAgent


class Buddy:
Expand Down Expand Up @@ -63,10 +63,12 @@ def start(self, message: str):
message=message, recipient=self.assistant, request_reply=True
)
else:
self.user_proxy.initiate_chat(recipient=self.assistant, message=message)
self.user_proxy.initiate_chat(
recipient=self.assistant, message=message)

# Store the assistant's reply to the conversation context
reply = self.user_proxy.last_message()["content"]
self.conversation_context.append({"role": "assistant", "content": reply})
self.conversation_context.append(
{"role": "assistant", "content": reply})

return reply
9 changes: 5 additions & 4 deletions agents/planner.py → app/agents/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
CONFIG_LIST,
)

from agents.services.planner_service import PlannerService
from agents.services.function_service import FunctionService
from agents.services.chat_service import ChatService
from app.services.planner_service import PlannerService
from app.services.function_service import FunctionService
from app.services.chat_service import ChatService

if __name__ == "__main__":
planner_service = PlannerService(CONFIG_LIST)
function_service = FunctionService()
chat_service = ChatService(CONFIG_LIST, function_service.registry.functions)
chat_service = ChatService(
CONFIG_LIST, function_service.registry.functions)

message = input("Enter a message to send to the assistant: ")
chat_service.initiate_chat(message)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 3 additions & 12 deletions agents/services/chat_service.py → app/services/chat_service.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
# agents/services/chat_service.py

import sys
import os

sys.path.insert(
0,
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")),
)

from app.agents.user_agent import UserAgent
from autogen import AssistantAgent
from agents.user_agent import UserAgent


class ChatService:
Expand All @@ -25,7 +15,8 @@ def __init__(self, config_list, function_map, assistant=None, user_proxy=None):
if assistant is None:
self.assistant = AssistantAgent(
name="assistant",
llm_config={"config_list": config_list, "functions": function_map},
llm_config={"config_list": config_list,
"functions": function_map},
)
else:
self.assistant = assistant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ast
from agents.helpers.function_registry import FunctionRegistry
from app.helpers.function_registry import FunctionRegistry


class FunctionService:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import sys
import os

sys.path.insert(
0,
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")),
)

from autogen import AssistantAgent, UserProxyAgent


Expand Down
9 changes: 5 additions & 4 deletions streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import streamlit as st
import sqlite3
import pandas as pd
from agents.buddy import Buddy
from app.agents.buddy import Buddy


def main():
Expand All @@ -23,7 +22,8 @@ def main():
st.subheader("Agent Interaction")

# Agent selection
agent_selection = st.selectbox("Choose an agent:", ["Buddy", "Planner"])
agent_selection = st.selectbox(
"Choose an agent:", ["Buddy", "Planner"])

# Message input and interaction with the selected agent
user_input = st.text_input(
Expand All @@ -32,7 +32,8 @@ def main():

if st.button("Send"):
if agent_selection == "Buddy":
st.session_state.response = st.session_state.buddy.start(user_input)
st.session_state.response = st.session_state.buddy.start(
user_input)
elif agent_selection == "Planner":
st.session_state.response = (
"Planner agent interaction not yet implemented."
Expand Down
10 changes: 5 additions & 5 deletions tests/agents/buddy_test.py → tests/app/agents/buddy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
This module contains unit tests for the Buddy class.
"""

from agents.buddy import Buddy, UserAgent, AssistantAgent
from app.agents.buddy import Buddy, UserAgent, AssistantAgent
from unittest.mock import Mock, patch
import os
import sys
# import os
# import sys
import unittest

sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "..")))
# sys.path.insert(0, os.path.abspath(
# os.path.join(os.path.dirname(__file__), "..", "..")))


class TestBuddy(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from app.helpers.message_router import MessageRouter
from app.helpers.agent_manager import AgentManager
import os
import sys
import pytest

sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
0, os.path.abspath(os.path.join(
os.path.dirname(__file__), "..", "..", ".."))
)

from agents.helpers.agent_manager import AgentManager
from agents.helpers.message_router import MessageRouter


@pytest.mark.asyncio
async def test_message_routing():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# ./tests/agents/planner_test.py

import pytest
from unittest.mock import patch
from agents.services.planner_service import PlannerService
from app.services.planner_service import PlannerService


@pytest.fixture
Expand All @@ -18,11 +16,12 @@ def planner_service():
return service


@patch("agents.services.planner_service.UserProxyAgent.initiate_chat")
@patch("agents.services.planner_service.UserProxyAgent.last_message")
@patch("app.services.planner_service.UserProxyAgent.initiate_chat")
@patch("app.services.planner_service.UserProxyAgent.last_message")
def test_ask_planner(mocked_last_message, mocked_initiate_chat, planner_service):
# Mock the last_message() method to return a dict with 'content' key
mocked_last_message.return_value = {"content": "Go to the store and buy groceries"}
mocked_last_message.return_value = {
"content": "Go to the store and buy groceries"}

response = planner_service.ask_planner("buy groceries")

Expand All @@ -33,8 +32,8 @@ def test_ask_planner(mocked_last_message, mocked_initiate_chat, planner_service)
assert response == "Go to the store and buy groceries"


@patch("agents.services.planner_service.UserProxyAgent.initiate_chat")
@patch("agents.services.planner_service.UserProxyAgent.last_message")
@patch("app.services.planner_service.UserProxyAgent.initiate_chat")
@patch("app.services.planner_service.UserProxyAgent.last_message")
def test_ask_planner_invalid_input(
mocked_last_message, mocked_initiate_chat, planner_service
):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

from agents.user_agent import UserAgent
from app.agents.user_agent import UserAgent

import pytest

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
from agents.helpers.inter_agent_comm import InterAgentComm
from agents.helpers.agent_manager import AgentManager
import sys
import os
import pytest


sys.path.insert(
0, os.path.abspath(os.path.join(
os.path.dirname(__file__), "..", "..", ".."))
)
from app.helpers.inter_agent_comm import InterAgentComm
from app.helpers.agent_manager import AgentManager


@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import sys
import os
from app.helpers.agent_registry import AgentRegistry
import pytest

sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
)

from agents.helpers.agent_registry import AgentRegistry


@pytest.mark.asyncio
async def test_register_agent():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import sys
import os


sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
)

from agents.helpers.function_registry import FunctionRegistry
import pytest
from app.helpers.function_registry import FunctionRegistry


class TestFunctionRegistry:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import sys
import os

sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
)

import pytest
from inter_agent_comm import InterAgentComm
from app.helpers.inter_agent_comm import InterAgentComm


@pytest.mark.asyncio
Expand Down
33 changes: 33 additions & 0 deletions tests/app/helpers/message_router_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from app.helpers.message_router import MessageRouter
from app.helpers.agent_manager import AgentManager
from app.helpers.inter_agent_comm import InterAgentComm
import pytest


@pytest.mark.asyncio
async def test_message_routing():
# Initialize InterAgentComm, AgentManager and MessageRouter
inter_agent_comm = InterAgentComm()
agent_manager = AgentManager(inter_agent_comm)
message_router = MessageRouter(agent_manager, inter_agent_comm)

# Register an agent for testing
await agent_manager.register_agent(
"001", {"name": "Agent Smith", "status": "active"}
)

# Test message sending
assert (
await message_router.send_message("001", "Hello, Agent Smith.")
== "Message sent to agent 001."
)

# Test message fetching
assert await message_router.fetch_messages("001") == ["Hello, Agent Smith."]

# Test message sending after unregistration
await agent_manager.unregister_agent("001")
assert (
await message_router.send_message("001", "Hello, Agent Smith.")
== "Agent 001 not found."
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import patch
import pytest
from agents.services.chat_service import ChatService
from app.services.chat_service import ChatService


@patch("autogen.oai.ChatCompletion.create")
Expand All @@ -10,7 +10,7 @@ def test_initiate_chat(mocked_wait, mocked_create):
mocked_wait.return_value = None

with patch("autogen.AssistantAgent") as MockedAssistant, patch(
"agents.user_agent.UserAgent"
"app.agents.user_agent.UserAgent"
) as MockedProxy:
service = ChatService(
"some_config", {}, MockedAssistant.return_value, MockedProxy.return_value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from agents.services.function_service import FunctionService
from app.services.function_service import FunctionService


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import sys
import os
import pytest
from unittest.mock import patch

sys.path.insert(
0,
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")),
)

from agents.services.planner_service import PlannerService
from app.services.planner_service import PlannerService


@pytest.fixture(scope="function")
def planner_service_with_mocked_chat():
with patch(
"agents.services.planner_service.UserProxyAgent.initiate_chat"
"app.services.planner_service.UserProxyAgent.initiate_chat"
) as MockedInitiateChat:
MockedInitiateChat.return_value = (
None # No return value as we're mocking the method
Expand All @@ -31,7 +23,8 @@ def planner_service_with_mocked_chat():


def test_get_service(planner_service_with_mocked_chat):
assert planner_service_with_mocked_chat.get_service("test_service") == "TestService"
assert planner_service_with_mocked_chat.get_service(
"test_service") == "TestService"


def test_get_parameters(planner_service_with_mocked_chat):
Expand All @@ -42,8 +35,10 @@ def test_get_parameters(planner_service_with_mocked_chat):

def test_ask_planner(planner_service_with_mocked_chat):
with patch(
"agents.services.planner_service.UserProxyAgent.last_message"
"app.services.planner_service.UserProxyAgent.last_message"
) as MockedLastMessage:
MockedLastMessage.return_value = {"content": "Test response from planner"}
response = planner_service_with_mocked_chat.ask_planner("buy groceries")
MockedLastMessage.return_value = {
"content": "Test response from planner"}
response = planner_service_with_mocked_chat.ask_planner(
"buy groceries")
assert response == "Test response from planner"
2 changes: 1 addition & 1 deletion tests/streamlit_app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from streamlit_app import main
from agents.buddy import Buddy
from app.agents.buddy import Buddy


class DynamicMock(Mock):
Expand Down

0 comments on commit c7a377f

Please sign in to comment.