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

# LangChain

> This quickstart guide will help you get started using LangChain with SeekrFlow chat models.

This guide provides a quick overview for getting started with Seekr [chat models](https://python.langchain.com/concepts/chat_models) via our LangChain integration.

## Introduction

`ChatSeekrFlow` class wraps a chat model endpoint hosted on SeekrFlow, enabling seamless integration with LangChain applications.

### Integration Details

| Class                                                                                                           | Package                                                              | Local | Serializable | Package downloads                                                                                                  | Package latest                                                                                                  |
| --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- | ----- | ------------ | ------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- |
| [ChatSeekrFlow](https://github.com/benfaircloth/langchain-seekrflow/blob/main/langchain_seekrflow/seekrflow.py) | [langchain-seekrflow](https://pypi.org/project/langchain-seekrflow/) | ❌     | beta         | <img src="https://mintlify.s3.us-west-1.amazonaws.com/seekr/images/docs/pypi/dm/seekrai" alt="PyPI - Downloads" /> | <img src="https://mintlify.s3.us-west-1.amazonaws.com/seekr/images/docs/pypi/v/seekrai" alt="PyPI - Version" /> |

### Model Features

| [Tool calling](https://python.langchain.com/how_to/tool_calling/) | [Structured output](https://python.langchain.com/how_to/structured_output/) | JSON mode | [Image input](https://python.langchain.com/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](https://python.langchain.com/how_to/chat_streaming/) | Native async | [Token usage](https://python.langchain.com/how_to/chat_token_usage_tracking/) | [Logprobs](https://python.langchain.com/how_to/logprobs/) |
| ----------------------------------------------------------------- | --------------------------------------------------------------------------- | --------- | --------------------------------------------------------------------- | ----------- | ----------- | ---------------------------------------------------------------------------- | ------------ | ----------------------------------------------------------------------------- | --------------------------------------------------------- |
| ✅                                                                 | ✅                                                                           | ✅         | ❌                                                                     | ❌           | ❌           | ✅                                                                            | ❌            | ✅                                                                             | ❌                                                         |

**Supported Methods**

`ChatSeekrFlow` supports all methods of `ChatModel`, **except async APIs**.

**Endpoint Requirements**

The serving endpoint `ChatSeekrFlow` wraps **must** have OpenAI-compatible chat input/output format. It can be used for:

1. **Fine-tuned Seekr models**
2. **Custom SeekrFlow models**
3. **RAG-enabled models using Seekr's retrieval system**

For async usage, please refer to `AsyncChatSeekrFlow` (coming soon).

## Installation

Ensure you have the necessary dependencies installed:

<CodeGroup>
  ```bash Bash theme={null}
  pip install seekrai langchain langchain-community
  ```
</CodeGroup>

You must also have an API key from Seekr to authenticate requests.

<CodeGroup>
  ```python Python theme={null}
  # Standard library
  import getpass
  import os

  # Third-party
  from langchain.prompts import ChatPromptTemplate
  from langchain.schema import HumanMessage
  from langchain_core.runnables import RunnableSequence

  # OSS SeekrFlow integration
  from langchain_seekrflow import ChatSeekrFlow
  from seekrai import SeekrFlow
  ```
</CodeGroup>

## API Key Setup

You'll need to set your API key as an environment variable to authenticate requests.

Run the below cell.

Or manually assign it before running queries:

<CodeGroup>
  ```python Python theme={null}
  SEEKR_API_KEY = "your-api-key-here"
  ```
</CodeGroup>

<CodeGroup>
  ```python Python theme={null}
  os.environ["SEEKR_API_KEY"] = getpass.getpass("Enter your Seekr API key:")
  ```
</CodeGroup>

## Instantiation

<CodeGroup>
  ```python Python theme={null}
  os.environ["SEEKR_API_KEY"]
  seekr_client = SeekrFlow(api_key=SEEKR_API_KEY)

  llm = ChatSeekrFlow(
      client=seekr_client, model_name="meta-llama/Meta-Llama-3-8B-Instruct"
  )
  ```
</CodeGroup>

## Invocation

<CodeGroup>
  ```python Python theme={null}
  response = llm.invoke([HumanMessage(content="Hello, Seekr!")])
  print(response.content)
  ```
</CodeGroup>

Output:

```
Hello there! I'm Seekr, nice to meet you! What brings you here today? Do you have a question, or are you looking for some help with something? I'm all ears (or rather, all text)!
```

## Chaining

<CodeGroup>
  ```python Python theme={null}
  prompt = ChatPromptTemplate.from_template("Translate to French: {text}")

  chain: RunnableSequence = prompt | llm
  result = chain.invoke({"text": "Good morning"})
  print(result)
  ```
</CodeGroup>

**Sample response:**

```
content='The translation of "Good morning" in French is:\n\n"Bonne journée"' additional_kwargs={} response_metadata={}
```

<CodeGroup>
  ```python Python theme={null}
  def test_stream():
      """Test synchronous invocation in streaming mode."""
      print("\n🔹 Testing Sync `stream()` (Streaming)...")

      for chunk in llm.stream([HumanMessage(content="Write me a haiku.")]):
          print(chunk.content, end="", flush=True)

  # ✅ Ensure streaming is enabled
  llm = ChatSeekrFlow(
      client=seekr_client,
      model_name="meta-llama/Meta-Llama-3-8B-Instruct",
      streaming=True,  # ✅ Enable streaming
  )

  # ✅ Run sync streaming test
  test_stream()
  ```
</CodeGroup>

**Sample response:**

```
🔹 Testing Sync `stream()` (Streaming)...
Here is a haiku:

Golden sunset fades
Ripples on the quiet lake
Peaceful evening sky
```

## Error Handling & Debugging

<CodeGroup>
  ```python Python expandable theme={null}
  # Define a minimal mock SeekrFlow client
  class MockSeekrClient:
      """Mock SeekrFlow API client that mimics the real API structure."""

      class MockChat:
          """Mock Chat object with a completions method."""

          class MockCompletions:
              """Mock Completions object with a create method."""

              def create(self, *args, **kwargs):
                  return {
                      "choices": [{"message": {"content": "Mock response"}}]
                  }  # Mimic API response

          completions = MockCompletions()

      chat = MockChat()

  def test_initialization_errors():
      """Test that invalid ChatSeekrFlow initializations raise expected errors."""

      test_cases = [
          {
              "name": "Missing Client",
              "args": {"client": None, "model_name": "seekrflow-model"},
              "expected_error": "SeekrFlow client cannot be None.",
          },
          {
              "name": "Missing Model Name",
              "args": {"client": MockSeekrClient(), "model_name": ""},
              "expected_error": "A valid model name must be provided.",
          },
      ]

      for test in test_cases:
          try:
              print(f"Running test: {test['name']}")
              faulty_llm = ChatSeekrFlow(**test["args"])

              # If no error is raised, fail the test
              print(f"❌ Test '{test['name']}' failed: No error was raised!")
          except Exception as e:
              error_msg = str(e)
              assert test["expected_error"] in error_msg, f"Unexpected error: {error_msg}"
              print(f"✅ Expected Error: {error_msg}")

  # Run test
  test_initialization_errors()
  ```
</CodeGroup>

Output:

```
Running test: Missing Client
✅ Expected Error: SeekrFlow client cannot be None.
Running test: Missing Model Name
✅ Expected Error: A valid model name must be provided.
```

### API reference

* `ChatSeekrFlow` class: [`langchain_seekrflow.ChatSeekrFlow`](https://github.com/benfaircloth/langchain-seekrflow/blob/main/langchain_seekrflow/seekrflow.py)
* PyPI package: [`langchain-seekrflow`](https://pypi.org/project/langchain-seekrflow/)
