Browse Source

Fix async hil sample

pull/4911/head
Eric Zhu 1 year ago
parent
commit
c4eeaf80bb
3 changed files with 51 additions and 7 deletions
  1. +1
    -0
      python/samples/core_async_human_in_the_loop/.gitignore
  2. +12
    -7
      python/samples/core_async_human_in_the_loop/main.py
  3. +38
    -0
      python/samples/core_async_human_in_the_loop/model_config_template.json

+ 1
- 0
python/samples/core_async_human_in_the_loop/.gitignore View File

@@ -0,0 +1 @@
model_config.json

+ 12
- 7
python/samples/core_async_human_in_the_loop/main.py View File

@@ -28,7 +28,7 @@ import datetime
import json
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from typing import Any, Mapping, Optional
from typing import Any, Dict, Mapping, Optional

from autogen_core import (
CancellationToken,
@@ -50,11 +50,10 @@ from autogen_core.models import (
)
from autogen_core.tools import BaseTool
from pydantic import BaseModel, Field
from utils import get_chat_completion_client_from_envs


@dataclass
class TextMessage(BaseModel):
class TextMessage:
source: str
content: str

@@ -113,7 +112,7 @@ class SlowUserProxyAgent(RoutedAgent):

async def save_state(self) -> Mapping[str, Any]:
state_to_save = {
"memory": self._model_context.save_state(),
"memory": await self._model_context.save_state(),
}
return state_to_save

@@ -251,7 +250,7 @@ class TerminationHandler(DefaultInterventionHandler):
return self.terminateMessage.content


async def main(latest_user_input: Optional[str] = None) -> None | str:
async def main(model_config: Dict[str, Any], latest_user_input: Optional[str] = None) -> None | str:
"""
Asynchronous function that serves as the entry point of the program.
This function initializes the necessary components for the program and registers the user and scheduling assistant agents.
@@ -268,6 +267,8 @@ async def main(latest_user_input: Optional[str] = None) -> None | str:
"""
global state_persister

model_client = ChatCompletionClient.load_component(model_config)

termination_handler = TerminationHandler()
needs_user_input_handler = NeedsUserInputHandler()
runtime = SingleThreadedAgentRuntime(intervention_handlers=[needs_user_input_handler, termination_handler])
@@ -283,11 +284,12 @@ async def main(latest_user_input: Optional[str] = None) -> None | str:
lambda: SchedulingAssistantAgent(
"SchedulingAssistant",
description="AI that helps you schedule meetings",
model_client=get_chat_completion_client_from_envs(model="gpt-4o-mini"),
model_client=model_client,
initial_message=initial_schedule_assistant_message,
),
)

runtime_initiation_message: UserTextMessage | AssistantTextMessage
if latest_user_input is not None:
runtime_initiation_message = UserTextMessage(content=latest_user_input, source="User")
else:
@@ -330,6 +332,9 @@ if __name__ == "__main__":
# if os.path.exists("state.json"):
# os.remove("state.json")

with open("model_config.json") as f:
model_config = json.load(f)

def get_user_input(question_for_user: str):
print("--------------------------QUESTION_FOR_USER--------------------------")
print(question_for_user)
@@ -342,7 +347,7 @@ if __name__ == "__main__":
user_input = get_user_input(question_for_user)
else:
user_input = None
user_input_needed = await main(user_input)
user_input_needed = await main(model_config, user_input)
if user_input_needed:
await run_main(user_input_needed)



+ 38
- 0
python/samples/core_async_human_in_the_loop/model_config_template.json View File

@@ -0,0 +1,38 @@
// Use Azure OpenAI with AD token provider.
// {
// "provider": "AzureOpenAIChatCompletionClient",
// "config": {
// "model": "gpt-4o-2024-05-13",
// "azure_endpoint": "https://{your-custom-endpoint}.openai.azure.com/",
// "azure_deployment": "{your-azure-deployment}",
// "api_version": "2024-06-01",
// "azure_ad_token_provider": {
// "provider": "autogen_ext.models.openai.AzureTokenProvider",
// "config": {
// "provider_kind": "DefaultAzureCredential",
// "scopes": [
// "https://cognitiveservices.azure.com/.default"
// ]
// }
// }
// }
// }
// Use Azure Open AI with key
// {
// "provider": "AzureOpenAIChatCompletionClient",
// "config": {
// "model": "gpt-4o-2024-05-13",
// "azure_endpoint": "https://{your-custom-endpoint}.openai.azure.com/",
// "azure_deployment": "{your-azure-deployment}",
// "api_version": "2024-06-01",
// "api_key": "REPLACE_WITH_YOUR_API_KEY"
// }
// }
// Use Open AI with key
{
"provider": "OpenAIChatCompletionClient",
"config": {
"model": "gpt-4o-2024-05-13",
"api_key": "REPLACE_WITH_YOUR_API_KEY"
}
}

Loading…
Cancel
Save