Skip to content

Commit a7138c5

Browse files
@W-21605883
SDK refactoring for function
1 parent 626ccf6 commit a7138c5

15 files changed

Lines changed: 386 additions & 26 deletions

File tree

src/datacustomcode/file/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
from __future__ import annotations
16+
from abc import abstractmethod
17+
from pathlib import Path
1618

1719

1820
class BaseDataAccessLayer:
19-
"""Base class for data access layer implementations."""
21+
def __init__(self):
22+
pass
23+
24+
@abstractmethod
25+
def find_file_path(self, file_name: str) -> Path: ...

src/datacustomcode/function_runtime/FunctionRuntime.py

Whitespace-only changes.

src/datacustomcode/llm_gateway/LLMGateway.py

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2025, Salesforce, Inc.
2+
# SPDX-License-Identifier: Apache-2
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.

src/datacustomcode/llm_gateway/base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@
1919
from datacustomcode.proxy.base import BaseProxyAccessLayer
2020

2121

22-
class BaseLLMGateway:
22+
class LLMGateway:
2323
def __init__(self):
2424
pass
2525

2626
@abstractmethod
2727
def generate_text(self, GenerateTextRequest) -> GenerateTextResponse: ...
28-
29-
@abstractmethod
30-
def llm_gateway_generate_text(
31-
self, template, values, llmModelId: str, maxTokens: int
32-
): ...
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2025, Salesforce, Inc.
2+
# SPDX-License-Identifier: Apache-2
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from datacustomcode.llm_gateway.base import LLMGateway
17+
from datacustomcode.llm_gateway.types.generate_text_request import GenerateTextRequest
18+
from datacustomcode.llm_gateway.types.generate_text_response import GenerateTextResponse
19+
20+
21+
class DefaultLLMGateway(LLMGateway):
22+
def generate_text(
23+
self,
24+
request: GenerateTextRequest
25+
) -> GenerateTextResponse:
26+
27+
28+
response_data = {
29+
'generation' : {'generatedText' : "I am dreaming!!"},
30+
}
31+
32+
return GenerateTextResponse(200, {"data": response_data})

src/datacustomcode/llm_gateway/types/GenerateTextRequest.py renamed to src/datacustomcode/llm_gateway/types/generate_text_request.py

File renamed without changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from datacustomcode.validator.base import Validator
2+
from datacustomcode.llm_gateway.types.generate_text_request import GenerateTextRequest
3+
4+
5+
class GenerateTextRequestValidator:
6+
@staticmethod
7+
def create_validator() -> Validator:
8+
"""Create a validator with all CEL rules for GenerateTextRequest"""
9+
validator = Validator()
10+
11+
# Rule 1: version == "v1" (CEL: const)
12+
validator.add_rule(
13+
id="request.version_v1",
14+
message="Platform currently only supports version 'v1'",
15+
expression=lambda this: this.version == "v1"
16+
)
17+
18+
# Rule 2: modelName.size() >= 1 (CEL: min_len)
19+
validator.add_rule(
20+
id="request.model_name_required",
21+
message="modelName must not be empty (min_len: 1)",
22+
expression=lambda this: len(this.model_name) >= 1
23+
)
24+
25+
return validator
26+
27+
class GenerateTextRequestBuilder:
28+
def __init__(self):
29+
self._validator = GenerateTextRequestValidator.create_validator()
30+
self._version = "v1" # Hardcoded default for your SDK
31+
self._prompt = ""
32+
self._model_name = ""
33+
34+
35+
def set_prompt(self, prompt: str):
36+
self._prompt = prompt
37+
return self
38+
39+
def set_model(self, model_name: str):
40+
self._model_name = model_name
41+
return self
42+
43+
def build(self) -> GenerateTextRequest:
44+
45+
request = GenerateTextRequest(
46+
version=self._version,
47+
prompt=self._prompt,
48+
model_name=self._model_name
49+
)
50+
51+
# 2. Run the Protovalidate check
52+
# This reads the 'max_len: 1000' rule from the .proto metadata
53+
violations = self._validator.validate(request)
54+
if violations:
55+
raise ValueError(f"Validation Error: {violations}")
56+
57+
return request

src/datacustomcode/llm_gateway/types/GenerateTextResponse.py renamed to src/datacustomcode/llm_gateway/types/generate_text_response.py

File renamed without changes.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
from dataclasses import dataclass
3+
from typing import Optional
4+
5+
import betterproto
6+
import grpclib
7+
8+
from .google import protobuf
9+
10+
11+
class GenerateTextResponseBuilder:
12+
def __init__(self):
13+
self._validator = Validator()
14+
self._rules = validate_pb2.MessageConstraints()
15+
16+
# Rule 1: Prompt Length
17+
prompt_rule = self.rules.cel.add()
18+
prompt_rule.id = "request.prompt_limit"
19+
prompt_rule.message = "Prompt must be 1-1000 characters."
20+
prompt_rule.expression = "this.prompt.size() > 0 && this.prompt.size() <= 1000"
21+
22+
# Rule 3: ModelName Constraint
23+
model_name_rule = self.rules.cel.add()
24+
version_rule.id = "request.version_v1"
25+
version_rule.message = "Platform currently only supports version 'v1'."
26+
version_rule.expression = "this.version == 'v1'"
27+
28+
self._version = "v1" # Hardcoded default for your SDK
29+
self._prompt = ""
30+
self._model_name = ""
31+
32+
33+
def validate(self, request: GenerateTextRequest):
34+
violations = self.validator.validate(request, constraints=self.rules)
35+
if violations:
36+
# protovalidate returns a structured 'Violations' object
37+
error_msg = "; ".join([v.message for v in violations.violations])
38+
raise ValueError(f"GenerateTextRequest Validation Failed: {error_msg}")
39+
40+
def set_prompt(self, prompt: str):
41+
self._prompt = prompt
42+
return self
43+
44+
def set_model(self, model_name: str):
45+
self._model_name = model_name
46+
return self
47+
48+
def build(self) -> GenerateTextRequest:
49+
50+
request = GenerateTextRequest(
51+
version=self._version,
52+
prompt=self._prompt,
53+
model_name=self._model_name
54+
)
55+
56+
# 2. Run the Protovalidate check
57+
# This reads the 'max_len: 1000' rule from the .proto metadata
58+
violations = _validator.validate(request)
59+
if violations:
60+
raise ValueError(f"Validation Error: {violations}")
61+
62+
return request

0 commit comments

Comments
 (0)