Skip to content

Qwen openai style llm

QWenOpenAIStyleLLM

Bases: OpenAIStyleLLM

QWen OpenAI style LLM Args: api_key: API key for the model ,from dashscope : DASHSCOPE_API_KEY api_base: API base URL for the model, from dashscope : DASHSCOPE_API_BASE

Source code in agentuniverse/llm/default/qwen_openai_style_llm.py
Python
class QWenOpenAIStyleLLM(OpenAIStyleLLM):

    """
        QWen OpenAI style LLM
        Args:
            api_key: API key for the model ,from dashscope : DASHSCOPE_API_KEY
            api_base: API base URL for the model, from dashscope : DASHSCOPE_API_BASE
    """

    api_key: Optional[str] = Field(default_factory=lambda: get_from_env("DASHSCOPE_API_KEY"))
    api_base: Optional[str] = "https://dashscope.aliyuncs.com/compatible-mode/v1"
    proxy: Optional[str] = Field(default_factory=lambda: get_from_env("DASHSCOPE_PROXY"))
    organization: Optional[str] = Field(default_factory=lambda: get_from_env("DASHSCOPE_ORGANIZATION"))

    def max_context_length(self) -> int:
        if super().max_context_length():
            return super().max_context_length()
        return QWen_Max_CONTEXT_LENGTH.get(self.model_name, 8000)

    def get_num_tokens(self, text: str) -> int:
        tokenizer = get_tokenizer(self.model_name)
        return len(tokenizer.encode(text))