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

# LangChat

> Complete API reference for the LangChat class.

## Class: `LangChat`

The main entry point for the LangChat SDK. Wraps `LangChatEngine` and returns typed `ChatResponse` objects.

```python theme={null}
from langchat import LangChat
from langchat.providers import OpenAI, Pinecone, Supabase
```

***

## Constructor

```python theme={null}
LangChat(
    *,
    llm,
    vector_db,
    db,
    reranker=None,
    prompt_template=None,
    standalone_question_prompt=None,
    verbose=False,
    max_chat_history=20,
)
```

All parameters are keyword-only (note the `*`).

<ParamField path="llm" type="Any" required>
  LLM provider instance. From `langchat.providers`: `OpenAI`, `Anthropic`, `Gemini`, `Mistral`, `Cohere`, `Ollama`.
</ParamField>

<ParamField path="vector_db" type="Any" required>
  Vector database provider. From `langchat.providers`: `Pinecone`.
</ParamField>

<ParamField path="db" type="Any" required>
  History database provider. From `langchat.providers`: `Supabase`.
</ParamField>

<ParamField path="reranker" type="Any | None" default="FlashrankRerankAdapter">
  Reranker instance. Defaults to `FlashrankRerankAdapter(model_name="ms-marco-MiniLM-L-12-v2", top_n=3)`.
</ParamField>

<ParamField path="prompt_template" type="str | None" default="None">
  Custom system prompt. Must include `{context}`, `{chat_history}`, `{question}`. See [Prompts guide](/guides/prompts).
</ParamField>

<ParamField path="standalone_question_prompt" type="str | None" default="None">
  Custom standalone question prompt. Must include `{chat_history}` and `{question}`.
</ParamField>

<ParamField path="verbose" type="bool" default="False">
  Enable verbose LangChain logging.
</ParamField>

<ParamField path="max_chat_history" type="int" default="20">
  Number of recent exchanges to include in each prompt. One exchange = one (user, AI) pair.
</ParamField>

**Example:**

```python theme={null}
lc = LangChat(
    llm=OpenAI("gpt-4o-mini"),
    vector_db=Pinecone("my-index"),
    db=Supabase(),
    max_chat_history=10,
)
```

***

## Methods

### `chat()`

Send a message and receive a typed response. Async.

```python theme={null}
async def chat(
    self,
    query: str,
    user_id: str,
    platform: str = "default",
) -> ChatResponse
```

<ParamField path="query" type="str" required>
  The user's message.
</ParamField>

<ParamField path="user_id" type="str" required>
  Unique identifier for the user. Scopes conversation history.
</ParamField>

<ParamField path="platform" type="str" default="default">
  Namespace for the conversation. Use different values to separate conversations for the same user across multiple apps.
</ParamField>

**Returns:** [`ChatResponse`](/api-reference/langchat-config)

**Example:**

```python theme={null}
response = await lc.chat(
    query="What is our return policy?",
    user_id="alice",
    platform="web",
)

if response:                                    # True when status == "success"
    print(response.text)
    print(f"{response.response_time:.2f}s")
else:
    print(f"Error: {response.error}")
```

***

### `chat_sync()`

Synchronous wrapper around `chat()`. Blocks until the response is ready.

```python theme={null}
def chat_sync(
    self,
    query: str,
    user_id: str,
    platform: str = "default",
) -> ChatResponse
```

Same parameters and return type as `chat()`.

**Example:**

```python theme={null}
# Useful in scripts, notebooks, and synchronous frameworks
response = lc.chat_sync(query="Hello!", user_id="alice")
print(response)
```

***

### `index()`

Index documents into Pinecone. Can accept a file path, a list of paths, or a directory.

```python theme={null}
def index(
    self,
    paths: str | list[str],
    *,
    chunk_size: int = 1000,
    chunk_overlap: int = 200,
    namespace: str | None = None,
    prevent_duplicates: bool = True,
) -> dict
```

<ParamField path="paths" type="str | list[str]" required>
  Path to a file, a list of file paths, or a directory path.
</ParamField>

<ParamField path="chunk_size" type="int" default="1000">
  Maximum characters per chunk.
</ParamField>

<ParamField path="chunk_overlap" type="int" default="200">
  Character overlap between adjacent chunks.
</ParamField>

<ParamField path="namespace" type="str | None" default="None">
  Pinecone namespace to index into.
</ParamField>

<ParamField path="prevent_duplicates" type="bool" default="True">
  Skip chunks that are already in Pinecone (detected by content hash).
</ParamField>

**Returns:** `dict` with:

* `chunks_indexed` (`int`) — chunks added
* `chunks_skipped` (`int`) — duplicates skipped
* `files_processed` (`int`) — files successfully loaded
* `errors` (`list`) — files that failed to load

**Example:**

```python theme={null}
result = lc.index(["docs/faq.pdf", "docs/manual.pdf"], namespace="support")
print(f"Indexed {result['chunks_indexed']} chunks")
```

***

### `get_session()`

Get or create a `UserSession` for a user.

```python theme={null}
def get_session(
    self,
    user_id: str,
    platform: str = "default",
) -> UserSession
```

**Example:**

```python theme={null}
session = lc.get_session("alice", platform="web")
print(session.chat_history)   # list of (query, response) tuples
```

***

### `load_env()` (class method)

Load a `.env` file into the environment. Call this before creating `LangChat`.

```python theme={null}
@classmethod
def load_env(cls, path: str = ".env") -> None
```

**Example:**

```python theme={null}
LangChat.load_env()            # loads .env from current directory
LangChat.load_env(".env.prod") # loads a specific file
```

***

## Properties

<ParamField path="engine" type="LangChatEngine">
  Access to the underlying `LangChatEngine` instance.
</ParamField>
