Skip to content

Commit

Permalink
remove unnecessary import
Browse files Browse the repository at this point in the history
  • Loading branch information
peterychang committed Dec 2, 2024
1 parent b5a659c commit be68de0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
from aioconsole import ainput
from inspect import iscoroutinefunction
from typing import Awaitable, Callable, Coroutine, List, Optional, Sequence, Union, cast
from typing import Awaitable, Callable, List, Optional, Sequence, Union, cast

from aioconsole import ainput
from autogen_core.base import CancellationToken

from ..base import Response
Expand All @@ -14,12 +14,14 @@
AsyncInputFunc = Callable[[str, Optional[CancellationToken]], Awaitable[str]]
InputFuncType = Union[SyncInputFunc, AsyncInputFunc]


# TODO: ainput doesn't seem to play nicely with jupyter.
async def cancellable_input(prompt: str, cancellation_token: Optional[CancellationToken]) -> Awaitable[str]:
task = asyncio.create_task(ainput(prompt))
cancellation_token.link_future(task)
return await task


class UserProxyAgent(BaseChatAgent):
"""An agent that can represent a human user through an input function.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
from typing import Any, List, Dict
from .... import TRACE_LOGGER_NAME
from typing import Any, Dict, List

from autogen_core.base import AgentId, CancellationToken, MessageContext
from autogen_core.components import DefaultTopicId, Image, event, rpc
Expand All @@ -12,8 +11,10 @@
UserMessage,
)

from .... import TRACE_LOGGER_NAME
from ....base import Response, TerminationCondition
from ....messages import AgentMessage, MultiModalMessage, StopMessage, TextMessage, ChatMessage
from ....messages import AgentMessage, ChatMessage, MultiModalMessage, StopMessage, TextMessage
from .._base_group_chat_manager import BaseGroupChatManager
from .._events import (
GroupChatAgentResponse,
GroupChatMessage,
Expand All @@ -22,7 +23,6 @@
GroupChatStart,
GroupChatTermination,
)
from .._base_group_chat_manager import BaseGroupChatManager
from ._prompts import (
ORCHESTRATOR_FINAL_ANSWER_PROMPT,
ORCHESTRATOR_PROGRESS_LEDGER_PROMPT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,38 @@
],
"source": [
"import asyncio\n",
"\n",
"from autogen_agentchat.agents import UserProxyAgent\n",
"from autogen_agentchat.messages import TextMessage\n",
"from autogen_core.base import CancellationToken\n",
"\n",
"token = CancellationToken()\n",
"agent = UserProxyAgent(\"user_proxy\")\n",
"\n",
"\n",
"async def timeout(delay):\n",
" await asyncio.sleep(delay)\n",
" await asyncio.sleep(delay)\n",
"\n",
"\n",
"def cancellation_callback(task):\n",
" token.cancel()\n",
" token.cancel()\n",
"\n",
"\n",
"try:\n",
" timeout_task = asyncio.create_task(timeout(3))\n",
" timeout_task.add_done_callback(cancellation_callback)\n",
" agent_task = asyncio.create_task(\n",
" agent.on_messages(\n",
" [TextMessage(content=\"What is your name? \", source=\"user\")],\n",
" cancellation_token=token,\n",
" timeout_task = asyncio.create_task(timeout(3))\n",
" timeout_task.add_done_callback(cancellation_callback)\n",
" agent_task = asyncio.create_task(\n",
" agent.on_messages(\n",
" [TextMessage(content=\"What is your name? \", source=\"user\")],\n",
" cancellation_token=token,\n",
" )\n",
" )\n",
" )\n",
"\n",
" response = await agent_task\n",
" print(f\"Your name is {response.chat_message.content}\")\n",
" response = await agent_task\n",
" print(f\"Your name is {response.chat_message.content}\")\n",
"\n",
"except Exception as e:\n",
" print(f\"Exception: {e}\")\n",
" print(f\"Exception: {e}\")\n",
"except BaseException as e:\n",
" print(f\"BaseException: {e}\")"
" print(f\"BaseException: {e}\")"
]
},
{
Expand Down

0 comments on commit be68de0

Please sign in to comment.