zilliztech / GPTCache

Semantic cache for LLMs. Fully integrated with LangChain and llama_index.
https://gptcache.readthedocs.io
MIT License
7.26k stars 507 forks source link

[Pre]: Bug fix _get_pattern_value() #645

Open Kirushikesh opened 2 months ago

Kirushikesh commented 2 months ago

When using the existing _get_pattern_value function for extracting the values i was getting the IndexError: list index out of range error.

Code to Reproduce:

from langchain import PromptTemplate
from gptcache.processor.pre import _get_pattern_value

template_obj = PromptTemplate.from_template("""
tell me a joke about {subject}:{object}
""")

prompt = template_obj.format(subject="animal",object = "dog")

result = _get_pattern_value(template_obj.template, prompt)
print(result)

Error Trace:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input [In [2]](vscode-notebook-cell:?execution_count=2), in <cell line: 10>()
      [4](vscode-notebook-cell:?execution_count=2&line=4) template_obj = PromptTemplate.from_template("""
      [5](vscode-notebook-cell:?execution_count=2&line=5) tell me a joke about {subject}:{object}
      [6](vscode-notebook-cell:?execution_count=2&line=6) """)
      [8](vscode-notebook-cell:?execution_count=2&line=8) prompt = template_obj.format(subject="animal",object = "dog")
---> [10](vscode-notebook-cell:?execution_count=2&line=10) result = _get_pattern_value(template_obj.template, prompt)
     [11](vscode-notebook-cell:?execution_count=2&line=11) print(result)

File /dccstor/kirushikesh/.conda/pdfrag/lib/python3.10/site-packages/gptcache/processor/pre.py:72, in _get_pattern_value(pattern_str, value_str)
     [70](https://vscode-remote+ssh-002dremote-002bcccxl012-002epok-002eibm-002ecom.vscode-resource.vscode-cdn.net/dccstor/kirushikesh/.conda/pdfrag/lib/python3.10/site-packages/gptcache/processor/pre.py:70)         break
     [71](https://vscode-remote+ssh-002dremote-002bcccxl012-002epok-002eibm-002ecom.vscode-resource.vscode-cdn.net/dccstor/kirushikesh/.conda/pdfrag/lib/python3.10/site-packages/gptcache/processor/pre.py:71)     start += len(literal_text)
---> [72](https://vscode-remote+ssh-002dremote-002bcccxl012-002epok-002eibm-002ecom.vscode-resource.vscode-cdn.net/dccstor/kirushikesh/.conda/pdfrag/lib/python3.10/site-packages/gptcache/processor/pre.py:72)     pattern_values[field_name_arr[i]] = value_str[start:end]
     [73](https://vscode-remote+ssh-002dremote-002bcccxl012-002epok-002eibm-002ecom.vscode-resource.vscode-cdn.net/dccstor/kirushikesh/.conda/pdfrag/lib/python3.10/site-packages/gptcache/processor/pre.py:73)     last_end = end
     [74](https://vscode-remote+ssh-002dremote-002bcccxl012-002epok-002eibm-002ecom.vscode-resource.vscode-cdn.net/dccstor/kirushikesh/.conda/pdfrag/lib/python3.10/site-packages/gptcache/processor/pre.py:74) return pattern_values

IndexError: list index out of range

The changes covers the bug fix, but the warning still holds

WARNING: Two parameters without intervals cannot appear in the template, for example: template = "{foo}{hoo}" is not supported, but template = "{foo}:{hoo}" is supported

sre-ci-robot commented 2 months ago

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Kirushikesh To complete the pull request process, please assign cxie after the PR has been reviewed. You can assign the PR to them by writing /assign @cxie in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files: - **[OWNERS](https://github.com/zilliztech/GPTCache/blob/main/OWNERS)** Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
sre-ci-robot commented 2 months ago

Welcome @Kirushikesh! It looks like this is your first PR to zilliztech/GPTCache 🎉