Skip to content

Commit

Permalink
Remove excess rule fetching on /training/intents page (#754)
Browse files Browse the repository at this point in the history
* Merge relevant from old branch

* Fix guard

* Fix response logic

* Fix deps

* Remove deupe code

* TODOs

* Revert unrelated changes

* Clean up

* Clean up

* Clean up

* Fix deletion

* Clean up

* Move entities

* Clean up

* Clean up

* Fix adding example

* Fix examples

* Clean up

* Clean up

* Clean up

* Fix rule

* Fix editing example

* gsa

* Clean up

* Clean up

* Clean up

* Fix the issue

* PoC without data mapping

* PoC with poor format

* PoC with better format

* Clean up

* Cle

* PoC component

* Clean up

* Fix name

* Fix query

* Fix fetch

* Revert unrelated changes

* Clean up

* Clean up

* Clean up

* Clean up

* PoC request

* Finish
  • Loading branch information
IgorKrupenja authored Dec 13, 2024
1 parent 45782c5 commit 97bfef9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
3 changes: 3 additions & 0 deletions DSL/DMapper/hbs/get_rule_by_intent_id.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "{{hits.0._source.rule}}"
}
6 changes: 4 additions & 2 deletions DSL/OpenSearch/templates/response-with-name-and-text.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"query": "*{{response_name}}*",
"default_field": "name"
}
},
}
{{#response_text}},
{
"query_string": {
"query": "*{{response_text}}*",
"default_field": "response.text"
}
}
{{/response_text}}
]
}
}
Expand All @@ -27,4 +29,4 @@
"response_text": ""
}
}
}
}
42 changes: 42 additions & 0 deletions DSL/Ruuter.private/GET/rasa/rule-by-intent-id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
declaration:
call: declare
version: 0.1
description: "Get rule by intent name"
method: get
accepts: json
returns: json
namespace: training
allowlist:
params:
- field: intent
type: string
description: "Intent ID"

assign_values:
assign:
rule: ${"rule_" + incoming.params.intent}

getRules:
call: http.post
args:
url: "[#TRAINING_OPENSEARCH]/rules/_search/template"
body:
id: "rule-with-name"
params:
rule: ${rule}
result: getRulesResult

mapRulesData:
call: http.post
args:
url: "[#TRAINING_DMAPPER]/hbs/training/get_rule_by_intent_id"
headers:
type: "json"
body:
hits: ${getRulesResult.response.body.hits.hits}
result: rulesData
# next: returnSuccess

returnSuccess:
return: ${rulesData.response.body}
next: end
33 changes: 10 additions & 23 deletions GUI/src/pages/Training/Intents/IntentDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ interface IntentResponse {
response: Intent;
}

interface RulesResponse {
response: Rule[];
interface RuleResponse {
response: { id: string };
}

interface IntentDetailsProps {
Expand Down Expand Up @@ -80,18 +80,6 @@ const IntentDetails: FC<IntentDetailsProps> = ({ intentId, setSelectedIntent, li
queryKey: [`intents/is-marked-for-service?intent=${intentId}`],
});

const addIntentRule = useCallback(
(rulesResponse: RulesResponse | undefined) => {
if (!rulesResponse) return;

const intentRule = rulesResponse.response.find((rule: Rule) => rule.id === `rule_${intentId}`);
if (intentRule) {
setIntentRule(intentRule.id);
}
},
[intentId]
);

const queryRefresh = useCallback(
async (intent?: string) => {
const intentsResponse = await queryClient.fetchQuery<IntentResponse>([
Expand All @@ -105,10 +93,10 @@ const IntentDetails: FC<IntentDetailsProps> = ({ intentId, setSelectedIntent, li
]);
setResponse(responseResponse.response);

const rulesResponse = await queryClient.fetchQuery<RulesResponse>(['rules']);
addIntentRule(rulesResponse);
const rulesResponse = await queryClient.fetchQuery<RuleResponse>([`rule-by-intent-id?intent=${intentId}`]);
setIntentRule(rulesResponse.response.id);
},
[addIntentRule, intentId, queryClient, setResponse, setSelectedIntent]
[intentId, queryClient, setResponse, setSelectedIntent]
);

const { data: responseResponse } = useQuery<ResponseResponse>({
Expand All @@ -122,14 +110,13 @@ const IntentDetails: FC<IntentDetailsProps> = ({ intentId, setSelectedIntent, li
const responseText = response?.text ?? '';
const responseName = response?.name ?? '';

// TODO: need to fetch rules for the selected intent only
const { data: rulesResponse } = useQuery<RulesResponse>({
queryKey: ['rules'],
const { data: rulesResponse } = useQuery<RuleResponse>({
queryKey: [`rule-by-intent-id?intent=${intentId}`],
});

useEffect(() => {
addIntentRule(rulesResponse);
}, [rulesResponse, addIntentRule]);
if (rulesResponse) setIntentRule(rulesResponse.response.id);
}, [rulesResponse]);

const markIntentServiceMutation = useMutation({
mutationFn: (data: { name: string; isForService: boolean }) => markForService(data),
Expand Down Expand Up @@ -462,7 +449,7 @@ const IntentDetails: FC<IntentDetailsProps> = ({ intentId, setSelectedIntent, li
},
onSuccess: async () => {
await queryClient.invalidateQueries([`intents/is-marked-for-service?intent=${intentId}`]);
await queryClient.invalidateQueries(['rules']);
await queryClient.invalidateQueries([`rule-by-intent-id?intent=${intentId}`]);
toast.open({
type: 'success',
title: t('global.notification'),
Expand Down

0 comments on commit 97bfef9

Please sign in to comment.