File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed
llama-index-core/llama_index/core Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change 28
28
SelectorPromptTemplate ,
29
29
)
30
30
from llama_index .core .prompts .prompt_utils import get_empty_prompt_txt
31
- from llama_index .core .prompts .utils import format_string
31
+ from llama_index .core .prompts .utils import format_content_blocks
32
32
from llama_index .core .schema import BaseComponent
33
33
from llama_index .core .utilities .token_counting import TokenCounter
34
34
@@ -198,9 +198,10 @@ def _get_available_chunk_size(
198
198
for message in messages :
199
199
partial_message = deepcopy (message )
200
200
201
+ # TODO: This does not count tokens in non-text blocks
201
202
prompt_kwargs = prompt .kwargs or {}
202
- partial_message .content = format_string (
203
- partial_message .content or "" , ** prompt_kwargs
203
+ partial_message .blocks = format_content_blocks (
204
+ partial_message .blocks , ** prompt_kwargs
204
205
)
205
206
206
207
# add to list of partial messages
Original file line number Diff line number Diff line change 2
2
import re
3
3
4
4
from llama_index .core .base .llms .base import BaseLLM
5
+ from llama_index .core .base .llms .types import ContentBlock , TextBlock
5
6
6
7
7
8
class SafeFormatter :
@@ -27,6 +28,21 @@ def format_string(string_to_format: str, **kwargs: str) -> str:
27
28
return formatter .format (string_to_format )
28
29
29
30
31
+ def format_content_blocks (
32
+ content_blocks : List [ContentBlock ], ** kwargs : str
33
+ ) -> List [ContentBlock ]:
34
+ """Format content blocks with kwargs."""
35
+ formatter = SafeFormatter (format_dict = kwargs )
36
+ formatted_blocks : List [ContentBlock ] = []
37
+ for block in content_blocks :
38
+ if isinstance (block , TextBlock ):
39
+ formatted_blocks .append (TextBlock (text = formatter .format (block .text )))
40
+ else :
41
+ formatted_blocks .append (block )
42
+
43
+ return formatted_blocks
44
+
45
+
30
46
def get_template_vars (template_str : str ) -> List [str ]:
31
47
"""Get template variables from a template string."""
32
48
variables = []
You can’t perform that action at this time.
0 commit comments