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

# LlamaIndex

> Integrate FriendliAI with LlamaIndex. Use the Friendli LLM class for chat completions and text completions with sync, async, and streaming support.

You can use [**LlamaIndex**](https://github.com/run-llama/llama_index) to interact with FriendliAI.
This makes migration of existing applications already using LlamaIndex particularly easy.

## How to Use

Before you start, ensure you've already obtained the `API_KEY` from the [Friendli Suite > Personal Settings > API Keys](https://friendli.ai/suite/~/setting/keys).

```python theme={null}
pip install llama-index llama-index-llms-friendli
```

### Instantiation

Now you can instantiate the model object and generate chat completions.
The default model (i.e. `zai-org/GLM-5.2`) will be used if no model is specified.

```python theme={null}
import os
from llama_index.llms.friendli import Friendli

os.environ['API_KEY'] = "YOUR_API_KEY"

llm = Friendli(model="zai-org/GLM-5.2")
```

### Chat Completion

Generate a response from a given conversation.

<CodeGroup>
  ```python Default theme={null}
  from llama_index.core.llms import ChatMessage, MessageRole

  message = ChatMessage(role=MessageRole.USER, content="Tell me a joke.")
  resp = llm.chat([message])

  print(resp)
  ```

  ```python Streaming theme={null}
  from llama_index.core.llms import ChatMessage, MessageRole

  message = ChatMessage(role=MessageRole.USER, content="Tell me a joke.")
  resp = llm.stream_chat([message])

  for r in resp:
      print(r.delta, end="")
  ```

  ```python Async theme={null}
  from llama_index.core.llms import ChatMessage, MessageRole

  message = ChatMessage(role=MessageRole.USER, content="Tell me a joke.")
  resp = await llm.achat([message])

  print(resp)
  ```

  ```python Async Streaming theme={null}
  from llama_index.core.llms import ChatMessage, MessageRole

  message = ChatMessage(role=MessageRole.USER, content="Tell me a joke.")
  resp = await llm.astream_chat([message])

  async for r in resp:
      print(r.delta, end="")
  ```
</CodeGroup>

### Completion

Generate a response from a given prompt.

<CodeGroup>
  ```python Default theme={null}
  prompt = "Draft a cover letter for a role in software engineering."
  resp = llm.complete(prompt)

  print(resp)
  ```

  ```python Streaming theme={null}
  prompt = "Draft a cover letter for a role in software engineering."
  resp = llm.stream_complete(prompt)

  for r in resp:
      print(r.delta, end="")
  ```

  ```python Async theme={null}
  prompt = "Draft a cover letter for a role in software engineering."
  resp = await llm.acomplete(prompt)

  print(resp)
  ```

  ```python Async Streaming theme={null}
  prompt = "Draft a cover letter for a role in software engineering."
  resp = await llm.astream_complete(prompt)

  async for r in resp:
      print(r.delta, end="")
  ```
</CodeGroup>
