# Tongyi

> **Class** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/llms/tongyi/Tongyi)

Tongyi completion model integration.

## Signature

```python
Tongyi()
```

## Description

**Setup:**

Install ``dashscope`` and set environment variables ``DASHSCOPE_API_KEY``.

.. code-block:: bash

    pip install dashscope
    export DASHSCOPE_API_KEY="your-api-key"

Key init args — completion params:
    model: str
        Name of Tongyi model to use.
    top_p: float
        Total probability mass of tokens to consider at each step.
    streaming: bool
        Whether to stream the results or not.

Key init args — client params:
    api_key: Optional[str]
        Dashscope API KEY. If not passed in will be read from env var DASHSCOPE_API_KEY.
    max_retries: int
        Maximum number of retries to make when generating.

See full list of supported init args and their descriptions in the params section.

**Instantiate:**

.. code-block:: python

from langchain_community.llms import Tongyi

llm = Tongyi(
    model="qwen-max",
    # top_p="...",
    # api_key="...",
    # other params...
)

**Invoke:**

.. code-block:: python

    input_text = "用50个字左右阐述，生命的意义在于"
    llm.invoke(input_text)

.. code-block:: python

    '探索、成长、连接与爱——在有限的时间里，不断学习、体验、贡献并寻找与世界和谐共存之道，让每一刻充满价值与意义。'

**Stream:**

.. code-block:: python

    for chunk in llm.stream(input_text):
        print(chunk)

.. code-block:: python

    探索 | 、 | 成长 | 、连接与爱。 | 在有限的时间里，寻找个人价值， | 贡献于他人，共同体验世界的美好 | ，让世界因自己的存在而更 | 温暖。

**Async:**

.. code-block:: python

    await llm.ainvoke(input_text)

    # stream:
    # async for chunk in llm.astream(input_text):
    #    print(chunk)

    # batch:
    # await llm.abatch([input_text])

.. code-block:: python

    '探索、成长、连接与爱。在有限的时间里，寻找个人价值，贡献于他人和社会，体验丰富多彩的情感与经历，不断学习进步，让世界因自己的存在而更美好。'

## Extends

- `BaseLLM`

## Properties

- `lc_secrets`
- `client`
- `model_name`
- `model_kwargs`
- `top_p`
- `dashscope_api_key`
- `streaming`
- `max_retries`

## Methods

- [`validate_environment()`](https://reference.langchain.com/python/langchain-community/llms/tongyi/Tongyi/validate_environment)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/a6a6079511ac8a5c1293337f88096b8641562e77/libs/community/langchain_community/llms/tongyi.py#L161)