3
from typing import Dict, Any
6
def last_content(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
7
"""get the last content of the message list
9
:param data: the user llm request data
10
:type data: Dict[str, Any]
13
.. code-block:: python
15
from gptcache.processor.pre import last_content
17
content = last_content({"messages": [{"content": "foo1"}, {"content": "foo2"}]})
20
return data.get("messages")[-1]["content"]
23
def last_content_without_prompt(data: Dict[str, Any], **params: Dict[str, Any]) -> Any:
24
"""get the last content of the message list without prompts content
26
:param data: the user llm request data
27
:type data: Dict[str, Any]
28
:param params: the special gptcache params, like prompts param in the cache object
29
:type params: Dict[str, Any]
32
.. code-block:: python
34
from gptcache.processor.pre import last_content_without_prompt
36
content = last_content_without_prompt(
37
{"messages": [{"content": "foo1"}, {"content": "foo2"}]}, prompts=["foo"]
42
last_content_str = data.get("messages")[-1]["content"]
43
prompts = params.get("prompts", [])
45
return last_content_str
46
pattern = "|".join(prompts)
47
new_content_str = re.sub(pattern, "", last_content_str)
48
return new_content_str
51
def _get_pattern_value(pattern_str: str, value_str: str):
54
for literal_text, field_name, _, _ in string.Formatter().parse(pattern_str):
55
literal_text_arr.append(literal_text)
56
if field_name is not None:
57
field_name_arr.append(
58
field_name if field_name else str(len(field_name_arr))
63
for i, literal_text in enumerate(literal_text_arr):
64
start = value_str.find(literal_text, last_end)
65
if i == len(literal_text_arr) - 1:
68
end = value_str.find(literal_text_arr[i + 1], start + 1)
69
if start == -1 or end == -1:
71
start += len(literal_text)
72
pattern_values[field_name_arr[i]] = value_str[start:end]
77
def last_content_without_template(data: Dict[str, Any], **params: Dict[str, Any]) -> Any:
78
"""get the last content's template values of the message list without template content.
80
When considering a cache agent or chain, the majority of the content consists of template content,
81
while the essential information is simply a list of parameters within the template.
82
In this way, the cache key is composed of a string made up of all the parameter values in the list.
84
WARNING: Two parameters without intervals cannot appear in the template,
85
for example: template = "{foo}{hoo}" is not supported,
86
but template = "{foo}:{hoo}" is supported
88
:param data: the user llm request data
89
:type data: Dict[str, Any]
91
:Example with str template:
92
.. code-block:: python
94
from gptcache import Config
95
from gptcache.processor.pre import last_content_without_template
97
template_obj = "tell me a joke about {subject}"
98
prompt = template_obj.format(subject="animal")
99
value = last_content_without_template(
100
data={"messages": [{"content": prompt}]}, cache_config=Config(template=template_obj)
105
:Example with langchain template:
106
.. code-block:: python
108
from langchain import PromptTemplate
110
from gptcache import Config
111
from gptcache.processor.pre import last_content_without_template
113
template_obj = PromptTemplate.from_template("tell me a joke about {subject}")
114
prompt = template_obj.format(subject="animal")
116
value = last_content_without_template(
117
data={"messages": [{"content": prompt}]},
118
cache_config=Config(template=template_obj.template),
123
NOTE: At present, only the simple PromptTemplate in langchain is supported.
124
For ChatPromptTemplate, it needs to be adjusted according to the template array.
125
If you need to use it, you need to pass in the final dialog template yourself.
126
The reason why it cannot be advanced is that ChatPromptTemplate
127
does not provide a method to directly return the template string.
129
last_content_str = data.get("messages")[-1]["content"]
130
cache_config = params.get("cache_config", None)
131
if not (cache_config and cache_config.template):
132
return last_content_str
134
pattern_value = _get_pattern_value(cache_config.template, last_content_str)
135
return str(list(pattern_value.values()))
138
def all_content(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
139
"""get all content of the message list
141
:param data: the user llm request data
142
:type data: Dict[str, Any]
145
.. code-block:: python
147
from gptcache.processor.pre import all_content
149
content = all_content(
150
{"messages": [{"content": "foo1"}, {"content": "foo2"}]}
152
# content = "foo1\\nfoo2"
155
messages = data.get("messages")
156
for i, message in enumerate(messages):
157
if i == len(messages) - 1:
158
s += message["content"]
160
s += message["content"] + "\n"
164
def nop(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
165
"""do nothing of the llm request params
167
:param data: the user llm request data
168
:type data: Dict[str, Any]
171
.. code-block:: python
173
from gptcache.processor.pre import nop
175
content = nop({"str": "hello"})
181
def get_prompt(data: Dict[str, Any], **_: Dict[str, Any]) -> Any:
182
"""get the prompt of the llm request params
184
:param data: the user llm request data
185
:type data: Dict[str, Any]
188
.. code-block:: python
190
from gptcache.processor.pre import get_prompt
192
content = get_prompt({"prompt": "foo"})
195
return data.get("prompt")
198
def get_file_name(data: Dict[str, Any], **_: Dict[str, Any]) -> str:
199
"""get the file name of the llm request params
201
:param data: the user llm request data
202
:type data: Dict[str, Any]
205
.. code-block:: python
207
from gptcache.processor.pre import get_file_name
209
file = open("test.txt", "a")
210
content = get_file_name({"file": file})
213
return data.get("file").name
216
def get_file_bytes(data: Dict[str, Any], **_: Dict[str, Any]) -> bytes:
217
"""get the file bytes of the llm request params
219
:param data: the user llm request data
220
:type data: Dict[str, Any]
223
.. code-block:: python
225
from gptcache.processor.pre import get_file_bytes
227
content = get_file_bytes({"file": open("test.txt", "rb")})
229
return data.get("file").peek()
232
def get_input_str(data: Dict[str, Any], **_: Dict[str, Any]) -> str:
233
"""get the image and question str of the llm request params
235
:param data: the user llm request data
236
:type data: Dict[str, Any]
239
.. code-block:: python
241
from gptcache.processor.pre import get_input_str
243
content = get_input_str({"input": {"image": open("test.png", "rb"), "question": "foo"}})
245
input_data = data.get("input")
246
return str(input_data["image"].peek()) + input_data["question"]
249
def get_input_image_file_name(data: Dict[str, Any], **_: Dict[str, Any]) -> str:
250
"""get the image file name of the llm request params
252
:param data: the user llm request data
253
:type data: Dict[str, Any]
256
.. code-block:: python
258
from gptcache.processor.pre import get_input_image_file_name
260
content = get_input_image_file_name({"input": {"image": open("test.png", "rb")}})
263
input_data = data.get("input")
264
return input_data["image"].name
267
def get_image_question(data: Dict[str, Any], **_: Dict[str, Any]) -> str: # pragma: no cover
268
"""get the image and question str of the llm request params
270
:param data: the user llm request data
271
:type data: Dict[str, Any]
274
.. code-block:: python
276
from gptcache.processor.pre import get_image_question
278
content = get_image_question({"image": open("test.png", "rb"), "question": "foo"})
280
img = data.get("image")
281
data_img = str(open(img, "rb").peek()) if isinstance(img, str) else str(img) # pylint: disable=consider-using-with
282
return data_img + data.get("question")
285
def get_image(data: Dict[str, Any], **_: Dict[str, Any]) -> str: # pragma: no cover
286
"""get the image of the llm request params
288
:param data: the user llm request data
289
:type data: Dict[str, Any]
292
.. code-block:: python
294
from gptcache.processor.pre import get_image
296
content = get_image({"image": open("test.png", "rb")})
299
return data.get("image")
302
def get_inputs(data: Dict[str, Any], **_: Dict[str, Any]):
303
"""get the inputs of the llm request params
305
:param data: the user llm request data
306
:type data: Dict[str, Any]
309
.. code-block:: python
311
from gptcache.processor.pre import get_inputs
313
content = get_inputs({"inputs": "hello"})
316
return data.get("inputs")
319
def get_messages_last_content(data: Dict[str, Any], **_: Any) -> str:
320
""" get the last content of the llm request messages array
322
:param data: the user llm request data
323
:type data: Dict[str, Any]
326
.. code-block:: python
328
from gptcache.processor.pre import get_messages_last_content
330
content = get_messages_last_content({"messages": [{"content": "hello"}, {"content": "world"}]})
333
return data.get("messages")[-1].content
336
def get_openai_moderation_input(data: Dict[str, Any], **_: Dict[str, Any]) -> str:
337
"""get the input param of the openai moderation request params
339
:param data: the user openai moderation request data
340
:type data: Dict[str, Any]
343
.. code-block:: python
345
from gptcache.processor.pre import get_openai_moderation_input
347
content = get_openai_moderation_input({"input": ["hello", "world"]})
348
# "['hello', 'world']"
351
return str(data.get("input"))
354
def concat_all_queries(data: Dict[str, Any], **params: Dict[str, Any]) -> Any:
357
:param data: the user llm request data
358
:type data: Dict[str, Any]
361
.. code-block:: python
363
from gptcache.processor.pre import concat_all_queries
365
content = concat_all_queries({"messages": [{"role": "system", "content": "hello"},
366
{"role": "user", "content": "world"},
367
{"role": "assistant", "content": "alice"}]})
370
cache_config = params.get("cache_config", None)
371
skip_list = cache_config.skip_list
372
context_len = cache_config.context_len
373
context_len = context_len * 2
375
messages = data.get("messages")
376
length = min(context_len, len(messages))
377
messages = messages[len(messages) - length:]
378
for i, message in enumerate(messages):
379
if message["role"] in skip_list:
381
if i == len(messages) - 1:
382
s += f'{message["role"].upper()}: {message["content"]}'
384
s += f'{message["role"].upper()}: {message["content"]}\n'