Skip to content

Commit dc7cb45

Browse files
eavanvalkenburgmoonbox3
andauthoredNov 29, 2024
Python: remove mistaken on_activate func (microsoft#9839)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Removes an outdated function `on_activate` from the KernelProcessStep class and the one place it was overridden. Fixes microsoft#9829 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 Co-authored-by: Evan Mattson <[email protected]>
1 parent 6d3497e commit dc7cb45

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed
 

‎python/samples/getting_started_with_processes/step01/step01_processes.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class UserInputState(KernelBaseModel):
3737
current_input_index: int = 0
3838

3939

40-
class ScriptedUserInputStep(KernelProcessStep[UserInputState]):
40+
class UserInputStep(KernelProcessStep[UserInputState]):
4141
GET_USER_INPUT: ClassVar[str] = "get_user_input"
4242

4343
def create_default_state(self) -> "UserInputState":
@@ -48,26 +48,21 @@ def populate_user_inputs(self):
4848
"""Method to be overridden by the user to populate with custom user messages."""
4949
pass
5050

51-
async def on_activate(self):
52-
"""This is called during the activation of the process step."""
53-
self.populate_user_inputs()
54-
5551
async def activate(self, state: KernelProcessStepState[UserInputState]):
5652
"""Activates the step and sets the state."""
5753
state.state = state.state or self.create_default_state()
5854
self.state = state.state
5955
self.populate_user_inputs()
60-
pass
6156

6257
@kernel_function(name=GET_USER_INPUT)
6358
async def get_user_input(self, context: KernelProcessStepContext):
6459
"""Gets the user input."""
6560
if not self.state:
6661
raise ValueError("State has not been initialized")
6762

68-
user_message = self.state.user_inputs[self.state.current_input_index]
63+
user_message = input("USER: ")
6964

70-
print(f"USER: {user_message}")
65+
# print(f"USER: {user_message}")
7166

7267
if "exit" in user_message:
7368
await context.emit_event(process_event=ChatBotEvents.Exit, data=None)
@@ -79,7 +74,7 @@ async def get_user_input(self, context: KernelProcessStepContext):
7974
await context.emit_event(process_event=CommonEvents.UserInputReceived, data=user_message)
8075

8176

82-
class ChatUserInputStep(ScriptedUserInputStep):
77+
class ScriptedInputStep(UserInputStep):
8378
def populate_user_inputs(self):
8479
"""Override the method to populate user inputs for the chat step."""
8580
if self.state is not None:
@@ -89,6 +84,25 @@ def populate_user_inputs(self):
8984
self.state.user_inputs.append("How wide is the widest river?")
9085
self.state.user_inputs.append("exit")
9186

87+
@kernel_function
88+
async def get_user_input(self, context: KernelProcessStepContext):
89+
"""Gets the user input."""
90+
if not self.state:
91+
raise ValueError("State has not been initialized")
92+
93+
user_message = self.state.user_inputs[self.state.current_input_index]
94+
95+
print(f"USER: {user_message}")
96+
97+
if "exit" in user_message:
98+
await context.emit_event(process_event=ChatBotEvents.Exit, data=None)
99+
return
100+
101+
self.state.current_input_index += 1
102+
103+
# Emit the user input event
104+
await context.emit_event(process_event=CommonEvents.UserInputReceived, data=user_message)
105+
92106

93107
class IntroStep(KernelProcessStep):
94108
@kernel_function
@@ -146,14 +160,14 @@ async def get_chat_response(self, context: "KernelProcessStepContext", user_mess
146160
kernel = Kernel()
147161

148162

149-
async def step01_processes():
163+
async def step01_processes(scripted: bool = True):
150164
kernel.add_service(OpenAIChatCompletion(service_id="default"))
151165

152166
process = ProcessBuilder(name="ChatBot")
153167

154168
# Define the steps on the process builder based on their types, not concrete objects
155169
intro_step = process.add_step(IntroStep)
156-
user_input_step = process.add_step(ChatUserInputStep)
170+
user_input_step = process.add_step(ScriptedInputStep if scripted else UserInputStep)
157171
response_step = process.add_step(ChatBotResponseStep)
158172

159173
# Define the input event that starts the process and where to send it
@@ -186,4 +200,5 @@ async def step01_processes():
186200

187201

188202
if __name__ == "__main__":
189-
asyncio.run(step01_processes())
203+
# if you want to run this sample with your won input, set the below parameter to False
204+
asyncio.run(step01_processes(scripted=False))

‎python/semantic_kernel/processes/kernel_process/kernel_process_step.py

-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,3 @@ class KernelProcessStep(ABC, KernelBaseModel, Generic[TState]):
2121
async def activate(self, state: "KernelProcessStepState[TState]"):
2222
"""Activates the step and sets the state."""
2323
pass # pragma: no cover
24-
25-
async def on_activate(self):
26-
"""To be overridden by subclasses if needed."""
27-
pass # pragma: no cover

0 commit comments

Comments
 (0)
Please sign in to comment.