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

# LlamaIndex

> OpenAI 호환 통합을 사용하여 TokenLab과 LlamaIndex를 통합하세요

## 개요

<Note>
  **유형**: 프레임워크 또는 플랫폼

  **기본 경로**: OpenAILike를 통한 OpenAI 호환

  **지원 신뢰도**: OpenAILike를 통해 지원됨
</Note>

TokenLab의 경우, 내장된 OpenAI 클래스 대신 **OpenAI 호환 통합**을 사용하는 것이 더 안정적인 LlamaIndex 설정 방법입니다.

현재 LlamaIndex 문서에서는 내장 OpenAI 클래스가 공식 모델 이름에서 메타데이터를 추론하기 때문에, 타사 OpenAI 호환 엔드포인트에 대해 `OpenAILike`를 명시적으로 권장하고 있습니다.

즉, 내장 OpenAI 클래스가 아닌 `OpenAILike`를 여기에서 지원되는 TokenLab 경로로 간주하십시오.

## 설치

```bash theme={null}
pip install llama-index-core \
  llama-index-readers-file \
  llama-index-llms-openai-like \
  llama-index-embeddings-openai-like
```

## 기본 설정

```python theme={null}
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
```

## 기본 사용법

```python theme={null}
response = llm.complete("Explain TokenLab in one sentence.")
print(response.text)
```

## 최소 OpenAILike LLM

```python theme={null}
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,
)
```

## 채팅

```python theme={null}
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)
```

## 스트리밍

```python theme={null}
for chunk in llm.stream_complete("Write a short poem about AI."):
    print(chunk.delta, end="", flush=True)
```

## 임베딩

```python theme={null}
vector = embed_model.get_text_embedding("Hello, world!")
print(vector[:5])
```

## 문서 기반 RAG

```python theme={null}
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)
```

## 채팅 엔진

```python theme={null}
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)
```

## 비동기 사용법

```python theme={null}
import asyncio

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

asyncio.run(main())
```

## 모범 사례

<AccordionGroup>
  <Accordion title="TokenLab에 OpenAILike 사용">
    TokenLab 및 기타 타사 OpenAI 호환 게이트웨이에는 `llama_index.llms.openai_like.OpenAILike` 및 `llama_index.embeddings.openai_like.OpenAILikeEmbedding`을 사용하는 것이 좋습니다.
  </Accordion>

  <Accordion title="api_base 명시적 설정">
    이전의 OpenAI 환경 변수 이름에 의존하는 대신 코드 내에서 `api_base="https://api.tokenlab.sh/v1"`을 직접 전달하십시오.
  </Accordion>

  <Accordion title="모델 역할 분리 유지">
    합성(synthesis)에는 채팅/추론 모델을 사용하고, 검색(retrieval)에는 `text-embedding-3-small` 또는 `text-embedding-3-large`를 사용하십시오.
  </Accordion>
</AccordionGroup>
