code: """DashScope llm api."""
  
  from http import HTTPStatus
  from typing import Any, Dict, List, Optional, Sequence, Tuple
  
  from llama_index.legacy.bridge.pydantic import Field
  from llama_index.legacy.callbacks import CallbackManager
  from llama_index.legacy.constants import DEFAULT_NUM_OUTPUTS, DEFAULT_TEMPERATURE
  from llama_index.legacy.core.llms.types import (
      ChatMessage,
      ChatResponse,
      ChatResponseGen,
      CompletionResponse,
      CompletionResponseGen,
      LLMMetadata,
      MessageRole,
  )
  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.dashscope_utils import (
      chat_message_to_dashscope_messages,
      dashscope_response_to_chat_response,
      dashscope_response_to_completion_response,
  )
  
  
  class DashScopeGenerationModels:
      """DashScope Qwen serial models."""
  
      QWEN_TURBO = "qwen-turbo"
      QWEN_PLUS = "qwen-plus"
      QWEN_MAX = "qwen-max"
      QWEN_MAX_1201 = "qwen-max-1201"
      QWEN_MAX_LONGCONTEXT = "qwen-max-longcontext"
  
  
  DASHSCOPE_MODEL_META = {
      DashScopeGenerationModels.QWEN_TURBO: {
          "context_window": 1024 * 8,
          "num_output": 1024 * 8,
          "is_chat_model": True,
      },
      DashScopeGenerationModels.QWEN_PLUS: {
          "context_window": 1024 * 32,
          "num_output": 1024 * 32,
          "is_chat_model": True,
      },
      DashScopeGenerationModels.QWEN_MAX: {
          "context_window": 1024 * 8,
          "num_output": 1024 * 8,
          "is_chat_model": True,
      },
      DashScopeGenerationModels.QWEN_MAX_1201: {
          "context_window": 1024 * 8,
          "num_output": 1024 * 8,
          "is_chat_model": True,
      },
      DashScopeGenerationModels.QWEN_MAX_LONGCONTEXT: {
          "context_window": 1024 * 30,
          "num_output": 1024 * 30,
          "is_chat_model": True,
      },
  }
  
  
  def call_with_messages(
      model: str,
      messages: List[Dict],
      parameters: Optional[Dict] = None,
      api_key: Optional[str] = None,
      **kwargs: Any,
  ) -> Dict:
      try:
          from dashscope import Generation
      except ImportError:
          raise ValueError(
              "DashScope is not installed. Please install it with "
              "`pip install dashscope`."
          )
      return Generation.call(
          model=model, messages=messages, api_key=api_key, **parameters
      )
  
  
  class DashScope(CustomLLM):
      """DashScope LLM."""
  
      model_name: str = Field(
          default=DashScopeGenerationModels.QWEN_MAX,
          description="The DashScope model to use.",
      )
      max_tokens: Optional[int] = Field(
          description="The maximum number of tokens to generate.",
          default=DEFAULT_NUM_OUTPUTS,
          gt=0,
      )
      incremental_output: Optional[bool] = Field(
          description="Control stream output, If False, the subsequent \
                                                              output will include the content that has been \
                                                              output previously.",
          default=True,
      )
      enable_search: Optional[bool] = Field(
          description="The model has a built-in Internet search service. \
                                                              This parameter controls whether the model refers to \
                                                              the Internet search results when generating text.",
          default=False,
      )
      stop: Optional[Any] = Field(
          description="str, list of str or token_id, list of token id. It will automatically \
                                               stop when the generated content is about to contain the specified string \
                                               or token_ids, and the generated content does not contain \
                                               the specified content.",
          default=None,
      )
      temperature: Optional[float] = Field(
          description="The temperature to use during generation.",
          default=DEFAULT_TEMPERATURE,
          gte=0.0,
          lte=2.0,
      )
      top_k: Optional[int] = Field(
          description="Sample counter when generate.", default=None
      )
      top_p: Optional[float] = Field(
          description="Sample probability threshold when generate."
      )
      seed: Optional[int] = Field(
          description="Random seed when generate.", default=1234, gte=0
      )
      repetition_penalty: Optional[float] = Field(
          description="Penalty for repeated words in generated text; \
                                                               1.0 is no penalty, values greater than 1 discourage \
                                                               repetition.",
          default=None,
      )
      api_key: str = Field(
          default=None, description="The DashScope API key.", exclude=True
      )
  
      def __init__(
          self,
          model_name: Optional[str] = DashScopeGenerationModels.QWEN_MAX,
          max_tokens: Optional[int] = DEFAULT_NUM_OUTPUTS,
          incremental_output: Optional[int] = True,
          enable_search: Optional[bool] = False,
          stop: Optional[Any] = None,
          temperature: Optional[float] = DEFAULT_TEMPERATURE,
          top_k: Optional[int] = None,
          top_p: Optional[float] = None,
          seed: Optional[int] = 1234,
          api_key: Optional[str] = None,
          callback_manager: Optional[CallbackManager] = None,
          **kwargs: Any,
      ):
          super().__init__(
              model_name=model_name,
              max_tokens=max_tokens,
              incremental_output=incremental_output,
              enable_search=enable_search,
              stop=stop,
              temperature=temperature,
              top_k=top_k,
              top_p=top_p,
              seed=seed,
              api_key=api_key,
              callback_manager=callback_manager,
              kwargs=kwargs,
          )
  
      @classmethod
      def class_name(cls) -> str:
          return "DashScope_LLM"
  
      @property
      def metadata(self) -> LLMMetadata:
          DASHSCOPE_MODEL_META[self.model_name]["num_output"] = (
              self.max_tokens or DASHSCOPE_MODEL_META[self.model_name]["num_output"]
          )
          return LLMMetadata(
              model_name=self.model_name, **DASHSCOPE_MODEL_META[self.model_name]
          )
  
      def _get_default_parameters(self) -> Dict:
          params: Dict[Any, Any] = {}
          if self.max_tokens is not None:
              params["max_tokens"] = self.max_tokens
          params["incremental_output"] = self.incremental_output
          params["enable_search"] = self.enable_search
          if self.stop is not None:
              params["stop"] = self.stop
          if self.temperature is not None:
              params["temperature"] = self.temperature
  
          if self.top_k is not None:
              params["top_k"] = self.top_k
  
          if self.top_p is not None:
              params["top_p"] = self.top_p
          if self.seed is not None:
              params["seed"] = self.seed
  
          return params
  
      def _get_input_parameters(
          self, prompt: str, **kwargs: Any
      ) -> Tuple[ChatMessage, Dict]:
          parameters = self._get_default_parameters()
          parameters.update(kwargs)
          parameters["stream"] = False
          # we only use message response
          parameters["result_format"] = "message"
          message = ChatMessage(
              role=MessageRole.USER.value,
              content=prompt,
          )
          return message, parameters
  
      @llm_completion_callback()
      def complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
          message, parameters = self._get_input_parameters(prompt=prompt, **kwargs)
          parameters.pop("incremental_output", None)
          parameters.pop("stream", None)
          messages = chat_message_to_dashscope_messages([message])
          response = call_with_messages(
              model=self.model_name,
              messages=messages,
              api_key=self.api_key,
              parameters=parameters,
          )
          return dashscope_response_to_completion_response(response)
  
      @llm_completion_callback()
      def stream_complete(self, prompt: str, **kwargs: Any) -> CompletionResponseGen:
          message, parameters = self._get_input_parameters(prompt=prompt, kwargs=kwargs)
          parameters["incremental_output"] = True
          parameters["stream"] = True
          responses = call_with_messages(
              model=self.model_name,
              messages=chat_message_to_dashscope_messages([message]),
              api_key=self.api_key,
              parameters=parameters,
          )
  
          def gen() -> CompletionResponseGen:
              content = ""
              for response in responses:
                  if response.status_code == HTTPStatus.OK:
                      top_choice = response.output.choices[0]
                      incremental_output = top_choice["message"]["content"]
                      if not incremental_output:
                          incremental_output = ""
  
                      content += incremental_output
                      yield CompletionResponse(
                          text=content, delta=incremental_output, raw=response
                      )
                  else:
                      yield CompletionResponse(text="", raw=response)
                      return
  
          return gen()
  
      @llm_chat_callback()
      def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
          parameters = self._get_default_parameters()
          parameters.update({**kwargs})
          parameters.pop("stream", None)
          parameters.pop("incremental_output", None)
          parameters["result_format"] = "message"  # only use message format.
          response = call_with_messages(
              model=self.model_name,
              messages=chat_message_to_dashscope_messages(messages),
              api_key=self.api_key,
              parameters=parameters,
          )
          return dashscope_response_to_chat_response(response)
  
      @llm_chat_callback()
      def stream_chat(
          self, messages: Sequence[ChatMessage], **kwargs: Any
      ) -> ChatResponseGen:
          parameters = self._get_default_parameters()
          parameters.update({**kwargs})
          parameters["stream"] = True
          parameters["incremental_output"] = True
          parameters["result_format"] = "message"  # only use message format.
          response = call_with_messages(
              model=self.model_name,
              messages=chat_message_to_dashscope_messages(messages),
              api_key=self.api_key,
              parameters=parameters,
          )
  
          def gen() -> ChatResponseGen:
              content = ""
              for r in response:
                  if r.status_code == HTTPStatus.OK:
                      top_choice = r.output.choices[0]
                      incremental_output = top_choice["message"]["content"]
                      role = top_choice["message"]["role"]
                      content += incremental_output
                      yield ChatResponse(
                          message=ChatMessage(role=role, content=content),
                          delta=incremental_output,
                          raw=r,
                      )
                  else:
                      yield ChatResponse(message=ChatMessage(), raw=response)
                      return
  
          return gen()
  
