Skip to content

Commit

Permalink
Python: remove mistaken on_activate func (#9839)
Browse files Browse the repository at this point in the history
### 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 #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]>
  • Loading branch information
eavanvalkenburg and moonbox3 authored Nov 29, 2024
1 parent 6d3497e commit dc7cb45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class UserInputState(KernelBaseModel):
current_input_index: int = 0


class ScriptedUserInputStep(KernelProcessStep[UserInputState]):
class UserInputStep(KernelProcessStep[UserInputState]):
GET_USER_INPUT: ClassVar[str] = "get_user_input"

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

async def on_activate(self):
"""This is called during the activation of the process step."""
self.populate_user_inputs()

async def activate(self, state: KernelProcessStepState[UserInputState]):
"""Activates the step and sets the state."""
state.state = state.state or self.create_default_state()
self.state = state.state
self.populate_user_inputs()
pass

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

user_message = self.state.user_inputs[self.state.current_input_index]
user_message = input("USER: ")

print(f"USER: {user_message}")
# print(f"USER: {user_message}")

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


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

@kernel_function
async def get_user_input(self, context: KernelProcessStepContext):
"""Gets the user input."""
if not self.state:
raise ValueError("State has not been initialized")

user_message = self.state.user_inputs[self.state.current_input_index]

print(f"USER: {user_message}")

if "exit" in user_message:
await context.emit_event(process_event=ChatBotEvents.Exit, data=None)
return

self.state.current_input_index += 1

# Emit the user input event
await context.emit_event(process_event=CommonEvents.UserInputReceived, data=user_message)


class IntroStep(KernelProcessStep):
@kernel_function
Expand Down Expand Up @@ -146,14 +160,14 @@ async def get_chat_response(self, context: "KernelProcessStepContext", user_mess
kernel = Kernel()


async def step01_processes():
async def step01_processes(scripted: bool = True):
kernel.add_service(OpenAIChatCompletion(service_id="default"))

process = ProcessBuilder(name="ChatBot")

# Define the steps on the process builder based on their types, not concrete objects
intro_step = process.add_step(IntroStep)
user_input_step = process.add_step(ChatUserInputStep)
user_input_step = process.add_step(ScriptedInputStep if scripted else UserInputStep)
response_step = process.add_step(ChatBotResponseStep)

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


if __name__ == "__main__":
asyncio.run(step01_processes())
# if you want to run this sample with your won input, set the below parameter to False
asyncio.run(step01_processes(scripted=False))
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@ class KernelProcessStep(ABC, KernelBaseModel, Generic[TState]):
async def activate(self, state: "KernelProcessStepState[TState]"):
"""Activates the step and sets the state."""
pass # pragma: no cover

async def on_activate(self):
"""To be overridden by subclasses if needed."""
pass # pragma: no cover

0 comments on commit dc7cb45

Please sign in to comment.