This is an automated email from the ASF dual-hosted git repository.
sxnan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-agents.git
The following commit(s) were added to refs/heads/main by this push:
new 4c0ea9a [Feature][API][python] Use Python property to simplify the
getter in RunnerContext (#183)
4c0ea9a is described below
commit 4c0ea9ac600e01a8a1dab15cd8137280191e6b42
Author: Eugene <[email protected]>
AuthorDate: Mon Sep 22 19:46:22 2025 +0800
[Feature][API][python] Use Python property to simplify the getter in
RunnerContext (#183)
---
python/flink_agents/api/memory_reference.py | 2 +-
python/flink_agents/api/runner_context.py | 15 ++++++++++-----
python/flink_agents/examples/agent_example.py | 4 ++--
python/flink_agents/examples/my_agent.py | 4 ++--
.../quickstart/agents/product_suggestion_agent.py | 10 +++++-----
.../examples/quickstart/agents/review_analysis_agent.py | 6 +++---
python/flink_agents/plan/actions/chat_model_action.py | 4 ++--
python/flink_agents/runtime/flink_runner_context.py | 15 ++++++++++-----
python/flink_agents/runtime/local_runner.py | 15 ++++++++++-----
.../flink_agents/runtime/tests/test_memory_reference.py | 3 ++-
10 files changed, 47 insertions(+), 31 deletions(-)
diff --git a/python/flink_agents/api/memory_reference.py
b/python/flink_agents/api/memory_reference.py
index 0b9d403..952f7b5 100644
--- a/python/flink_agents/api/memory_reference.py
+++ b/python/flink_agents/api/memory_reference.py
@@ -61,4 +61,4 @@ class MemoryRef(BaseModel):
Any
The deserialized, original data object.
"""
- return ctx.get_short_term_memory().get(self)
+ return ctx.short_term_memory.get(self)
diff --git a/python/flink_agents/api/runner_context.py
b/python/flink_agents/api/runner_context.py
index 9555ce1..0a3eb01 100644
--- a/python/flink_agents/api/runner_context.py
+++ b/python/flink_agents/api/runner_context.py
@@ -55,8 +55,9 @@ class RunnerContext(ABC):
The type of the resource.
"""
+ @property
@abstractmethod
- def get_action_config(self) -> Dict[str, Any]:
+ def action_config(self) -> Dict[str, Any]:
"""Get config of the action.
Returns:
@@ -80,8 +81,9 @@ class RunnerContext(ABC):
The config option value.
"""
+ @property
@abstractmethod
- def get_short_term_memory(self) -> "MemoryObject":
+ def short_term_memory(self) -> "MemoryObject":
"""Get the short-term memory.
Returns:
@@ -90,8 +92,9 @@ class RunnerContext(ABC):
The root object of the short-term memory.
"""
+ @property
@abstractmethod
- def get_agent_metric_group(self) -> MetricGroup:
+ def agent_metric_group(self) -> MetricGroup:
"""Get the metric group for flink agents.
Returns:
@@ -100,8 +103,9 @@ class RunnerContext(ABC):
The metric group shared across all actions.
"""
+ @property
@abstractmethod
- def get_action_metric_group(self) -> MetricGroup:
+ def action_metric_group(self) -> MetricGroup:
"""Get the individual metric group dedicated for each action.
Returns:
@@ -135,8 +139,9 @@ class RunnerContext(ABC):
The result of the function.
"""
+ @property
@abstractmethod
- def get_config(self) -> ReadableConfiguration:
+ def config(self) -> ReadableConfiguration:
"""Get the readable configuration for flink agents.
Returns:
diff --git a/python/flink_agents/examples/agent_example.py
b/python/flink_agents/examples/agent_example.py
index 613c6bb..1152393 100644
--- a/python/flink_agents/examples/agent_example.py
+++ b/python/flink_agents/examples/agent_example.py
@@ -50,7 +50,7 @@ class MyAgent(Agent):
def first_action(event: Event, ctx: RunnerContext): # noqa D102
key = ctx.key
input_message = event.input
- memory = ctx.get_short_term_memory()
+ memory = ctx.short_term_memory
data_path = f"user_data.{key}"
previous_data: ProcessedData = memory.get(data_path)
@@ -71,7 +71,7 @@ class MyAgent(Agent):
@staticmethod
def second_action(event: Event, ctx: RunnerContext): # noqa D102
content_ref: MemoryRef = event.value
- memory = ctx.get_short_term_memory()
+ memory = ctx.short_term_memory
processed_data: ProcessedData = memory.get(content_ref)
diff --git a/python/flink_agents/examples/my_agent.py
b/python/flink_agents/examples/my_agent.py
index 8dd8498..1af739e 100644
--- a/python/flink_agents/examples/my_agent.py
+++ b/python/flink_agents/examples/my_agent.py
@@ -86,7 +86,7 @@ class DataStreamAgent(Agent):
return True
input_data = event.input
- stm = ctx.get_short_term_memory()
+ stm = ctx.short_term_memory
current_total = stm.get("status.total_reviews") or 0
total = current_total + 1
@@ -105,7 +105,7 @@ class DataStreamAgent(Agent):
@staticmethod
def second_action(event: Event, ctx: RunnerContext): # noqa D102
input_data = event.value
- stm = ctx.get_short_term_memory()
+ stm = ctx.short_term_memory
resolved_data: ItemData = stm.get(input_data)
content = copy.deepcopy(resolved_data)
diff --git
a/python/flink_agents/examples/quickstart/agents/product_suggestion_agent.py
b/python/flink_agents/examples/quickstart/agents/product_suggestion_agent.py
index 8d9b072..31babc3 100644
--- a/python/flink_agents/examples/quickstart/agents/product_suggestion_agent.py
+++ b/python/flink_agents/examples/quickstart/agents/product_suggestion_agent.py
@@ -108,7 +108,7 @@ class ProductSuggestionAgent(Agent):
input:
{input}
"""
- return Prompt.from_text("generate_suggestion_prompt", prompt_str)
+ return Prompt.from_text(prompt_str)
@chat_model_setup
@staticmethod
@@ -125,8 +125,8 @@ class ProductSuggestionAgent(Agent):
def process_input(event: InputEvent, ctx: RunnerContext) -> None:
"""Process input event."""
input: ProductReviewSummary = event.input
- ctx.get_short_term_memory().set("id", input.id)
- ctx.get_short_term_memory().set("score_hist", input.score_hist)
+ ctx.short_term_memory.set("id", input.id)
+ ctx.short_term_memory.set("score_hist", input.score_hist)
content = f"""
"id": {input.id},
@@ -151,8 +151,8 @@ class ProductSuggestionAgent(Agent):
ctx.send_event(
OutputEvent(
output=ProductSuggestion(
- id=ctx.get_short_term_memory().get("id"),
-
score_hist=ctx.get_short_term_memory().get("score_hist"),
+ id=ctx.short_term_memory.get("id"),
+ score_hist=ctx.short_term_memory.get("score_hist"),
suggestions=json_content["suggestions"],
)
)
diff --git
a/python/flink_agents/examples/quickstart/agents/review_analysis_agent.py
b/python/flink_agents/examples/quickstart/agents/review_analysis_agent.py
index e3bbde5..2b067c5 100644
--- a/python/flink_agents/examples/quickstart/agents/review_analysis_agent.py
+++ b/python/flink_agents/examples/quickstart/agents/review_analysis_agent.py
@@ -107,7 +107,7 @@ class ReviewAnalysisAgent(Agent):
input:
{input}
"""
- return Prompt.from_text("review_analysis_prompt", prompt_str)
+ return Prompt.from_text(prompt_str)
@chat_model_setup
@staticmethod
@@ -124,7 +124,7 @@ class ReviewAnalysisAgent(Agent):
def process_input(event: InputEvent, ctx: RunnerContext) -> None:
"""Process input event and send chat request for review analysis."""
input: ProductReview = event.input
- ctx.get_short_term_memory().set("id", input.id)
+ ctx.short_term_memory.set("id", input.id)
content = f"""
"id": {input.id},
@@ -142,7 +142,7 @@ class ReviewAnalysisAgent(Agent):
ctx.send_event(
OutputEvent(
output=ProductReviewAnalysisRes(
- id=ctx.get_short_term_memory().get("id"),
+ id=ctx.short_term_memory.get("id"),
score=json_content["score"],
reasons=json_content["reasons"],
)
diff --git a/python/flink_agents/plan/actions/chat_model_action.py
b/python/flink_agents/plan/actions/chat_model_action.py
index 967d0d9..bb74a5f 100644
--- a/python/flink_agents/plan/actions/chat_model_action.py
+++ b/python/flink_agents/plan/actions/chat_model_action.py
@@ -53,7 +53,7 @@ def chat(
# TODO: support async execution of chat.
response = chat_model.chat(messages)
- short_term_memory = ctx.get_short_term_memory()
+ short_term_memory = ctx.short_term_memory
# generate tool request event according tool calls in response
if len(response.tool_calls) > 0:
@@ -113,7 +113,7 @@ def process_chat_request_or_tool_response(event: Event,
ctx: RunnerContext) -> N
Internally, this action will use short term memory to save the tool call
context,
which is a dict mapping request id to chat messages.
"""
- short_term_memory = ctx.get_short_term_memory()
+ short_term_memory = ctx.short_term_memory
if isinstance(event, ChatRequestEvent):
chat(
initial_request_id=event.id,
diff --git a/python/flink_agents/runtime/flink_runner_context.py
b/python/flink_agents/runtime/flink_runner_context.py
index e520949..8f3c323 100644
--- a/python/flink_agents/runtime/flink_runner_context.py
+++ b/python/flink_agents/runtime/flink_runner_context.py
@@ -73,8 +73,9 @@ class FlinkRunnerContext(RunnerContext):
def get_resource(self, name: str, type: ResourceType) -> Resource:
return self.__agent_plan.get_resource(name, type)
+ @property
@override
- def get_action_config(self) -> Dict[str, Any]:
+ def action_config(self) -> Dict[str, Any]:
"""Get config of the action."""
return self.__agent_plan.get_action_config(
self._j_runner_context.getActionName()
@@ -87,8 +88,9 @@ class FlinkRunnerContext(RunnerContext):
action_name=self._j_runner_context.getActionName(), key=key
)
+ @property
@override
- def get_short_term_memory(self) -> FlinkMemoryObject:
+ def short_term_memory(self) -> FlinkMemoryObject:
"""Get the short-term memory object associated with this context.
Returns:
@@ -103,8 +105,9 @@ class FlinkRunnerContext(RunnerContext):
err_msg = "Failed to get short-term memory of runner context"
raise RuntimeError(err_msg) from e
+ @property
@override
- def get_agent_metric_group(self) -> FlinkMetricGroup:
+ def agent_metric_group(self) -> FlinkMetricGroup:
"""Get the metric group for flink agents.
Returns:
@@ -114,8 +117,9 @@ class FlinkRunnerContext(RunnerContext):
"""
return FlinkMetricGroup(self._j_runner_context.getAgentMetricGroup())
+ @property
@override
- def get_action_metric_group(self) -> FlinkMetricGroup:
+ def action_metric_group(self) -> FlinkMetricGroup:
"""Get the individual metric group dedicated for each action.
Returns:
@@ -144,8 +148,9 @@ class FlinkRunnerContext(RunnerContext):
yield
return future.result()
+ @property
@override
- def get_config(self) -> ReadableConfiguration:
+ def config(self) -> ReadableConfiguration:
"""Get the readable configuration for flink agents.
Returns:
diff --git a/python/flink_agents/runtime/local_runner.py
b/python/flink_agents/runtime/local_runner.py
index 6f36221..8f0441c 100644
--- a/python/flink_agents/runtime/local_runner.py
+++ b/python/flink_agents/runtime/local_runner.py
@@ -109,8 +109,9 @@ class LocalRunnerContext(RunnerContext):
def get_resource(self, name: str, type: ResourceType) -> Resource:
return self.__agent_plan.get_resource(name, type)
+ @property
@override
- def get_action_config(self) -> Dict[str, Any]:
+ def action_config(self) -> Dict[str, Any]:
"""Get config of the action."""
return
self.__agent_plan.get_action_config(action_name=self.action_name)
@@ -121,8 +122,9 @@ class LocalRunnerContext(RunnerContext):
action_name=self.action_name, key=key
)
+ @property
@override
- def get_short_term_memory(self) -> MemoryObject:
+ def short_term_memory(self) -> MemoryObject:
"""Get the short-term memory object associated with this context.
Returns:
@@ -132,14 +134,16 @@ class LocalRunnerContext(RunnerContext):
"""
return self._short_term_memory
+ @property
@override
- def get_agent_metric_group(self) -> MetricGroup:
+ def agent_metric_group(self) -> MetricGroup:
# TODO: Support metric mechanism for local agent execution.
err_msg = "Metric mechanism is not supported for local agent execution
yet."
raise NotImplementedError(err_msg)
+ @property
@override
- def get_action_metric_group(self) -> MetricGroup:
+ def action_metric_group(self) -> MetricGroup:
# TODO: Support metric mechanism for local agent execution.
err_msg = "Metric mechanism is not supported for local agent execution
yet."
raise NotImplementedError(err_msg)
@@ -160,8 +164,9 @@ class LocalRunnerContext(RunnerContext):
yield
return func_result
+ @property
@override
- def get_config(self) -> AgentConfiguration:
+ def config(self) -> AgentConfiguration:
return self._config
diff --git a/python/flink_agents/runtime/tests/test_memory_reference.py
b/python/flink_agents/runtime/tests/test_memory_reference.py
index 8c5e395..d6f157b 100644
--- a/python/flink_agents/runtime/tests/test_memory_reference.py
+++ b/python/flink_agents/runtime/tests/test_memory_reference.py
@@ -24,7 +24,8 @@ class MockRunnerContext: # noqa D101
"""Mock RunnerContext for testing resolve() method."""
self._memory = memory
- def get_short_term_memory(self) -> LocalMemoryObject: # noqa D102
+ @property
+ def short_term_memory(self) -> LocalMemoryObject: # noqa D102
return self._memory