code: from typing import Any, Callable, Optional, Sequence
  
  from llama_index.legacy.callbacks import CallbackManager
  from llama_index.legacy.core.llms.types import (
      ChatMessage,
      CompletionResponse,
      CompletionResponseGen,
      LLMMetadata,
  )
  from llama_index.legacy.llms.base import llm_completion_callback
  from llama_index.legacy.llms.custom import CustomLLM
  from llama_index.legacy.types import PydanticProgramMode
  
  
  class MockLLM(CustomLLM):
      max_tokens: Optional[int]
  
      def __init__(
          self,
          max_tokens: Optional[int] = None,
          callback_manager: Optional[CallbackManager] = None,
          system_prompt: Optional[str] = None,
          messages_to_prompt: Optional[Callable[[Sequence[ChatMessage]], str]] = None,
          completion_to_prompt: Optional[Callable[[str], str]] = None,
          pydantic_program_mode: PydanticProgramMode = PydanticProgramMode.DEFAULT,
      ) -> None:
          super().__init__(
              max_tokens=max_tokens,
              callback_manager=callback_manager,
              system_prompt=system_prompt,
              messages_to_prompt=messages_to_prompt,
              completion_to_prompt=completion_to_prompt,
              pydantic_program_mode=pydantic_program_mode,
          )
  
      @classmethod
      def class_name(cls) -> str:
          return "MockLLM"
  
      @property
      def metadata(self) -> LLMMetadata:
          return LLMMetadata(num_output=self.max_tokens or -1)
  
      def _generate_text(self, length: int) -> str:
          return " ".join(["text" for _ in range(length)])
  
      @llm_completion_callback()
      def complete(
          self, prompt: str, formatted: bool = False, **kwargs: Any
      ) -> CompletionResponse:
          response_text = (
              self._generate_text(self.max_tokens) if self.max_tokens else prompt
          )
  
          return CompletionResponse(
              text=response_text,
          )
  
      @llm_completion_callback()
      def stream_complete(
          self, prompt: str, formatted: bool = False, **kwargs: Any
      ) -> CompletionResponseGen:
          def gen_prompt() -> CompletionResponseGen:
              for ch in prompt:
                  yield CompletionResponse(
                      text=prompt,
                      delta=ch,
                  )
  
          def gen_response(max_tokens: int) -> CompletionResponseGen:
              for i in range(max_tokens):
                  response_text = self._generate_text(i)
                  yield CompletionResponse(
                      text=response_text,
                      delta="text ",
                  )
  
          return gen_response(self.max_tokens) if self.max_tokens else gen_prompt()
  
apis: llama_index.legacy.llms.base.llm_completion_callback llama_index.legacy.core.llms.types.LLMMetadata llama_index.legacy.core.llms.types.CompletionResponse
extract_api: [((1537, 1562), 'llama_index.legacy.llms.base.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (1560, 1562), False, 'from llama_index.legacy.llms.base import llm_completion_callback\n'), ((1876, 1901), 'llama_index.legacy.llms.base.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (1899, 1901), False, 'from llama_index.legacy.llms.base import llm_completion_callback\n'), ((1377, 1422), 'llama_index.legacy.core.llms.types.LLMMetadata', 'LLMMetadata', ([], {'num_output': '(self.max_tokens or -1)'}), '(num_output=self.max_tokens or -1)\n', (1388, 1422), False, 'from llama_index.legacy.core.llms.types import ChatMessage, CompletionResponse, CompletionResponseGen, LLMMetadata\n'), ((1808, 1846), 'llama_index.legacy.core.llms.types.CompletionResponse', 'CompletionResponse', ([], {'text': 'response_text'}), '(text=response_text)\n', (1826, 1846), False, 'from llama_index.legacy.core.llms.types import ChatMessage, CompletionResponse, CompletionResponseGen, LLMMetadata\n'), ((2128, 2169), 'llama_index.legacy.core.llms.types.CompletionResponse', 'CompletionResponse', ([], {'text': 'prompt', 'delta': 'ch'}), '(text=prompt, delta=ch)\n', (2146, 2169), False, 'from llama_index.legacy.core.llms.types import ChatMessage, CompletionResponse, CompletionResponseGen, LLMMetadata\n'), ((2415, 2468), 'llama_index.legacy.core.llms.types.CompletionResponse', 'CompletionResponse', ([], {'text': 'response_text', 'delta': '"""text """'}), "(text=response_text, delta='text ')\n", (2433, 2468), False, 'from llama_index.legacy.core.llms.types import ChatMessage, CompletionResponse, CompletionResponseGen, LLMMetadata\n')]