Skip to content

Commit bbcfb3a

Browse files
Fixing lint error
1 parent 13d65ac commit bbcfb3a

2 files changed

Lines changed: 24 additions & 26 deletions

File tree

src/datacustomcode/templates/function/example/chunking_with_llm/entrypoint.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,35 @@
99
- Automatic validation and conversion
1010
"""
1111

12-
from datacustomcode.function.runtime import Runtime
1312
import logging
1413

15-
logger = logging.getLogger(__name__)
16-
logging.basicConfig(level=logging.INFO)
17-
18-
19-
2014
from datacustomcode.function.feature_types.chunking import (
15+
ChunkType,
16+
SearchIndexChunkingV1Output,
2117
SearchIndexChunkingV1Request,
2218
SearchIndexChunkingV1Response,
23-
SearchIndexChunkingV1Output,
24-
ChunkType
19+
)
20+
from datacustomcode.function.runtime import Runtime
21+
from datacustomcode.llm_gateway.types.generate_text_request_builder import (
22+
GenerateTextRequestBuilder,
2523
)
2624

27-
from datacustomcode.llm_gateway.types.generate_text_request_builder import GenerateTextRequestBuilder
28-
25+
logger = logging.getLogger(__name__)
26+
logging.basicConfig(level=logging.INFO)
2927

30-
# Load prompt template once at module load time
31-
PROMPT_TEMPLATE = None
3228

3329
def _load_prompt_template(runtime: Runtime) -> str:
3430
"""Load the chunking prompt template from file."""
35-
global PROMPT_TEMPLATE
36-
if PROMPT_TEMPLATE is None:
37-
prompt_file = runtime.file.find_file_path("chunking_prompt.txt")
38-
with open(prompt_file, 'r') as f:
39-
PROMPT_TEMPLATE = f.read()
40-
logger.info(f"Loaded prompt template from {prompt_file}")
41-
return PROMPT_TEMPLATE
31+
prompt_file = runtime.file.find_file_path("chunking_prompt.txt")
32+
with open(prompt_file, "r") as f:
33+
_prompt_template_cache = f.read()
34+
logger.info(f"Loaded prompt template from {prompt_file}")
35+
return _prompt_template_cache
4236

4337

44-
def function(request: SearchIndexChunkingV1Request, runtime: Runtime) -> SearchIndexChunkingV1Response:
38+
def function(
39+
request: SearchIndexChunkingV1Request, runtime: Runtime
40+
) -> SearchIndexChunkingV1Response:
4541
"""
4642
Chunk documents for Search Index.
4743
@@ -64,22 +60,22 @@ def function(request: SearchIndexChunkingV1Request, runtime: Runtime) -> SearchI
6460
for doc_idx, doc in enumerate(request.input):
6561
# Direct field access - no wrappers!
6662
text = doc.text
67-
metadata = doc.metadata
6863

6964
# Use LLM to intelligently chunk the document
7065
# This creates semantic chunks that preserve context and meaning
7166
prompt = prompt_template.format(text=text)
7267

7368
builder = GenerateTextRequestBuilder()
74-
llm_request = builder.set_model("sfdc_ai__DefaultGPT4Turbo").set_prompt(prompt).build()
69+
llm_request = (
70+
builder.set_model("sfdc_ai__DefaultGPT4Turbo").set_prompt(prompt).build()
71+
)
7572
response = runtime.llm_gateway.generate_text(llm_request)
7673

7774
if response.is_success:
7875
# Parse LLM response to extract chunks
7976
llm_chunks = response.text.split("---CHUNK---")
8077
llm_chunks = [chunk.strip() for chunk in llm_chunks if chunk.strip()]
8178

82-
8379
# Create chunk outputs
8480
for chunk_text in llm_chunks:
8581
chunk = SearchIndexChunkingV1Output(
@@ -93,9 +89,11 @@ def function(request: SearchIndexChunkingV1Request, runtime: Runtime) -> SearchI
9389

9490
else:
9591
# LLM chunking failed - log error and raise exception
96-
error_msg = f"LLM chunking failed for document {doc_idx + 1}: {response.error_code}"
92+
error_msg = (
93+
f"LLM chunking failed for document {doc_idx + 1}: {response.error_code}"
94+
)
9795
logger.error(error_msg)
9896
raise RuntimeError(error_msg)
9997

10098
# Return Pydantic response
101-
return SearchIndexChunkingV1Response(output=chunks)
99+
return SearchIndexChunkingV1Response(output=chunks)

src/datacustomcode/templates/function/example/chunking_with_llm/files/chunking_prompt.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ Output format:
1616
---CHUNK---
1717
<chunk 2 text>
1818
---CHUNK---
19-
...
19+
...

0 commit comments

Comments
 (0)