# LangChain และ LlamaIndex กับโมเดล open-weight

> ใช้ LLM แบบ open-weight ใน LangChain และ LlamaIndex: ChatOpenAI พร้อม base_url, การผูก tool, LangChain.js และ OpenAILike จ่ายตาม token ด้วยคริปโต

- URL: https://llmwithcrypto.com/th/docs/langchain/
- อัปเดตล่าสุด: 2026-10-23
- ผู้เผยแพร่: LLMwithCrypto (https://llmwithcrypto.com)

LangChain และ LlamaIndex ต่างมี chat model แบบ OpenAI ที่รับ base URL แบบกำหนดเองได้ ชี้ไปที่ API ของเรา แล้วทุก chain, agent และ retriever ที่คุณสร้างไว้ยังคงใช้งานได้ โดยรันบน DeepSeek, Qwen, gpt-oss และโมเดล open-weight อื่นๆ

**คำตอบสั้น — ใช้ LLMwithCrypto ใน LangChain อย่างไร?**

ติดตั้ง `langchain-openai` และสร้าง `ChatOpenAI(model="deepseek-v4-pro", base_url="https://llmwithcrypto.com/api/v1", api_key=…)` **LLMwithCrypto** พูดรูปแบบ OpenAI chat ดังนั้น `invoke`, streaming และ `bind_tools` ทำงานเหมือนกับ OpenAI บนโมเดลที่รองรับ tools LlamaIndex ใช้ `OpenAILike` กับ base URL เดียวกัน เอกสารเมื่อ 23 ตุลาคม 2026

## จะใช้คลาสไหนดี

| แอตทริบิวต์ | ค่า |
| --- | --- |
| LangChain (Python) | `ChatOpenAI` จาก `langchain-openai` พร้อม `base_url` |
| LangChain.js | `ChatOpenAI` จาก `@langchain/openai` พร้อม `configuration.baseURL` |
| LlamaIndex | `OpenAILike` จาก `llama-index-llms-openai-like` พร้อม `api_base` และ `is_chat_model=True` |
| Model id | บน[หน้ารุ่นโมเดล](https://llmwithcrypto.com/th/models/) |

## จะตั้งค่า ChatOpenAI ได้อย่างไร

ส่ง base URL และ API key ตอนสร้างโมเดล การตั้ง `max_tokens` จะกันไม่ให้ทุกขั้นของ chain หรือ agent เขียนมากกว่าที่คุณยอมจ่าย ส่วน `usage_metadata` ของคำตอบจะแสดงจำนวน token ของแต่ละครั้งที่เรียก

*LangChain: ChatOpenAI บน DeepSeek V4 Pro*

```python
import os

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="deepseek-v4-pro",
    base_url="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
    max_tokens=300,
    temperature=0.3,
)
reply = llm.invoke("Explain what a vector database is in two sentences.")
print(reply.content)
print(reply.usage_metadata)

```

## tools และ agents ใช้ได้ไหม

ได้ บนโมเดลที่รองรับ tool calling Streaming ใช้ได้กับทุกโมเดล; tool calling และ JSON output ขึ้นอยู่กับสิ่งที่แต่ละโมเดลรองรับเอง `bind_tools` จะส่ง tools ของคุณในรูปแบบ OpenAI ทดสอบ schema ของคุณบนโมเดลที่เลือกก่อนจะพึ่งพามัน agent แบบ LangGraph ที่สร้างบน `ChatOpenAI` ทำงานแบบเดียวกัน

*LangChain: tool ที่ผูกกับ gpt-oss-120b*

```python
import os

from langchain_core.tools import tool
from langchain_openai import ChatOpenAI


@tool
def convert_currency(amount: float, rate: float) -> float:
    """Convert an amount with an exchange rate."""
    return round(amount * rate, 2)


llm = ChatOpenAI(
    model="gpt-oss-120b",
    base_url="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
)
llm_with_tools = llm.bind_tools([convert_currency])
msg = llm_with_tools.invoke("How much is 250 at a rate of 0.92?")
for call in msg.tool_calls:
    print(call["name"], call["args"], "->", convert_currency.invoke(call["args"]))

```

## แล้วจาก JavaScript ล่ะ

LangChain.js รับ base URL ไว้ใน `configuration` ซึ่งจะส่งต่อไปยัง OpenAI client ที่อยู่ข้างใต้

*LangChain.js: ChatOpenAI พร้อม baseURL*

```javascript
import { ChatOpenAI } from '@langchain/openai';

const llm = new ChatOpenAI({
  model: 'qwen3-235b-a22b-instruct-2507',
  apiKey: process.env.LLMWITHCRYPTO_API_KEY,
  configuration: { baseURL: 'https://llmwithcrypto.com/api/v1' },
  maxTokens: 300,
});
const reply = await llm.invoke('Give me a three-step plan to learn SQL.');
console.log(reply.content);

```

## จะใช้กับ LlamaIndex ได้อย่างไร

ใช้ `OpenAILike` ซึ่งไม่ตรวจชื่อโมเดลกับรายการของ OpenAI ตั้ง `is_chat_model=True` เพื่อให้ส่งข้อความแบบ chat และระบุ context window ของโมเดลเพื่อให้ LlamaIndex กะขนาด prompt ได้: DeepSeek V4.1 Flash อ่านได้ถึง 1M token ณ วันที่ 23 ตุลาคม 2026 ตัวอย่างตั้ง window เล็กลงโดยตั้งใจเพื่อให้ prompt สั้น

*LlamaIndex: OpenAILike บน DeepSeek V4.1 Flash*

```python
import os

from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="deepseek-v4.1-flash",
    api_base="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
    is_chat_model=True,
    context_window=131072,
    max_tokens=300,
)
print(llm.complete("Name three uses of a knowledge graph."))

```

## จะคุมค่าใช้จ่ายของ agent ได้อย่างไร

agent เรียกโมเดลหลายครั้งต่อหนึ่งงาน: ครั้งหนึ่งเพื่อวางแผน ครั้งหนึ่งต่อผลลัพธ์ของ tool และอีกครั้งเพื่อตอบ แต่ละครั้งจะส่งบทสนทนาทั้งหมดซ้ำ input จึงโตขึ้นทุกขั้น

- **จำกัดแต่ละครั้งที่เรียก** ด้วย `max_tokens` และจำกัดทั้งรันด้วยจำนวนขั้นสูงสุด
- **ใช้โมเดลเล็กเมื่อเพียงพอ** ณ วันที่ 23 ตุลาคม 2026 gpt-oss-20b ราคา $0.033 ต่อล้าน input token เทียบกับ $1.254 ของ DeepSeek V4 Pro: ส่งขั้นตอนง่าย ๆ ไปที่โมเดลเล็ก และเก็บโมเดลใหญ่ไว้ตอบครั้งสุดท้าย
- **ตัดประวัติ** ในบทสนทนายาว หรือสรุปเทิร์นเก่า ๆ
- **ดูเครดิต** ด้วย `GET https://llmwithcrypto.com/api/v1/credits` ก่อนรันครั้งใหญ่

แดชบอร์ดแสดงทุกครั้งที่เรียกพร้อม token และค่าใช้จ่าย ซึ่งช่วยให้เห็นเร็วว่าขั้นไหนของ chain แพง

## Frameworks: คำถามที่พบบ่อย

### ผสมโมเดลใน chain เดียวได้ไหม

ได้: สร้าง `ChatOpenAI` หนึ่งตัวต่อโมเดล เช่นตัวถูกสำหรับ routing และตัวใหญ่สำหรับคำตอบสุดท้าย ทั้งหมดใช้เครดิตก้อนเดียวกัน

### ใช้ embeddings ได้ไหม

ไม่ได้ API นี้ให้บริการเฉพาะ chat completions; ใช้ embedding model ที่มีอยู่ควบคู่ไปด้วย

### จ่ายค่าอะไรสำหรับการรัน agent

ทุกครั้งที่เรียกโมเดลในรันนั้น คิดตาม token คุณจ่ายต่อ token จากยอดคงเหลือ: input และ output tokens ถูกนับด้วย tokenizer ของโมเดลนั้นเองและคิดตามราคาของโมเดลนั้นต่อ 1M tokens การเรียกที่จบด้วย error ไม่มีค่าใช้จ่าย ราคาต่อโมเดลดูได้ที่[หน้าราคา](https://llmwithcrypto.com/th/pricing/)

---

LLMwithCrypto: LLMwithCrypto คือ LLM API ที่เข้ากันได้กับ OpenAI สำหรับโมเดล open-weight — DeepSeek V4 Pro, DeepSeek V4.1 Flash, DeepSeek R1, Qwen3.5 397B A17B, Qwen3 235B A22B Instruct, gpt-oss-120b, gpt-oss-20b, GLM-4.7, Llama 3.3 70B Instruct, Gemma 4 31B, Mistral Large 3, Mistral Small 4 — จ่ายต่อ token จากยอดคงเหลือคริปโตแบบเติมเงิน ในราคาต่ำกว่าค่ามัธยฐานของโฮสต์สาธารณะ 5% ไม่ต้อง KYC: ไม่ต้องใช้บัตรประชาชน ไม่ต้องเซลฟี่ ไม่ต้องใช้เอกสาร ไม่มีการเก็บ prompt และ output
