code: from llama_index.llms.ollama import Ollama
  
  from typing import Any, Sequence
  
  
  from llama_index.core.bridge.pydantic import Field
  from llama_index.core.base.llms.types import (
      ChatMessage,
      ChatResponseGen,
      CompletionResponse,
      CompletionResponseGen,
  )
  from llama_index.core.llms.callbacks import llm_chat_callback, llm_completion_callback
  
  class Ollama(Ollama):
      system: str = Field(
          default="", description="Default system message to send to the model."
      )
      keep_alive: int = Field(
          default=0,
          description="Time, in minutes, to wait before unloading model.",
      )
  
      request_timeout = 120.0
  
  
      @llm_chat_callback()
      def chat(self, messages: Sequence[ChatMessage], **kwargs: Any):
          if self.system and len(messages) > 0 and messages[0].role != "system":
              messages.insert(
                  0, ChatMessage(role="system", content=self.system)
              )
          kwargs["keep_alive"] = self.keep_alive
          return super().chat(messages, **kwargs)
      
      @llm_chat_callback()
      def stream_chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponseGen:
          if self.system and len(messages) > 0 and messages[0].role != "system":
              messages.insert(
                  0, ChatMessage(role="system", content=self.system)
              )
          kwargs["keep_alive"] = self.keep_alive
          yield from super().stream_chat(messages, **kwargs)
      
      @llm_completion_callback()
      def complete(self, prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponse:
          if self.system:
              kwargs["system"] = self.system
          kwargs["keep_alive"] = self.keep_alive
          return super().complete(prompt, formatted, **kwargs)
      
      @llm_completion_callback()
      def stream_complete(self, prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponseGen:
          if self.system:
              kwargs["system"] = self.system
          kwargs["keep_alive"] = self.keep_alive
          yield from super().stream_complete(prompt, formatted, **kwargs)
apis: llama_index.core.llms.callbacks.llm_completion_callback llama_index.core.llms.callbacks.llm_chat_callback llama_index.core.base.llms.types.ChatMessage llama_index.core.bridge.pydantic.Field
extract_api: [((396, 473), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default': '""""""', 'description': '"""Default system message to send to the model."""'}), "(default='', description='Default system message to send to the model.')\n", (401, 473), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((510, 596), 'llama_index.core.bridge.pydantic.Field', 'Field', ([], {'default': '(0)', 'description': '"""Time, in minutes, to wait before unloading model."""'}), "(default=0, description=\n    'Time, in minutes, to wait before unloading model.')\n", (515, 596), False, 'from llama_index.core.bridge.pydantic import Field\n'), ((651, 670), 'llama_index.core.llms.callbacks.llm_chat_callback', 'llm_chat_callback', ([], {}), '()\n', (668, 670), False, 'from llama_index.core.llms.callbacks import llm_chat_callback, llm_completion_callback\n'), ((1033, 1052), 'llama_index.core.llms.callbacks.llm_chat_callback', 'llm_chat_callback', ([], {}), '()\n', (1050, 1052), False, 'from llama_index.core.llms.callbacks import llm_chat_callback, llm_completion_callback\n'), ((1452, 1477), 'llama_index.core.llms.callbacks.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (1475, 1477), False, 'from llama_index.core.llms.callbacks import llm_chat_callback, llm_completion_callback\n'), ((1762, 1787), 'llama_index.core.llms.callbacks.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (1785, 1787), False, 'from llama_index.core.llms.callbacks import llm_chat_callback, llm_completion_callback\n'), ((866, 913), 'llama_index.core.base.llms.types.ChatMessage', 'ChatMessage', ([], {'role': '"""system"""', 'content': 'self.system'}), "(role='system', content=self.system)\n", (877, 913), False, 'from llama_index.core.base.llms.types import ChatMessage, ChatResponseGen, CompletionResponse, CompletionResponseGen\n'), ((1274, 1321), 'llama_index.core.base.llms.types.ChatMessage', 'ChatMessage', ([], {'role': '"""system"""', 'content': 'self.system'}), "(role='system', content=self.system)\n", (1285, 1321), False, 'from llama_index.core.base.llms.types import ChatMessage, ChatResponseGen, CompletionResponse, CompletionResponseGen\n')]