Files
doczyai-pipelines/fieldExtraction/tests/test_llm_utils.py
T
Praneel Panchigar ff400f4b1c Merged in feature/llm-utils-refactor (pull request #776)
Feature/llm utils refactor

* - Removed some temporary logging code
- Removed unused imports (uuid, datetime) from llm_utils.py
- Added cache token columns (cache_creation_tokens, cache_read_tokens) to CSV exports
- Added tracking for cache warming
- Updated usage_tracking.py to track cache tokens separately in data structure

All token tracking functionality remains intact.

* Split input tokens into fresh and cache in summary section

- Add total_fresh_input_tokens and total_cache_tokens to GLOBAL_USAGE
- Update summary CSV to include split token columns for better tractability
- Add averages for fresh_input_tokens and cache_tokens
- Maintain total_input_tokens = fresh_input_tokens + cache_tokens relationship
- All calculations verified and match per-file totals correctly

* upload files functionality

* Add comprehensive unit tests for usage_tracking module

- Implement Phase 1-4 tests covering all 10 functions in usage_tracking.py
- Add 98 test cases with parametrized tests for comprehensive coverage
- Fix extract_usage_from_response to handle None input gracefully
- Add python-dotenv dependency to pyproject.toml
- Tests cover: core functions, data access, export functions, and utilities
- All tests passing (98/98)

* Add S3 upload tests for main.py and fix f-string syntax error

- Add tests/test_investment_main_s3.py with 3 test cases:
  * test_main_writes_final_and_error_to_s3: validates both final and error DataFrames uploaded when processing has errors
  * test_main_writes_only_final_when_no_errors: validates only final DataFrame uploaded when all files succeed
  * test_main_writes_usage_when_present: validates usage tracking files uploaded when usage data exists
- Tests use monkeypatching and module stubs to isolate S3 write logic without external dependencies
- Fix syntax error in src/codes/code_funcs.py: nested f-string quotes (level_dict['level_suffix'])
- All tests passing

* Add comprehensive test for usage_tracking.get_usage_dataframes S3 uploads

- Add test_usage_dataframes_written_to_s3 to validate per_file_usage_df and batch_summary_df
- Test verifies both dataframes from usage_tracking.get_usage_dataframes() are written to S3
- Validates 'usage' suffix writes per_file_usage_df with correct schema (file_name, tokens, cost)
- Validates 'usage_summary' suffix writes batch_summary_df with correct schema (totals, averages, region_mode)
- Uses pd.testing.assert_frame_equal to ensure exact dataframes are uploaded
- Fix module caching issue between tests by clearing sys.modules cache
- All 4 tests passing

* Add cleanup fixture to prevent test pollution

- Add autouse pytest fixture to clean up stubbed modules after each test
- Remove manual cache clearing from individual tests (now handled by fixture)
- Prevents our module stubs from affecting other test files in CI/CD pipeline
- Fixes Bitbucket pipeline failures where other tests couldn't find llm_utils attributes

* Refactor tests: add comprehensive io_utils coverage; disable usage_tracking suite

- Added 22 focused tests for write_local and write_s3 plus existing IO behaviors
- Introduced FakeS3Client to avoid boto3 network/credential dependency
- Added preserve_config fixture (no monkeypatch) to isolate config side effects
- Marked test_usage_tracking.py skipped per new consolidation approach
- Verified 26 tests (io_utils + investment_main) all pass locally without AWS exceptions

* Fix S3_CLIENT mocking: use mocker.patch instead of direct assignment

- Changed preserve_config fixture to NOT save/restore S3_CLIENT
- Updated all 5 write_s3 tests to use mocker.patch('src.config.S3_CLIENT', fake)
- This prevents pollution of config.S3_CLIENT across test modules
- Fixes NoCredentialsError in other tests that were importing config after our tests set FakeS3Client
- All 22 io_utils tests pass + 4 investment_main_s3 tests pass
- test_llm_utils::test_invoke_claude_local_mode now passes

* Add tests for write_s3 function

* Resolve merge conflict: keep mocker-based write_s3 tests

* Remove rogue skip mark from test_usage_tracking.py

The skip mark was incorrectly added by remote branch claiming tests were
migrated to io_utils. However, test_io_utils.py only tests io_utils functions
(read/write operations), not usage_tracking functions.

Restore the comprehensive usage_tracking tests from commit 26a556f0 which
includes 98 test cases covering all 10 functions in usage_tracking.py.

* Fix test pollution: add config cleanup fixture and restore RUN_MODE

- Add autouse fixture in test_io_utils.py to restore config values after each test
- Fix test_llm_utils.py to restore RUN_MODE after test_invoke_claude_local_mode
- Prevents config patches from leaking into subsequent tests causing NoCredentialsError

* Remove redundant test_investment_main_s3.py

- File was testing same write_s3 functionality already covered in test_io_utils.py
- Was causing module pollution by stubbing src.utils.llm_utils
- Caused test failures in other test files due to module cache issues
- Integration testing was minimal and mocked write_s3 anyway

* Phase 1: Remove unused code and align cost constants

- Remove count_tokens() function (unused, replaced by exact API counts)
- Remove unused cost constants from invoke_claude() and invoke_claude_with_image_array()
- Remove unused math import
- Update usage_tracking.py to match correct cost constants from llm_utils.py:
  - Corrected Haiku cache_read: 0.000025 -> 0.00003
  - Implemented correct cache creation cost calculation:
    * Sonnet models: 25% higher than input (0.00375 vs 0.003)
    * Haiku: 20% higher than input (0.0003 vs 0.00025)
- Remove test_count_tokens() test
- Update test_track_usage_single_call() to account for corrected cache creation costs

All 437 tests passing. Token counts verified against previous runs.

* Phase 2: Remove redundant update_stats() tracking

- Remove all update_stats() calls from EC2 mode (3 locations)
- Remove update_stats() function definition
- Remove estimated token counting (tokens_sent, tokens_received)
- Remove elapsed_time calculations
- Remove unused start_time assignments
- Remove test_update_stats() test case
- Clean up setUp() method in test_llm_utils.py
- Update docstrings to reflect usage_tracking.py as source of truth

All token tracking now uses exact API counts via usage_tracking.track_usage().
All 436 tests passing.

* Merge main into feature/llm-utils-refactor

Resolved merge conflicts:
- llm_utils.py: Kept refactored code (removed legacy update_stats, math import)
- usage_tracking.py: Kept corrected cost constants and cache creation pricing logic
- io_utils.py: Merged main's new 'dtc' and 'dtc_summary' output types
- test_usage_tracking.py: Kept corrected test values for cache_read and cache_creation costs

All conflicts resolved while preserving Phase 1 & 2 refactoring work.

* Fix merge conflict artifact in test_usage_tracking.py

Removed leftover merge conflict marker (<<<<<<< HEAD) that was missed
during merge resolution. The test now correctly calculates cache creation
costs for Sonnet (25% higher) and Haiku (20% higher) models.

* Phase 3: Add thread safety and enhance cache key generation

Step 3.1: Add thread safety to cache operations
- Added _cache_lock = threading.Lock() at module level
- Wrapped all cache operations (get, set, move_to_end, popitem) with lock
- All 4 cache operation locations now thread-safe:
  * Cache check/retrieval in invoke_claude()
  * Cache storage in invoke_claude()
  * Cache check/retrieval in invoke_claude_with_image_array()
  * Cache storage in invoke_claude_with_image_array()

Step 3.2: Enhance cache key generation
- Updated get_cache_key() to include instruction, max_tokens, and cache flag
- Updated get_cache_key_with_image_array() with same enhancements
- Cache keys now differentiate based on all relevant parameters
- Backward compatible (all new parameters are optional)

Step 3.3: Update call sites
- Updated invoke_claude() to pass instruction, max_tokens, cache to get_cache_key()
- Updated invoke_claude_with_image_array() to pass max_tokens

All 412 tests passing. Cache operations are now thread-saf…
* Phase 4: Add comprehensive tests for cache operations and token tracking

Step 4.1: Remove legacy test cases
- Already completed in Phase 2 (test_update_stats, test_count_tokens removed)
- setUp() cleaned up (no MODEL_STATS/GLOBAL_STATS setup needed)

Step 4.2: Add tests for thread-safe cache operations
- test_cache_thread_safety_concurrent_reads: 10 concurrent cache reads
- test_cache_thread_safety_concurrent_writes: 10 concurrent cache writes
- test_cache_thread_safety_mixed_operations: mixed read/write operations

Step 4.3: Add tests for enhanced cache key generation
- test_get_cache_key_basic: backward compatibility test
- test_get_cache_key_with_instruction: instruction parameter
- test_get_cache_key_with_max_tokens: max_tokens parameter
- test_get_cache_key_with_cache_flag: cache flag
- test_get_cache_key_all_parameters: all parameters together
- test_get_cache_key_with_image_array: image array variant
- test_invoke_claude_uses_enhanced_cache_key: integration test

Step 4.4: Verify token tracking uses e…
* Phase 5: Replace legacy CSV export with usage_tracking.py

Step 5.1: Replace write_stats_to_csv() implementation
- Updated write_stats_to_csv() to use usage_tracking.get_usage_dataframes() instead of config.MODEL_STATS/GLOBAL_STATS
- Added import for usage_tracking module
- Function now reads from usage_tracking.py as the single source of truth

Step 5.2: Add optional run_id and timestamp parameters
- Added optional run_id parameter (defaults to config.BATCH_ID)
- Added optional timestamp parameter (defaults to current timestamp)
- Updated upload_tracking_logs() to pass batch_id and run_timestamp

Step 5.3: Transform usage_tracking format to legacy CSV format
- Aggregates per-file, per-model data to per-file data
- Maps model names to Claude 2/3/3.5 request counts
- Calculates tokens_sent (fresh input tokens) and tokens_received (output tokens)
- Maintains backward compatibility with legacy CSV structure
- Handles empty data gracefully (writes headers-only CSV)

Step 5.4: Verify call sites work with updated f…
* refactor(llm_utils): extract helper functions and reduce code duplication

- Add private helper functions: _track_usage_from_response(), _build_claude_3_request_body(), _store_in_cache()
- Simplify routing logic using model sets (_CLAUDE_2_MODELS, _CLAUDE_3_AND_UP_MODELS)
- Replace 8 instances of usage tracking pattern with single function call
- Replace 3 instances of request body building with helper function
- Replace 2 instances of cache storage with helper function
- Add Claude 4.5 Sonnet to cache support list
- Reduce file size from 1,004 to 860 lines (14.3% reduction, 144 lines removed)
- All tests passing (422 tests)
- Maintains backward compatibility and functionality

* Re-support Claude 3 Haiku and add comprehensive tests for new helper functions

- Re-added Claude 3 Haiku to supported models (removed from deprecated patterns)
- Updated model validation to include Haiku in supported models list
- Added comprehensive test coverage for new helper functions:
  - _validate_model_support() - 5 tests covering deprecation, support, and context
  - _supports_prompt_cache() - 2 tests for cache support detection
  - _build_claude_3_request_body() - 4 tests for request body building with/without cache
- Updated error messages to reflect Claude 3+ models (Haiku, Sonnet 3.5/3.7/4/4.5)
- All 23 tests passing

* Merged main into feature/llm-utils-refactor

* Merged main into feature/llm-utils-refactor


Approved-by: Siddhant Medar
Approved-by: Katon Minhas
2025-11-17 14:15:46 +00:00

429 lines
18 KiB
Python

import hashlib
import threading
import unittest
from unittest.mock import mock_open, patch
import src.utils.llm_utils as llm_utils
from src import config
class TestLLMUtils(unittest.TestCase):
def setUp(self):
self.filename = "test_file.txt"
self.prompt = "This is a test prompt."
self.response = "This is a test response."
# Clear cache before each test
llm_utils.claude_cache.clear()
def tearDown(self):
# Clear cache after each test
llm_utils.claude_cache.clear()
def test_get_cache_key_basic(self):
"""Test basic cache key generation (backward compatible)."""
model_id = "test_model"
expected_hash = hashlib.sha256(f"{model_id}:{self.prompt}".encode()).hexdigest()
self.assertEqual(llm_utils.get_cache_key(self.prompt, model_id), expected_hash)
def test_get_cache_key_with_instruction(self):
"""Test cache key includes instruction parameter."""
model_id = "test_model"
instruction = "You are a helpful assistant."
key1 = llm_utils.get_cache_key(self.prompt, model_id, instruction=instruction)
key2 = llm_utils.get_cache_key(self.prompt, model_id, instruction="Different instruction")
# Keys should be different when instruction differs
self.assertNotEqual(key1, key2)
# Same instruction should produce same key
key3 = llm_utils.get_cache_key(self.prompt, model_id, instruction=instruction)
self.assertEqual(key1, key3)
def test_get_cache_key_with_max_tokens(self):
"""Test cache key includes max_tokens parameter."""
model_id = "test_model"
key1 = llm_utils.get_cache_key(self.prompt, model_id, max_tokens=100)
key2 = llm_utils.get_cache_key(self.prompt, model_id, max_tokens=200)
# Keys should be different when max_tokens differs
self.assertNotEqual(key1, key2)
# Same max_tokens should produce same key
key3 = llm_utils.get_cache_key(self.prompt, model_id, max_tokens=100)
self.assertEqual(key1, key3)
def test_get_cache_key_with_cache_flag(self):
"""Test cache key includes cache flag."""
model_id = "test_model"
key1 = llm_utils.get_cache_key(self.prompt, model_id, cache=False)
key2 = llm_utils.get_cache_key(self.prompt, model_id, cache=True)
# Keys should be different when cache flag differs
self.assertNotEqual(key1, key2)
def test_get_cache_key_all_parameters(self):
"""Test cache key with all parameters."""
model_id = "test_model"
instruction = "You are helpful"
max_tokens = 100
key1 = llm_utils.get_cache_key(
self.prompt, model_id, instruction=instruction, max_tokens=max_tokens, cache=True
)
key2 = llm_utils.get_cache_key(
self.prompt, model_id, instruction=instruction, max_tokens=max_tokens, cache=True
)
# Same parameters should produce same key
self.assertEqual(key1, key2)
# Different parameters should produce different keys
key3 = llm_utils.get_cache_key(
self.prompt, model_id, instruction="Different", max_tokens=max_tokens, cache=True
)
self.assertNotEqual(key1, key3)
def test_get_cache_key_with_image_array(self):
"""Test cache key generation for image arrays."""
model_id = "test_model"
image_list = ["base64_image1", "base64_image2"]
key1 = llm_utils.get_cache_key_with_image_array(
self.prompt, model_id, image_list, max_tokens=100
)
key2 = llm_utils.get_cache_key_with_image_array(
self.prompt, model_id, image_list, max_tokens=200
)
# Keys should be different when max_tokens differs
self.assertNotEqual(key1, key2)
# Same parameters should produce same key
key3 = llm_utils.get_cache_key_with_image_array(
self.prompt, model_id, image_list, max_tokens=100
)
self.assertEqual(key1, key3)
def test_cache_thread_safety_concurrent_reads(self):
"""Test thread safety with concurrent cache reads."""
model_id = "test_model"
cache_key = llm_utils.get_cache_key(self.prompt, model_id)
# Pre-populate cache
llm_utils.claude_cache[cache_key] = self.response
results = []
errors = []
def read_cache():
try:
# Simulate cache read
with llm_utils._cache_lock:
if cache_key in llm_utils.claude_cache:
result = llm_utils.claude_cache[cache_key]
results.append(result)
except Exception as e:
errors.append(e)
# Create multiple threads reading from cache
threads = []
for _ in range(10):
thread = threading.Thread(target=read_cache)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
# Verify no errors and all reads succeeded
self.assertEqual(len(errors), 0)
self.assertEqual(len(results), 10)
self.assertTrue(all(r == self.response for r in results))
def test_cache_thread_safety_concurrent_writes(self):
"""Test thread safety with concurrent cache writes."""
model_id = "test_model"
num_threads = 10
def write_to_cache(thread_id):
prompt = f"prompt_{thread_id}"
response = f"response_{thread_id}"
cache_key = llm_utils.get_cache_key(prompt, model_id)
# Simulate cache write with lock
with llm_utils._cache_lock:
if len(llm_utils.claude_cache) >= llm_utils.CACHE_LIMIT:
llm_utils.claude_cache.popitem(last=False)
llm_utils.claude_cache[cache_key] = response
llm_utils.claude_cache.move_to_end(cache_key)
threads = []
for i in range(num_threads):
thread = threading.Thread(target=write_to_cache, args=(i,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
# Verify all writes succeeded (cache should have entries)
self.assertGreater(len(llm_utils.claude_cache), 0)
self.assertLessEqual(len(llm_utils.claude_cache), num_threads)
def test_cache_thread_safety_mixed_operations(self):
"""Test thread safety with mixed read/write operations."""
model_id = "test_model"
initial_key = llm_utils.get_cache_key("initial_prompt", model_id)
llm_utils.claude_cache[initial_key] = "initial_response"
read_count = [0]
write_count = [0]
def read_operation():
with llm_utils._cache_lock:
if initial_key in llm_utils.claude_cache:
_ = llm_utils.claude_cache[initial_key]
read_count[0] += 1
def write_operation(thread_id):
prompt = f"write_prompt_{thread_id}"
response = f"write_response_{thread_id}"
cache_key = llm_utils.get_cache_key(prompt, model_id)
with llm_utils._cache_lock:
if len(llm_utils.claude_cache) >= llm_utils.CACHE_LIMIT:
llm_utils.claude_cache.popitem(last=False)
llm_utils.claude_cache[cache_key] = response
write_count[0] += 1
threads = []
# Mix of read and write threads
for i in range(5):
threads.append(threading.Thread(target=read_operation))
for i in range(5):
threads.append(threading.Thread(target=write_operation, args=(i,)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
# Verify operations completed without errors
self.assertEqual(read_count[0], 5)
self.assertEqual(write_count[0], 5)
@patch("builtins.open", new_callable=mock_open)
def test_invoke_claude_local_mode(self, mock_open_file):
"""Test invoke_claude in local mode."""
original_run_mode = config.RUN_MODE
try:
config.RUN_MODE = "local"
model_id = config.MODEL_ID_CLAUDE35_SONNET
with patch("src.utils.llm_utils.local_claude_3_and_up", return_value=self.response):
response = llm_utils.invoke_claude(self.prompt, model_id, self.filename)
self.assertEqual(response, self.response)
finally:
config.RUN_MODE = original_run_mode
@patch("builtins.open", new_callable=mock_open)
def test_invoke_claude_uses_enhanced_cache_key(self, mock_open_file):
"""Test that invoke_claude uses enhanced cache key with all parameters."""
original_run_mode = config.RUN_MODE
try:
config.RUN_MODE = "local"
model_id = config.MODEL_ID_CLAUDE35_SONNET
instruction = "Test instruction"
max_tokens = 100
cache = True
with patch("src.utils.llm_utils.local_claude_3_and_up", return_value=self.response):
# First call should not use cache (cache is empty)
response1 = llm_utils.invoke_claude(
self.prompt, model_id, self.filename,
max_tokens=max_tokens, cache=cache, instruction=instruction
)
# Second call with same parameters should use cache
with patch("src.utils.llm_utils.local_claude_3_and_up") as mock_local:
response2 = llm_utils.invoke_claude(
self.prompt, model_id, self.filename,
max_tokens=max_tokens, cache=cache, instruction=instruction
)
# Should not call local_claude_3_and_up again (cache hit)
mock_local.assert_not_called()
self.assertEqual(response1, response2)
finally:
config.RUN_MODE = original_run_mode
llm_utils.claude_cache.clear()
def test_token_tracking_uses_exact_api_counts(self):
"""Test that token tracking uses exact API response counts, not estimates."""
# This test verifies that we're using usage_tracking.extract_usage_from_response()
# which gets exact counts from API, not estimated counts like len(prompt.split())
# Mock API response with exact token counts
mock_response_body = {
"usage": {
"input_tokens": 150,
"output_tokens": 75,
"cache_creation_input_tokens": 20,
"cache_read_input_tokens": 30,
}
}
# Verify extract_usage_from_response extracts exact counts
import src.utils.usage_tracking as usage_tracking
input_tokens, output_tokens, cache_creation, cache_read = (
usage_tracking.extract_usage_from_response(mock_response_body)
)
# Should match exact API response, not estimates
self.assertEqual(input_tokens, 150)
self.assertEqual(output_tokens, 75)
self.assertEqual(cache_creation, 20)
self.assertEqual(cache_read, 30)
# Verify these are not estimated (e.g., len(prompt.split()))
# Exact counts from API are integers, not based on string length
self.assertIsInstance(input_tokens, int)
self.assertIsInstance(output_tokens, int)
def test_validate_model_support_deprecated_claude_2(self):
"""Test that deprecated Claude 2 models raise ValueError."""
with self.assertRaises(ValueError) as context:
llm_utils._validate_model_support(
"anthropic.claude-instant-v1",
"anthropic.claude-instant-v1"
)
self.assertIn("Claude 2 is no longer supported", str(context.exception))
def test_validate_model_support_claude_3_haiku_supported(self):
"""Test that Claude 3 Haiku models are supported."""
# Should not raise an exception
try:
llm_utils._validate_model_support(
config.MODEL_ID_CLAUDE3_HAIKU,
config.MODEL_ID_CLAUDE3_HAIKU
)
except ValueError:
self.fail("_validate_model_support raised ValueError for supported Claude 3 Haiku model")
def test_validate_model_support_supported_model(self):
"""Test that supported models pass validation."""
# Should not raise an exception
try:
llm_utils._validate_model_support(
config.MODEL_ID_CLAUDE35_SONNET,
config.MODEL_ID_CLAUDE35_SONNET
)
except ValueError:
self.fail("_validate_model_support raised ValueError for supported model")
def test_validate_model_support_unsupported_model(self):
"""Test that unsupported models raise ValueError."""
with self.assertRaises(ValueError) as context:
llm_utils._validate_model_support(
"unknown.model.id",
"unknown.model.id"
)
self.assertIn("Only Claude 3+ models are supported", str(context.exception))
self.assertIn("Haiku", str(context.exception))
def test_validate_model_support_with_context(self):
"""Test that context parameter is included in error messages."""
with self.assertRaises(ValueError) as context:
llm_utils._validate_model_support(
"unknown.model.id",
"unknown.model.id",
context="for image array"
)
self.assertIn("for image array", str(context.exception))
def test_supports_prompt_cache_supported_models(self):
"""Test that models supporting prompt cache return True."""
# Test Claude 3.7 Sonnet
if config.MODEL_ID_CLAUDE37_SONNET:
self.assertTrue(llm_utils._supports_prompt_cache(config.MODEL_ID_CLAUDE37_SONNET))
# Test Claude 4 Sonnet
if config.MODEL_ID_CLAUDE4_SONNET:
self.assertTrue(llm_utils._supports_prompt_cache(config.MODEL_ID_CLAUDE4_SONNET))
# Test Claude 4.5 Sonnet
if config.MODEL_ID_CLAUDE45_SONNET:
self.assertTrue(llm_utils._supports_prompt_cache(config.MODEL_ID_CLAUDE45_SONNET))
def test_supports_prompt_cache_unsupported_models(self):
"""Test that models not supporting prompt cache return False."""
# Claude 3.5 Sonnet v1 doesn't support caching (only v2+)
if config.MODEL_ID_CLAUDE35_SONNET:
self.assertFalse(llm_utils._supports_prompt_cache(config.MODEL_ID_CLAUDE35_SONNET))
# Unknown model
self.assertFalse(llm_utils._supports_prompt_cache("unknown.model.id"))
def test_build_claude_3_request_body_basic(self):
"""Test building request body without instruction."""
body = llm_utils._build_claude_3_request_body(
"test prompt", max_tokens=100
)
self.assertEqual(body["anthropic_version"], "bedrock-2023-05-31")
self.assertEqual(body["max_tokens"], 100)
self.assertEqual(body["temperature"], 0.0)
self.assertEqual(len(body["messages"]), 1)
self.assertEqual(body["messages"][0]["role"], "user")
self.assertNotIn("system", body)
def test_build_claude_3_request_body_with_instruction_no_cache(self):
"""Test building request body with instruction but no caching."""
instruction = "You are a helpful assistant."
body = llm_utils._build_claude_3_request_body(
"test prompt", max_tokens=100, instruction=instruction, cache=False
)
self.assertEqual(body["system"], instruction)
self.assertNotIsInstance(body["system"], list)
def test_build_claude_3_request_body_with_cache_supported_model(self):
"""Test building request body with caching enabled for supported model."""
instruction = "You are a helpful assistant."
model_id = config.MODEL_ID_CLAUDE37_SONNET or config.MODEL_ID_CLAUDE4_SONNET
if model_id and llm_utils._supports_prompt_cache(model_id):
body = llm_utils._build_claude_3_request_body(
"test prompt", max_tokens=100, instruction=instruction,
cache=True, model_id=model_id
)
# Should use array format with cache_control
self.assertIsInstance(body["system"], list)
self.assertEqual(len(body["system"]), 1)
self.assertEqual(body["system"][0]["type"], "text")
self.assertEqual(body["system"][0]["text"], instruction)
self.assertEqual(body["system"][0]["cache_control"]["type"], "ephemeral")
def test_build_claude_3_request_body_with_cache_unsupported_model(self):
"""Test building request body with caching enabled for unsupported model."""
instruction = "You are a helpful assistant."
model_id = config.MODEL_ID_CLAUDE35_SONNET # v1 doesn't support caching
if model_id:
body = llm_utils._build_claude_3_request_body(
"test prompt", max_tokens=100, instruction=instruction,
cache=True, model_id=model_id
)
# Should use simple string format (no cache_control)
self.assertEqual(body["system"], instruction)
self.assertNotIsInstance(body["system"], list)
if __name__ == "__main__":
unittest.main()