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

# LangChain Integration

> Use the platform with LangChain for building AI applications

## Overview

[LangChain](https://python.langchain.com/) is a popular framework for developing applications powered by language models. The API works seamlessly with LangChain.

<Info>
  All requests through LangChain automatically flow through the TEE-protected gateway.
</Info>

## Installation

<CodeGroup>
  ```bash Python theme={null}
  pip install langchain-openai
  ```

  ```bash JavaScript/TypeScript theme={null}
  npm install langchain @langchain/openai
  ```
</CodeGroup>

## Python Integration

### Basic Setup

```python theme={null}
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

# Initialize ChatOpenAI with the platform
chat = ChatOpenAI(
    model="openai/gpt-5",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1",
    temperature=0.7
)

# Send messages
messages = [
    SystemMessage(content="You are a helpful AI assistant."),
    HumanMessage(content="Explain quantum computing in simple terms")
]

response = chat.invoke(messages)
print(response.content)
```

### Streaming Responses

```python theme={null}
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

chat = ChatOpenAI(
    model="openai/gpt-5",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1",
    streaming=True
)

# Stream the response
for chunk in chat.stream([HumanMessage(content="Write a story about AI")]):
    print(chunk.content, end="", flush=True)
```

### Using Multiple Models

The platform supports a broad range of models. Switch between them easily:

```python theme={null}
from langchain_openai import ChatOpenAI

# Use GPU TEE model for sensitive reasoning
tee = ChatOpenAI(
    model="z-ai/glm-5.1",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)

# Use confidential (is_tee) model for systems engineering
near = ChatOpenAI(
    model="z-ai/glm-5",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)

# Use Claude for reasoning tasks
claude = ChatOpenAI(
    model="anthropic/claude-sonnet-4.5",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)

# Use DeepSeek for coding tasks
deepseek = ChatOpenAI(
    model="deepseek/deepseek-chat-v3.1",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)
```

### Conversation Chains

```python theme={null}
from langchain_openai import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

chat = ChatOpenAI(
    model="openai/gpt-5",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)

conversation = ConversationChain(
    llm=chat,
    memory=ConversationBufferMemory()
)

# Multi-turn conversation
response1 = conversation.predict(input="Hi, I'm learning about AI")
print(response1)

response2 = conversation.predict(input="What should I learn first?")
print(response2)
```

## TypeScript/JavaScript Integration

### Basic Setup

```typescript theme={null}
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage, SystemMessage } from "@langchain/core/messages";

const chat = new ChatOpenAI({
  model: "openai/gpt-5",
  apiKey: process.env.API_KEY,
  configuration: {
    baseURL: "https://api.redpill.ai/v1"
  },
  temperature: 0.7
});

const messages = [
  new SystemMessage("You are a helpful AI assistant."),
  new HumanMessage("Explain quantum computing in simple terms")
];

const response = await chat.invoke(messages);
console.log(response.content);
```

### Streaming in TypeScript

```typescript theme={null}
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage } from "@langchain/core/messages";

const chat = new ChatOpenAI({
  model: "openai/gpt-5",
  apiKey: process.env.API_KEY,
  configuration: {
    baseURL: "https://api.redpill.ai/v1"
  },
  streaming: true
});

const stream = await chat.stream([
  new HumanMessage("Write a story about AI")
]);

for await (const chunk of stream) {
  process.stdout.write(chunk.content);
}
```

## RAG (Retrieval Augmented Generation)

```python theme={null}
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_core.documents import Document
from langchain.chains import RetrievalQA

# Initialize embeddings with the platform
embeddings = OpenAIEmbeddings(
    model="openai/text-embedding-3-small",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)

# Create vector store
documents = [
    Document(page_content="The platform is a TEE-protected AI gateway"),
    Document(page_content="The platform supports a broad range of AI models"),
    Document(page_content="All requests are hardware-protected")
]
vectorstore = Chroma.from_documents(documents, embeddings)

# Initialize chat model
chat = ChatOpenAI(
    model="openai/gpt-5",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)

# Create RAG chain
qa_chain = RetrievalQA.from_chain_type(
    llm=chat,
    retriever=vectorstore.as_retriever()
)

# Query
result = qa_chain.invoke("How many models does the platform support?")
print(result["result"])
```

## Function Calling with LangChain

```python theme={null}
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

# Define tools
@tool
def get_weather(location: str, unit: str = "celsius") -> str:
    """Get the current weather in a location"""
    return f"The weather in {location} is 22°{unit[0].upper()}"

@tool
def calculate(expression: str) -> float:
    """Calculate a mathematical expression"""
    return eval(expression)

# Initialize chat model
chat = ChatOpenAI(
    model="openai/gpt-5",
    api_key="YOUR_API_KEY",
    base_url="https://api.redpill.ai/v1"
)

# Create agent
tools = [get_weather, calculate]
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant"),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}")
])

agent = create_tool_calling_agent(chat, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)

# Execute
result = agent_executor.invoke({
    "input": "What's the weather in Paris and what's 25 + 17?"
})
print(result["output"])
```

## Environment Variables

Store your API key securely:

```bash .env theme={null}
API_KEY=YOUR_API_KEY
```

```python Python theme={null}
import os
from langchain_openai import ChatOpenAI

chat = ChatOpenAI(
    model="openai/gpt-5",
    api_key=os.environ["API_KEY"],
    base_url="https://api.redpill.ai/v1"
)
```

## Supported Models

The available models work with LangChain:

| Provider  | Example Models                                             |
| --------- | ---------------------------------------------------------- |
| GPU TEE   | `z-ai/glm-5.1`, `z-ai/glm-5`, `qwen/qwen3.5-27b`           |
| OpenAI    | `openai/gpt-5`, `openai/gpt-5-mini`, `openai/o4-mini`      |
| Anthropic | `anthropic/claude-sonnet-4.5`, `anthropic/claude-opus-4.1` |
| Google    | `google/gemini-2.5-flash`                                  |
| DeepSeek  | `deepseek/deepseek-chat-v3.1`                              |

<Card title="View All Models" icon="list" href="/api-reference/models">
  Browse complete model list →
</Card>

## Best Practices

<AccordionGroup>
  <Accordion title="Use Environment Variables">
    Never hardcode API keys. Use environment variables or secret managers.
  </Accordion>

  <Accordion title="Choose the Right Model">
    * **GPU TEE models**: Best for sensitive data (full TEE protection)
    * **GLM 5.1**: Best for confidential reasoning
    * **GLM-5**: Best for confidential systems engineering
    * **GPT-5**: Best for general tasks
    * **Claude Sonnet 4.5**: Best for reasoning and analysis
    * **DeepSeek**: Best for coding tasks
  </Accordion>

  <Accordion title="Enable Streaming for UX">
    Use streaming for better user experience in interactive applications.
  </Accordion>

  <Accordion title="Implement Error Handling">
    Wrap API calls in try-catch blocks to handle rate limits and errors gracefully.
  </Accordion>
</AccordionGroup>

## Example Projects

<CardGroup cols={2}>
  <Card title="Chatbot" icon="comment">
    Build a multi-turn conversational AI with memory
  </Card>

  <Card title="RAG System" icon="database">
    Create a document Q\&A system with embeddings
  </Card>

  <Card title="AI Agent" icon="robot">
    Build an autonomous agent with function calling
  </Card>

  <Card title="Content Generator" icon="pen">
    Generate articles, summaries, and creative content
  </Card>
</CardGroup>

## Need Help?

* [LangChain Documentation](https://python.langchain.com/)
* [RedPill Discord](https://discord.gg/P2ukR4Z5ps)
* [GitHub Examples](https://github.com/redpill-ai/examples)
