Repository: LMCache/LMCache
File: lmcache/integration/vllm/vllm_v1_adapter.py (Python)

Code before:
        request: "Request",
        num_computed_tokens: int,
    ) -> Optional[int]:
        """
        Check for external KV cache hit.

        Args:
            request (Request): the request object.
            num_computed_tokens (int): the number of locally
                computed tokens for this request

        Returns:
            the number of tokens that can be loaded from the
            external KV cache beyond what is already computed.
        """
        if self.kv_role == "kv_producer" and not hasattr(
            self.lookup_client, "supports_producer_reuse"
        ):
            return 0

        self._requests_priority[request.request_id] = request.priority

        token_ids = request.prompt_token_ids

        # If the request has multimodal hashes, apply them to the token ids
        if request.mm_features or request.mm_hashes:
            # TODO(Jiayi): Optimize this
            token_ids = torch.tensor(request.prompt_token_ids)
            if request.mm_features:
                mm_hashes, mm_positions = zip(
                    *((f.identifier, f.mm_position) for f in request.mm_features)
                )
                apply_mm_hashes_to_token_ids(
                    token_ids,
                    list(mm_hashes),
                    list(mm_positions),
                )
            elif request.mm_hashes:
                apply_mm_hashes_to_token_ids(
                    token_ids, request.mm_hashes, request.mm_positions
                )
            token_ids = token_ids.tolist()

        request_configs = extract_request_configs(request.sampling_params)
        if self.skip_last_n_tokens > 0:
            token_ids = token_ids[: -self.skip_last_n_tokens]
        if self.async_loading:
            lookup_id = request.request_id
        else:
            lookup_id = str(uuid.uuid4())


Review comment (refactor):
Can we have a helper function called `extract_mm_features` that returns `Optional[Tuple[mm_hashes, mm_positions]]`?

So the code will be like:
```python
if mm_features := extract_mm_features(request):
    token_ids = torch.tensor(request.prompt_token_ids)
    mm_hashes, mm_positions = mm_features
    apply_mm_hashes_to_token_ids(.....)
```

Code after:
    def get_num_new_matched_tokens(
        self,
        request: "Request",
        num_computed_tokens: int,
    ) -> Optional[int]:
        """
        Check for external KV cache hit.

        Args:
            request (Request): the request object.
            num_computed_tokens (int): the number of locally
                computed tokens for this request

        Returns:
            the number of tokens that can be loaded from the
            external KV cache beyond what is already computed.
        """
        if self.kv_role == "kv_producer" and not hasattr(
            self.lookup_client, "supports_producer_reuse"
        ):
            return 0

        self._requests_priority[request.request_id] = request.priority

        token_ids = request.prompt_token_ids

        # If the request has multimodal hashes, apply them to the token ids
        mm_hashes, mm_positions = extract_mm_features(request)
        if mm_hashes and mm_positions:
            # TODO(Jiayi): Optimize this
            token_ids = torch.tensor(request.prompt_token_ids)
            apply_mm_hashes_to_token_ids(token_ids, mm_hashes, mm_positions)
            token_ids = token_ids.tolist()

        request_configs = extract_request_configs(request.sampling_params)
        if self.skip_last_n_tokens > 0:
            token_ids = token_ids[: -self.skip_last_n_tokens]
        if self.async_loading:
            lookup_id = request.request_id
        else:
            lookup_id = str(uuid.uuid4())

        self._lookup_requests_in_step.append(lookup_id)

        num_external_hit_tokens = self.lookup_client.lookup(
            token_ids,
            lookup_id=lookup_id,
            request_configs=request_configs,
        )

        if num_external_hit_tokens is None: