code: from typing import Any, Callable, Dict, Optional, Sequence
  
  from llama_index.legacy.bridge.pydantic import Field, PrivateAttr
  from llama_index.legacy.callbacks import CallbackManager
  from llama_index.legacy.constants import DEFAULT_NUM_OUTPUTS
  from llama_index.legacy.core.llms.types import (
      ChatMessage,
      ChatResponse,
      ChatResponseGen,
      CompletionResponse,
      CompletionResponseGen,
      LLMMetadata,
  )
  from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback
  from llama_index.legacy.llms.custom import CustomLLM
  from llama_index.legacy.llms.generic_utils import chat_to_completion_decorator
  from llama_index.legacy.llms.openai_utils import (
      from_openai_message_dict,
      to_openai_message_dicts,
  )
  from llama_index.legacy.types import BaseOutputParser, PydanticProgramMode
  
  
  class LlamaAPI(CustomLLM):
      model: str = Field(description="The llama-api model to use.")
      temperature: float = Field(description="The temperature to use for sampling.")
      max_tokens: int = Field(description="The maximum number of tokens to generate.")
      additional_kwargs: Dict[str, Any] = Field(
          default_factory=dict, description="Additional kwargs for the llama-api API."
      )
  
      _client: Any = PrivateAttr()
  
      def __init__(
          self,
          model: str = "llama-13b-chat",
          temperature: float = 0.1,
          max_tokens: int = DEFAULT_NUM_OUTPUTS,
          additional_kwargs: Optional[Dict[str, Any]] = None,
          api_key: Optional[str] = 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,
          output_parser: Optional[BaseOutputParser] = None,
      ) -> None:
          try:
              from llamaapi import LlamaAPI as Client
          except ImportError as e:
              raise ImportError(
                  "llama_api not installed."
                  "Please install it with `pip install llamaapi`."
              ) from e
  
          self._client = Client(api_key)
  
          super().__init__(
              model=model,
              temperature=temperature,
              max_tokens=max_tokens,
              additional_kwargs=additional_kwargs or {},
              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,
              output_parser=output_parser,
          )
  
      @classmethod
      def class_name(cls) -> str:
          return "llama_api_llm"
  
      @property
      def _model_kwargs(self) -> Dict[str, Any]:
          base_kwargs = {
              "model": self.model,
              "temperature": self.temperature,
              "max_length": self.max_tokens,
          }
          return {
              **base_kwargs,
              **self.additional_kwargs,
          }
  
      @property
      def metadata(self) -> LLMMetadata:
          return LLMMetadata(
              context_window=4096,
              num_output=DEFAULT_NUM_OUTPUTS,
              is_chat_model=True,
              is_function_calling_model=True,
              model_name="llama-api",
          )
  
      @llm_chat_callback()
      def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
          message_dicts = to_openai_message_dicts(messages)
          json_dict = {
              "messages": message_dicts,
              **self._model_kwargs,
              **kwargs,
          }
          response = self._client.run(json_dict).json()
          message_dict = response["choices"][0]["message"]
          message = from_openai_message_dict(message_dict)
  
          return ChatResponse(message=message, raw=response)
  
      @llm_completion_callback()
      def complete(
          self, prompt: str, formatted: bool = False, **kwargs: Any
      ) -> CompletionResponse:
          complete_fn = chat_to_completion_decorator(self.chat)
          return complete_fn(prompt, **kwargs)
  
      @llm_completion_callback()
      def stream_complete(
          self, prompt: str, formatted: bool = False, **kwargs: Any
      ) -> CompletionResponseGen:
          raise NotImplementedError("stream_complete is not supported for LlamaAPI")
  
      @llm_chat_callback()
      def stream_chat(
          self, messages: Sequence[ChatMessage], **kwargs: Any
      ) -> ChatResponseGen:
          raise NotImplementedError("stream_chat is not supported for LlamaAPI")
  
