-
Notifications
You must be signed in to change notification settings - Fork 84
Agentic flow integration #1849
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
Open
hasinaxp
wants to merge
3
commits into
digiteinfotech:master
Choose a base branch
from
hasinaxp:agentic_flow_integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Agentic flow integration #1849
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter | ||
from typing import List | ||
from rasa.cli import SubParsersAction | ||
|
||
from kairon.events.definitions.agentic_flow import AgenticFlowEvent | ||
|
||
|
||
def exec_agentic_flow(args): | ||
AgenticFlowEvent(args.bot, args.user).execute(flow_name=args.flow_name, slot_data=args.slot_data) | ||
|
||
|
||
def add_subparser(subparsers: SubParsersAction, parents: List[ArgumentParser]): | ||
agentic_fow_parser = subparsers.add_parser( | ||
"agentic-flow", | ||
conflict_handler="resolve", | ||
formatter_class=ArgumentDefaultsHelpFormatter, | ||
parents=parents, | ||
help="Mail channel initiate reading" | ||
) | ||
agentic_fow_parser.add_argument('bot', | ||
type=str, | ||
help="Bot id for which command is executed", action='store') | ||
|
||
agentic_fow_parser.add_argument('user', | ||
type=str, | ||
help="Kairon user who is initiating the command", action='store') | ||
|
||
agentic_fow_parser.add_argument('flow_name', | ||
type=str, | ||
help="Kairon flow name to execute", action='store') | ||
|
||
agentic_fow_parser.add_argument('slot_data', | ||
type=str, | ||
help="json containing slot values dictionary", action='store') | ||
|
||
agentic_fow_parser.set_defaults(func=exec_agentic_flow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import asyncio | ||
import json | ||
from typing import Text | ||
from loguru import logger | ||
from kairon import Utility | ||
from kairon.events.definitions.base import EventsBase | ||
from kairon.exceptions import AppException | ||
from kairon.shared.chat.agent.agent_flow import AgenticFlow | ||
from kairon.shared.constants import EventClass | ||
|
||
|
||
class AgenticFlowEvent(EventsBase): | ||
""" | ||
Event to execute an agentic flow | ||
""" | ||
|
||
def __init__(self, bot: Text, user: Text, **kwargs): | ||
""" | ||
Initialise event. | ||
""" | ||
self.bot = bot | ||
self.user = user | ||
self.flow_name = kwargs.get('flow_name') | ||
|
||
def validate(self): | ||
""" | ||
validate mail channel exists and works properly | ||
""" | ||
if self.flow_name: | ||
return AgenticFlow.flow_exists(self.bot, self.flow_name) | ||
|
||
def enqueue(self, **kwargs): | ||
""" | ||
Send event to event server. | ||
""" | ||
try: | ||
payload = {'bot': self.bot, 'user': self.user} | ||
if flow_name := kwargs.get('flow_name'): | ||
payload['flow_name'] = flow_name | ||
self.flow_name = flow_name | ||
if slot_data := kwargs.get('slot_data'): | ||
payload['slot_data'] = slot_data | ||
self.validate() | ||
Utility.request_event_server(EventClass.agentic_flow, payload) | ||
except Exception as e: | ||
logger.error(str(e)) | ||
raise AppException(e) | ||
|
||
def execute(self, **kwargs): | ||
""" | ||
Execute the event. | ||
""" | ||
try: | ||
if flow_name := kwargs.get('flow_name'): | ||
self.flow_name = flow_name | ||
|
||
slot_vals = {} | ||
if slot_data := kwargs.get('slot_data'): | ||
if isinstance(slot_data, str): | ||
slot_data = json.loads(slot_data) | ||
slot_vals = slot_data | ||
flow = AgenticFlow(bot=self.bot, slot_vals=slot_vals) | ||
resp, errors = asyncio.run(flow.execute_rule(self.flow_name)) | ||
logger.info(resp) | ||
if errors: | ||
logger.error(f"Failed to execute flow {self.flow_name}. Errors: {errors}") | ||
raise AppException(f"Failed to execute flow {self.flow_name}. Errors: {errors}") | ||
except Exception as e: | ||
logger.error(str(e)) | ||
raise AppException(f"Failed to execute flow {self.flow_name} for bot {self.bot}. Error: {str(e)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling for AgenticFlowEvent execution
The function doesn't handle any exceptions that might be raised during the execution of the agentic flow, which could lead to ungraceful termination of the CLI command.
📝 Committable suggestion