Chuyển đến nội dung chính

Tổng quan

Loại: Framework hoặc Nền tảngĐường dẫn chính: Tương thích với OpenAI thông qua OpenAILikeĐộ tin cậy hỗ trợ: Được hỗ trợ thông qua OpenAILike
Đối với TokenLab, thiết lập LlamaIndex mạnh mẽ nhất là sử dụng các tích hợp tương thích với OpenAI thay vì các lớp OpenAI tích hợp sẵn. Tài liệu hiện tại của LlamaIndex khuyến nghị rõ ràng việc sử dụng OpenAILike cho các endpoint của bên thứ ba tương thích với OpenAI, vì các lớp OpenAI tích hợp sẵn sẽ suy luận metadata từ tên mô hình chính thức. Nói cách khác: hãy coi OpenAILike là đường dẫn TokenLab được hỗ trợ tại đây, thay vì các lớp OpenAI tích hợp sẵn.

Cài đặt

pip install llama-index-core \
  llama-index-readers-file \
  llama-index-llms-openai-like \
  llama-index-embeddings-openai-like

Cấu hình cơ bản

from llama_index.core import Settings
from llama_index.llms.openai_like import OpenAILike
from llama_index.embeddings.openai_like import OpenAILikeEmbedding

llm = OpenAILike(
    model="gpt-5.4",
    api_base="https://api.tokenlab.sh/v1",
    api_key="sk-your-tokenlab-key",
    is_chat_model=True,
)

embed_model = OpenAILikeEmbedding(
    model_name="text-embedding-3-small",
    api_base="https://api.tokenlab.sh/v1",
    api_key="sk-your-tokenlab-key",
)

Settings.llm = llm
Settings.embed_model = embed_model

Cách sử dụng cơ bản

response = llm.complete("Explain TokenLab in one sentence.")
print(response.text)

OpenAILike LLM tối giản

from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="claude-sonnet-5",
    api_base="https://api.tokenlab.sh/v1",
    api_key="sk-your-tokenlab-key",
    context_window=200000,
    is_chat_model=True,
    is_function_calling_model=True,
)

Chat

from llama_index.core.llms import ChatMessage

messages = [
    ChatMessage(role="system", content="You are a helpful assistant."),
    ChatMessage(role="user", content="What is the capital of France?")
]

response = llm.chat(messages)
print(response.message.content)

Streaming

for chunk in llm.stream_complete("Write a short poem about AI."):
    print(chunk.delta, end="", flush=True)

Embeddings

vector = embed_model.get_text_embedding("Hello, world!")
print(vector[:5])

RAG với Documents

from llama_index.core import SimpleDirectoryReader, VectorStoreIndex

documents = SimpleDirectoryReader("./data").load_data()
index = VectorStoreIndex.from_documents(documents)

query_engine = index.as_query_engine()
response = query_engine.query("What is in my documents?")
print(response)

Chat Engine

chat_engine = index.as_chat_engine(chat_mode="condense_question")

response = chat_engine.chat("What is TokenLab?")
print(response)

response = chat_engine.chat("How many models does it support?")
print(response)

Sử dụng bất đồng bộ (Async)

import asyncio

async def main():
    response = await llm.acomplete("Hello!")
    print(response.text)

asyncio.run(main())

Các phương pháp tốt nhất

Ưu tiên sử dụng llama_index.llms.openai_like.OpenAILikellama_index.embeddings.openai_like.OpenAILikeEmbedding cho TokenLab và các cổng kết nối bên thứ ba tương thích với OpenAI khác.
Truyền api_base="https://api.tokenlab.sh/v1" trực tiếp vào mã thay vì dựa vào các tên biến môi trường OpenAI cũ.
Sử dụng các mô hình chat/reasoning để tổng hợp và text-embedding-3-small hoặc text-embedding-3-large để truy xuất.