Skip to content

Commit 5267c49

Browse files
committed
AAP-32854: Decouple Views from Pipelines
1 parent 69b02b5 commit 5267c49

File tree

5 files changed

+93
-9
lines changed

5 files changed

+93
-9
lines changed

ansible_ai_connect/ai/api/eventbus/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright Red Hat
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from django.apps import apps
16+
from django.dispatch import Signal, receiver
17+
18+
from ansible_ai_connect.ai.api.model_pipelines.pipelines import (
19+
ChatBotParameters,
20+
ModelPipelineChatBot,
21+
)
22+
23+
chat_service = Signal()
24+
25+
26+
@receiver(chat_service)
27+
def chat_service_receiver(
28+
sender, conversation_id, req_query, req_system_prompt, req_model_id, req_provider, **kwargs
29+
):
30+
llm: ModelPipelineChatBot = apps.get_app_config("ai").get_model_pipeline(ModelPipelineChatBot)
31+
data = llm.invoke(
32+
ChatBotParameters.init(
33+
query=req_query,
34+
system_prompt=req_system_prompt,
35+
model_id=req_model_id or llm.config.model_id,
36+
provider=req_provider,
37+
conversation_id=conversation_id,
38+
)
39+
)
40+
return data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations
12+
13+
from enum import StrEnum
14+
15+
from ansible_ai_connect.ai.api.eventbus.sinks import chat_service
16+
17+
18+
class EventType(StrEnum):
19+
CHAT = "chat"
20+
21+
22+
class EventBus:
23+
24+
def send(self, event_type: EventType, **kwargs) -> any:
25+
data = None
26+
match event_type:
27+
case "chat":
28+
response = chat_service.send(
29+
event_type,
30+
conversation_id=kwargs["conversation_id"],
31+
req_query=kwargs["query"],
32+
req_system_prompt=kwargs["system_prompt"],
33+
req_model_id=kwargs["model_id"],
34+
req_provider=kwargs["provider"],
35+
)
36+
data = response[0][1]
37+
return data

ansible_ai_connect/ai/api/views.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from rest_framework.views import APIView
3333

3434
from ansible_ai_connect.ai.api.aws.exceptions import WcaSecretManagerError
35+
from ansible_ai_connect.ai.api.eventbus.source import EventBus, EventType
3536
from ansible_ai_connect.ai.api.exceptions import (
3637
BaseWisdomAPIException,
3738
ChatbotInvalidResponseException,
@@ -68,7 +69,6 @@
6869
WcaUserTrialExpired,
6970
)
7071
from ansible_ai_connect.ai.api.model_pipelines.pipelines import (
71-
ChatBotParameters,
7272
ContentMatchParameters,
7373
MetaData,
7474
ModelPipelineChatBot,
@@ -1004,14 +1004,14 @@ def post(self, request) -> Response:
10041004
self.event.conversation_id = conversation_id
10051005
self.event.modelName = self.req_model_id or self.llm.config.model_id
10061006

1007-
data = self.llm.invoke(
1008-
ChatBotParameters.init(
1009-
query=req_query,
1010-
system_prompt=req_system_prompt,
1011-
model_id=self.req_model_id or self.llm.config.model_id,
1012-
provider=req_provider,
1013-
conversation_id=conversation_id,
1014-
)
1007+
event_bus: EventBus = apps.get_app_config("ai").get_event_bus()
1008+
data = event_bus.send(
1009+
EventType.CHAT,
1010+
conversation_id=conversation_id,
1011+
query=req_query,
1012+
system_prompt=req_system_prompt,
1013+
model_id=self.req_model_id,
1014+
provider=req_provider,
10151015
)
10161016

10171017
response_serializer = ChatResponseSerializer(data=data)

ansible_ai_connect/ai/apps.py

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
)
3535

3636
from .api.aws.wca_secret_manager import AWSSecretManager, DummySecretManager
37+
from .api.eventbus.source import EventBus
3738

3839
logger = logging.getLogger(__name__)
3940

@@ -50,6 +51,7 @@ class AiConfig(AppConfig):
5051
_ansible_lint_caller = UNINITIALIZED
5152
_reports_postman = UNINITIALIZED
5253
_pipeline_factory = UNINITIALIZED
54+
_event_bus = UNINITIALIZED
5355

5456
def ready(self) -> None:
5557
self._pipeline_factory = ModelPipelineFactory()
@@ -58,6 +60,11 @@ def ready(self) -> None:
5860
def get_model_pipeline(self, feature: Type[PIPELINE_TYPE]) -> PIPELINE_TYPE:
5961
return self._pipeline_factory.get_pipeline(feature)
6062

63+
def get_event_bus(self):
64+
if self._event_bus is UNINITIALIZED:
65+
self._event_bus = EventBus()
66+
return self._event_bus
67+
6168
def get_ari_caller(self):
6269
# Django calls apps.ready() when registering INSTALLED_APPS
6370
# We can therefore guarantee self.model_mesh_client is not None

0 commit comments

Comments
 (0)