This notebook shows how to use YUAN2 API in LangChain with the langchain.chat_models.ChatYuan2. Yuan2.0 is a new generation Fundamental Large Language Model developed by IEIT System. We have published all three models, Yuan 2.0-102B, Yuan 2.0-51B, and Yuan 2.0-2B. And we provide relevant scripts for pretraining, fine-tuning, and inference services for other developers. Yuan2.0 is based on Yuan1.0, utilizing a wider range of high-quality pre training data and instruction fine-tuning datasets to enhance the model’s understanding of semantics, mathematics, reasoning, code, knowledge, and other aspects.Documentation Index
Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
Use this file to discover all available pages before exploring further.
Getting started
Installation
First, Yuan2.0 provided an OpenAI compatible API, and we integrate ChatYuan2 into langchain chat model by using OpenAI client. Therefore, ensure the openai package is installed in your Python environment. Run the following command:Importing the required modules
After installation, import the necessary modules to your Python script:Setting up your API server
Setting up your OpenAI compatible API server following yuan2 openai api server. If you deployed api server locally, you can simply setyuan2_api_key="EMPTY" or anything you want.
Just make sure, the yuan2_api_base is set correctly.
Initialize the ChatYuan2 model
Here’s how to initialize the chat model:Basic usage
Invoke the model with system and human messages like this:Basic usage with streaming
For continuous interaction, use the streaming feature:Advanced features
Usage with async calls
Invoke the model with non-blocking calls, like this:Usage with prompt template
Invoke the model with non-blocking calls and used chat template like this:Usage with async calls in streaming
For non-blocking calls with streaming output, use the astream method:Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

