> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mixroute.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain

> LangChain 開發框架整合指南

## 一、簡介

LangChain 是一個用於開發基於語言模型的應用程式的強大框架。透過整合 **MixRoute**，可在 LangChain 中靈活調用各類主流 AI 模型。

## 二、快速開始

### 1. 安裝依賴

```bash theme={null}
pip install langchain langchain-openai
```

### 2. 基礎配置

```python theme={null}
import os
from langchain_openai import ChatOpenAI

os.environ["OPENAI_API_KEY"] = "您的 Mixroute Api 金鑰"
os.environ["OPENAI_BASE_URL"] = "https://api.mixroute.ai/v1"

llm = ChatOpenAI(
    model="gpt-5.5",
    temperature=0.7
)
```

## 三、核心功能

### 1. 基礎對話

```python theme={null}
from langchain.schema import HumanMessage, SystemMessage

messages = [
    SystemMessage(content="你是一個有幫助的助手"),
    HumanMessage(content="介紹一下 Python 的主要特點")
]

response = llm.invoke(messages)
print(response.content)
```

### 2. 對話鏈（帶記憶）

```python theme={null}
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain

memory = ConversationBufferMemory()
conversation = ConversationChain(llm=llm, memory=memory)

conversation.predict(input="我想學習機器學習")
conversation.predict(input="推薦一些入門資源")
```

### 3. 串流輸出

```python theme={null}
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

streaming_llm = ChatOpenAI(
    model="gpt-5.5",
    streaming=True,
    callbacks=[StreamingStdOutCallbackHandler()]
)

streaming_llm.invoke("寫一首關於春天的詩")
```
