Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: remove mistaken on_activate func #9839

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading