-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix missing pipeline implementations.
- Loading branch information
Showing
6 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,110 @@ | ||
# Model Pipelines | ||
|
||
Ansible AI Connect is becoming feature rich. | ||
|
||
It supports API for the following features: | ||
- Code completions | ||
- Content match | ||
- Playbook Generation | ||
- Role Generation | ||
- Playbook Explanation | ||
- Chat Bot | ||
|
||
"Model Pipelines" provides a mechanism to support different _pipelines_ and configuration for each of these features for different providers. Different providers require different configuration information. | ||
|
||
## Pipelines | ||
|
||
A pipeline can exist for each feature for each type of provider. | ||
|
||
Types of provider are: | ||
- `grpc` | ||
- `http` | ||
- `dummy` | ||
- `wca` | ||
- `wca-onprem` | ||
- `wca-dummy` | ||
- `ollama` | ||
- `llamacpp` | ||
- `nop` | ||
|
||
### Implementing pipelines | ||
|
||
Implementations of a pipeline, for a particular provider, for a particular feature should extend the applicable base class; implementing the `invoke(..)` method accordingly: | ||
- `ModelPipelineCompletions` | ||
- `ModelPipelineContentMatch` | ||
- `ModelPipelinePlaybookGeneration` | ||
- `ModelPipelineRoleGeneration` | ||
- `ModelPipelinePlaybookExplanation` | ||
- `ModelPipelineChatBot` | ||
|
||
### Registering pipelines | ||
|
||
Implementations of pipelines, per provider, per feature are dynamically registered. To register a pipeline the implementing class should be decorated with `@Register(api_type="<type>")`. | ||
|
||
In addition to the supported features themselves implementations for the following must also be provided and registered: | ||
- `MetaData` | ||
|
||
A class providing basic meta-data for all features for the applicable provider. | ||
|
||
For example API Key, Model ID, Timeout etc. | ||
|
||
|
||
- `PipelineConfiguration` | ||
|
||
A class representing the pipelines configuration parameters. | ||
|
||
|
||
- `Serializer` | ||
|
||
A class that can deserialise configuration JSON/YAML into the target `PipelineConfiguration` class. | ||
|
||
### Default implementations | ||
|
||
A "No Operation" pipeline is registered by default for each provider and each feature where a concrete implementation is not explicitly available. | ||
|
||
### Lookup | ||
|
||
A registry is constructed at start-up, containing information of configured pipelines for all providers for all features. | ||
``` | ||
REGISTRY = { | ||
"http": { | ||
MetaData: <Implementing class>, | ||
ModelPipelineCompletions: <Implementing class> | ||
ModelPipelineContentMatch: <Implementing class> | ||
ModelPipelinePlaybookGeneration: <Implementing class> | ||
ModelPipelineRoleGeneration: <Implementing class> | ||
ModelPipelinePlaybookExplanation: <Implementing class> | ||
ModelPipelineChatBot: <Implementing class> | ||
PipelineConfiguration: <Implementing class> | ||
Serializer: <Implementing class> | ||
} | ||
... | ||
} | ||
``` | ||
|
||
To invoke a pipeline for a particular feature the instance for the configured provider can be retrieved from the `ai` Django application: | ||
``` | ||
pipeline: ModelPipelinePlaybookGeneration = | ||
apps | ||
.get_app_config("ai") | ||
.get_model_pipeline(ModelPipelinePlaybookGeneration) | ||
``` | ||
The pipeline can then be invoked: | ||
``` | ||
playbook, outline, warnings = pipeline.invoke( | ||
PlaybookGenerationParameters.init( | ||
request=request, | ||
text=self.validated_data["text"], | ||
custom_prompt=self.validated_data["customPrompt"], | ||
create_outline=self.validated_data["createOutline"], | ||
outline=self.validated_data["outline"], | ||
generation_id=self.validated_data["generationId"], | ||
model_id=self.req_model_id, | ||
) | ||
) | ||
``` | ||
The code is identical irrespective of which provider is configured. | ||
|
||
### Configuration | ||
|
||
Refer to the [examples](../../../../docs/config). |
This file contains 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 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 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 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
65 changes: 65 additions & 0 deletions
65
ansible_ai_connect/ai/api/model_pipelines/tests/test_default_pipelines.py
This file contains 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,65 @@ | ||
# Copyright Red Hat | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import json | ||
|
||
from django.test import override_settings | ||
|
||
from ansible_ai_connect.ai.api.model_pipelines.factory import ModelPipelineFactory | ||
from ansible_ai_connect.ai.api.model_pipelines.nop.pipelines import NopChatBotPipeline | ||
from ansible_ai_connect.ai.api.model_pipelines.pipelines import ( | ||
MetaData, | ||
ModelPipelineChatBot, | ||
) | ||
from ansible_ai_connect.ai.api.model_pipelines.registry import REGISTRY, REGISTRY_ENTRY | ||
from ansible_ai_connect.test_utils import WisdomServiceAPITestCaseBaseOIDC | ||
|
||
CHATBOT = { | ||
"ModelPipelineChatBot": { | ||
"provider": "dummy", | ||
}, | ||
} | ||
|
||
|
||
class TestDefaultModelPipelines(WisdomServiceAPITestCaseBaseOIDC): | ||
|
||
@override_settings(ANSIBLE_AI_MODEL_MESH_CONFIG="{}") | ||
def test_default_pipeline_when_not_defined(self): | ||
factory = ModelPipelineFactory() | ||
|
||
# The configuration is empty. All pipelines should fall back to the NOP variety | ||
pipelines = list(filter(lambda p: issubclass(p, MetaData), REGISTRY_ENTRY.keys())) | ||
for pipeline in pipelines: | ||
nop = REGISTRY["nop"][pipeline] | ||
with self.assertLogs(logger="root", level="INFO") as log: | ||
implementation = factory.get_pipeline(pipeline) | ||
self.assertIsNotNone(implementation) | ||
self.assertIsInstance(implementation, nop) | ||
self.assertInLog( | ||
f"Using NOP implementation for '{pipeline.__name__}'.", | ||
log, | ||
) | ||
|
||
@override_settings(ANSIBLE_AI_MODEL_MESH_CONFIG=json.dumps(CHATBOT)) | ||
def test_default_pipeline_when_not_implemented(self): | ||
factory = ModelPipelineFactory() | ||
|
||
# ChatBot is configured to use "dummy" however there is no "dummy" ChatBot implementation | ||
with self.assertLogs(logger="root", level="INFO") as log: | ||
pipeline = factory.get_pipeline(ModelPipelineChatBot) | ||
self.assertIsNotNone(pipeline) | ||
self.assertIsInstance(pipeline, NopChatBotPipeline) | ||
self.assertInLog( | ||
"Using NOP implementation for 'ModelPipelineChatBot'.", | ||
log, | ||
) |