mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] added SerializationMixin and applied to contents, agents, chat client… (#1012)
* added SerializationMixin and applied to contents, agents, chat clients, removed AFBaseModel * fix annotations type * mypy fixes * fix tests * fix serializable subvalues and added large docstring * updated indents in code block * fixed exported urls
This commit is contained in:
committed by
GitHub
Unverified
parent
3eb26632ce
commit
54ad135914
@@ -1,3 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Test utilities for sample testing."""
|
||||
@@ -1,43 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def retry(
|
||||
func: Callable[[], Awaitable[Any]],
|
||||
retries: int = 3,
|
||||
reset: Callable[[], None] | None = None,
|
||||
name: str | None = None,
|
||||
) -> None:
|
||||
"""Retry function with reset capability and proper logging.
|
||||
|
||||
Args:
|
||||
func: The function to retry.
|
||||
retries: Number of retries.
|
||||
reset: Function to reset the state of any variables used in the function.
|
||||
name: Optional name for logging purposes.
|
||||
"""
|
||||
func_name = name or func.__module__
|
||||
logger.info(f"Running {retries} retries with func: {func_name}")
|
||||
|
||||
for i in range(retries):
|
||||
logger.info(f" Try {i + 1} for {func_name}")
|
||||
try:
|
||||
if reset:
|
||||
reset()
|
||||
await func()
|
||||
return
|
||||
except Exception as e:
|
||||
logger.warning(f" On try {i + 1} got this error: {e}")
|
||||
if i == retries - 1: # Last retry
|
||||
raise
|
||||
|
||||
# Binary exponential backoff like Semantic Kernel
|
||||
backoff = 2**i
|
||||
logger.info(f" Sleeping for {backoff} seconds before retrying")
|
||||
await asyncio.sleep(backoff)
|
||||
@@ -1,3 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Sample tests package."""
|
||||
@@ -1,3 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Getting started sample tests."""
|
||||
+2
-2
@@ -146,7 +146,6 @@ from samples.getting_started.agents.openai.openai_responses_client_with_thread i
|
||||
from samples.getting_started.agents.openai.openai_responses_client_with_web_search import (
|
||||
main as openai_responses_client_with_web_search,
|
||||
)
|
||||
from tests.sample_utils import retry
|
||||
|
||||
# Environment variable for controlling sample tests
|
||||
RUN_SAMPLES_TESTS = "RUN_SAMPLES_TESTS"
|
||||
@@ -579,6 +578,7 @@ agent_samples = [
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@mark.parametrize("sample, responses", agent_samples)
|
||||
async def test_agent_samples(sample: Callable[..., Awaitable[Any]], responses: list[str], monkeypatch: MonkeyPatch):
|
||||
"""Test agent samples with input mocking and retry logic."""
|
||||
@@ -592,4 +592,4 @@ async def test_agent_samples(sample: Callable[..., Awaitable[Any]], responses: l
|
||||
return responses.pop(0) if responses else "exit"
|
||||
|
||||
monkeypatch.setattr("builtins.input", mock_input)
|
||||
await retry(sample, retries=3, reset=reset)
|
||||
await sample
|
||||
+1
-2
@@ -32,7 +32,6 @@ from samples.getting_started.chat_client.openai_chat_client import (
|
||||
from samples.getting_started.chat_client.openai_responses_client import (
|
||||
main as openai_responses_client,
|
||||
)
|
||||
from tests.sample_utils import retry
|
||||
|
||||
# Environment variable for controlling sample tests
|
||||
RUN_SAMPLES_TESTS = "RUN_SAMPLES_TESTS"
|
||||
@@ -132,4 +131,4 @@ async def test_chat_client_samples(
|
||||
return responses.pop(0) if responses else "exit"
|
||||
|
||||
monkeypatch.setattr("builtins.input", mock_input)
|
||||
await retry(sample, retries=3, reset=reset)
|
||||
await sample
|
||||
+1
-2
@@ -10,7 +10,6 @@ from pytest import MonkeyPatch, mark, param
|
||||
|
||||
from samples.getting_started.threads.custom_chat_message_store_thread import main as threads_custom_store
|
||||
from samples.getting_started.threads.suspend_resume_thread import main as threads_suspend_resume
|
||||
from tests.sample_utils import retry
|
||||
|
||||
# Environment variable for controlling sample tests
|
||||
RUN_SAMPLES_TESTS = "RUN_SAMPLES_TESTS"
|
||||
@@ -51,4 +50,4 @@ async def test_thread_samples(sample: Callable[..., Awaitable[Any]], responses:
|
||||
return responses.pop(0) if responses else "exit"
|
||||
|
||||
monkeypatch.setattr("builtins.input", mock_input)
|
||||
await retry(sample, retries=3, reset=reset)
|
||||
await sample
|
||||
Reference in New Issue
Block a user