apis: llama_index.legacy.core.llms.types.CompletionResponse llama_index.legacy.llms.dashscope_utils.chat_message_to_dashscope_messages llama_index.legacy.llms.dashscope_utils.dashscope_response_to_chat_response llama_index.legacy.core.llms.types.LLMMetadata llama_index.legacy.llms.base.llm_chat_callback llama_index.legacy.bridge.pydantic.Field llama_index.legacy.llms.base.llm_completion_callback llama_index.legacy.core.llms.types.ChatMessage llama_index.legacy.llms.dashscope_utils.dashscope_response_to_completion_response
extract_api: [((2272, 2350), 'dashscope.Generation.call', 'Generation.call', ([], {'model': 'model', 'messages': 'messages', 'api_key': 'api_key'}), '(model=model, messages=messages, api_key=api_key, **parameters)\n', (2287, 2350), False, 'from dashscope import Generation\n'), ((2443, 2540), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'default': 'DashScopeGenerationModels.QWEN_MAX', 'description': '"""The DashScope model to use."""'}), "(default=DashScopeGenerationModels.QWEN_MAX, description=\n    'The DashScope model to use.')\n", (2448, 2540), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((2591, 2693), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""The maximum number of tokens to generate."""', 'default': 'DEFAULT_NUM_OUTPUTS', 'gt': '(0)'}), "(description='The maximum number of tokens to generate.', default=\n    DEFAULT_NUM_OUTPUTS, gt=0)\n", (2596, 2693), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((2761, 3038), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""Control stream output, If False, the subsequent                                                             output will include the content that has been                                                             output previously."""', 'default': '(True)'}), "(description=\n    'Control stream output, If False, the subsequent                                                             output will include the content that has been                                                             output previously.'\n    , default=True)\n", (2766, 3038), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((3092, 3409), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""The model has a built-in Internet search service.                                                             This parameter controls whether the model refers to                                                             the Internet search results when generating text."""', 'default': '(False)'}), "(description=\n    'The model has a built-in Internet search service.                                                             This parameter controls whether the model refers to                                                             the Internet search results when generating text.'\n    , default=False)\n", (3097, 3409), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((3453, 3855), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""str, list of str or token_id, list of token id. It will automatically                                              stop when the generated content is about to contain the specified string                                              or token_ids, and the generated content does not contain                                              the specified content."""', 'default': 'None'}), "(description=\n    'str, list of str or token_id, list of token id. It will automatically                                              stop when the generated content is about to contain the specified string                                              or token_ids, and the generated content does not contain                                              the specified content.'\n    , default=None)\n", (3458, 3855), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((3910, 4024), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""The temperature to use during generation."""', 'default': 'DEFAULT_TEMPERATURE', 'gte': '(0.0)', 'lte': '(2.0)'}), "(description='The temperature to use during generation.', default=\n    DEFAULT_TEMPERATURE, gte=0.0, lte=2.0)\n", (3915, 4024), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((4086, 4150), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""Sample counter when generate."""', 'default': 'None'}), "(description='Sample counter when generate.', default=None)\n", (4091, 4150), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((4194, 4258), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""Sample probability threshold when generate."""'}), "(description='Sample probability threshold when generate.')\n", (4199, 4258), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((4299, 4367), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""Random seed when generate."""', 'default': '(1234)', 'gte': '(0)'}), "(description='Random seed when generate.', default=1234, gte=0)\n", (4304, 4367), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((4424, 4700), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'description': '"""Penalty for repeated words in generated text;                                                              1.0 is no penalty, values greater than 1 discourage                                                              repetition."""', 'default': 'None'}), "(description=\n    'Penalty for repeated words in generated text;                                                              1.0 is no penalty, values greater than 1 discourage                                                              repetition.'\n    , default=None)\n", (4429, 4700), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((4737, 4808), 'llama_index.legacy.bridge.pydantic.Field', 'Field', ([], {'default': 'None', 'description': '"""The DashScope API key."""', 'exclude': '(True)'}), "(default=None, description='The DashScope API key.', exclude=True)\n", (4742, 4808), False, 'from llama_index.legacy.bridge.pydantic import Field\n'), ((7440, 7465), 'llama_index.legacy.llms.base.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (7463, 7465), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((8034, 8059), 'llama_index.legacy.llms.base.llm_completion_callback', 'llm_completion_callback', ([], {}), '()\n', (8057, 8059), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((9274, 9293), 'llama_index.legacy.llms.base.llm_chat_callback', 'llm_chat_callback', ([], {}), '()\n', (9291, 9293), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((9921, 9940), 'llama_index.legacy.llms.base.llm_chat_callback', 'llm_chat_callback', ([], {}), '()\n', (9938, 9940), False, 'from llama_index.legacy.llms.base import llm_chat_callback, llm_completion_callback\n'), ((6160, 6245), 'llama_index.legacy.core.llms.types.LLMMetadata', 'LLMMetadata', ([], {'model_name': 'self.model_name'}), '(model_name=self.model_name, **DASHSCOPE_MODEL_META[self.model_name]\n    )\n', (6171, 6245), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata, MessageRole\n'), ((7307, 7363), 'llama_index.legacy.core.llms.types.ChatMessage', 'ChatMessage', ([], {'role': 'MessageRole.USER.value', 'content': 'prompt'}), '(role=MessageRole.USER.value, content=prompt)\n', (7318, 7363), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata, MessageRole\n'), ((7731, 7776), 'llama_index.legacy.llms.dashscope_utils.chat_message_to_dashscope_messages', 'chat_message_to_dashscope_messages', (['[message]'], {}), '([message])\n', (7765, 7776), False, 'from llama_index.legacy.llms.dashscope_utils import chat_message_to_dashscope_messages, dashscope_response_to_chat_response, dashscope_response_to_completion_response\n'), ((7976, 8027), 'llama_index.legacy.llms.dashscope_utils.dashscope_response_to_completion_response', 'dashscope_response_to_completion_response', (['response'], {}), '(response)\n', (8017, 8027), False, 'from llama_index.legacy.llms.dashscope_utils import chat_message_to_dashscope_messages, dashscope_response_to_chat_response, dashscope_response_to_completion_response\n'), ((9869, 9914), 'llama_index.legacy.llms.dashscope_utils.dashscope_response_to_chat_response', 'dashscope_response_to_chat_response', (['response'], {}), '(response)\n', (9904, 9914), False, 'from llama_index.legacy.llms.dashscope_utils import chat_message_to_dashscope_messages, dashscope_response_to_chat_response, dashscope_response_to_completion_response\n'), ((8411, 8456), 'llama_index.legacy.llms.dashscope_utils.chat_message_to_dashscope_messages', 'chat_message_to_dashscope_messages', (['[message]'], {}), '([message])\n', (8445, 8456), False, 'from llama_index.legacy.llms.dashscope_utils import chat_message_to_dashscope_messages, dashscope_response_to_chat_response, dashscope_response_to_completion_response\n'), ((9729, 9773), 'llama_index.legacy.llms.dashscope_utils.chat_message_to_dashscope_messages', 'chat_message_to_dashscope_messages', (['messages'], {}), '(messages)\n', (9763, 9773), False, 'from llama_index.legacy.llms.dashscope_utils import chat_message_to_dashscope_messages, dashscope_response_to_chat_response, dashscope_response_to_completion_response\n'), ((10394, 10438), 'llama_index.legacy.llms.dashscope_utils.chat_message_to_dashscope_messages', 'chat_message_to_dashscope_messages', (['messages'], {}), '(messages)\n', (10428, 10438), False, 'from llama_index.legacy.llms.dashscope_utils import chat_message_to_dashscope_messages, dashscope_response_to_chat_response, dashscope_response_to_completion_response\n'), ((9010, 9082), 'llama_index.legacy.core.llms.types.CompletionResponse', 'CompletionResponse', ([], {'text': 'content', 'delta': 'incremental_output', 'raw': 'response'}), '(text=content, delta=incremental_output, raw=response)\n', (9028, 9082), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata, MessageRole\n'), ((9177, 9218), 'llama_index.legacy.core.llms.types.CompletionResponse', 'CompletionResponse', ([], {'text': '""""""', 'raw': 'response'}), "(text='', raw=response)\n", (9195, 9218), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata, MessageRole\n'), ((10971, 11010), 'llama_index.legacy.core.llms.types.ChatMessage', 'ChatMessage', ([], {'role': 'role', 'content': 'content'}), '(role=role, content=content)\n', (10982, 11010), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata, MessageRole\n'), ((11184, 11197), 'llama_index.legacy.core.llms.types.ChatMessage', 'ChatMessage', ([], {}), '()\n', (11195, 11197), False, 'from llama_index.legacy.core.llms.types import ChatMessage, ChatResponse, ChatResponseGen, CompletionResponse, CompletionResponseGen, LLMMetadata, MessageRole\n')]