apis: llama_index.legacy.llms.openai_utils.from_openai_message_dict llama_index.legacy.llms.base.llm_chat_callback llama_index.legacy.bridge.pydantic.Field llama_index.legacy.core.llms.types.LLMMetadata llama_index.legacy.llms.base.llm_completion_callback llama_index.legacy.bridge.pydantic.PrivateAttr llama_index.legacy.llms.openai_utils.to_openai_message_dicts llama_index.legacy.llms.generic_utils.chat_to_completion_decorator llama_index.legacy.core.llms.types.ChatResponse
extract_api: [((868, 916), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""The llama-api model to use."""'}), "(description='The llama-api model to use.')\n", (873, 916), False, 'from llama_index.legacy.bridge.pydantic import Field, PrivateAttr\n'), ((942, 999), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""The temperature to use for sampling."""'}), "(description='The temperature to use for sampling.')\n", (947, 999), False, 'from llama_index.legacy.bridge.pydantic import Field, PrivateAttr\n'), ((1022, 1084), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""The maximum number of tokens to generate."""'}), "(description='The maximum number of tokens to generate.')\n", (1027, 1084), False, 'from llama_index.legacy.bridge.pydantic import Field, PrivateAttr\n'), ((1125, 1213), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'default_factory': 'dict', 'description': '"""Additional kwargs for the llama-api API."""'}), "(default_factory=dict, description=\n    'Additional kwargs for the llama-api API.')\n", (1130, 1213), False, 'from llama_index.legacy.bridge.pydantic import Field, PrivateAttr\n'), ((1243, 1256), 'llama_index.legacy.bridge.pydantic.PrivateAttr', 'PrivateAttr', ([], {}), '()\n', (1254, 1256), False, 'from llama_index.legacy.bridge.pydantic import Field, PrivateAttr\n'), ((3379, 3398), 'llama_index.legacy.llms.base.llm_chat_callback', 'llm_chat_callback', ([], {}), '()\n', (3396, 3398), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((3902, 3927), 'llama_index.legacy.llms.base.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (3925, 3927), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((4154, 4179), 'llama_index.legacy.llms.base.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (4177, 4179), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((4392, 4411), 'llama_index.legacy.llms.base.llm_chat_callback', 'llm_chat_callback', ([], {}), '()\n', (4409, 4411), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((2205, 2220), 'llamaapi.LlamaAPI', 'Client', (['api_key'], {}), '(api_key)\n', (2211, 2220), True, 'from llamaapi import LlamaAPI as Client\n'), ((3161, 3305), 'llama_index.legacy.core.llms.types.LLMMetadata', 'LLMMetadata', ([], {'context_window': '(4096)', 'num_output': 'DEFAULT_NUM_OUTPUTS', 'is_chat_model': '(True)', 'is_function_calling_model': '(True)', 'model_name': '"""llama-api"""'}), "(context_window=4096, num_output=DEFAULT_NUM_OUTPUTS,\n    is_chat_model=True, is_function_calling_model=True, model_name='llama-api')\n", (3172, 3305), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata\n'), ((3507, 3540), 'llama_index.legacy.llms.openai_utils.to_openai_message_dicts', 'to_openai_message_dicts', (['messages'], {}), '(messages)\n', (3530, 3540), False, 'from llama_index.legacy.llms.openai_utils import from_openai_message_dict, to_openai_message_dicts\n'), ((3797, 3835), 'llama_index.legacy.llms.openai_utils.from_openai_message_dict', 'from_openai_message_dict', (['message_dict'], {}), '(message_dict)\n', (3821, 3835), False, 'from llama_index.legacy.llms.openai_utils import from_openai_message_dict, to_openai_message_dicts\n'), ((3852, 3895), 'llama_index.legacy.core.llms.types.ChatResponse', 'ChatResponse', ([], {'message': 'message', 'raw': 'response'}), '(message=message, raw=response)\n', (3864, 3895), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata\n'), ((4063, 4102), 'llama_index.legacy.llms.generic_utils.chat_to_completion_decorator', 'chat_to_completion_decorator', (['self.chat'], {}), '(self.chat)\n', (4091, 4102), False, 'from llama_index.legacy.llms.generic_utils import chat_to_completion_decorator\n')]