# LangChain

[LangChain](https://www.langchain.com/) is a framework for building and deploying applications powered by large language models (LLMs), streamlining the entire lifecycle from development to production.&#x20;

It offers open-source components and integrations for building applications, LangGraph for creating stateful agents with streaming and human-in-the-loop support, and LangSmith for inspecting, monitoring, and evaluating performance to ensure optimization.&#x20;

Finally, with LangGraph Platform, developers can deploy their applications as production-ready APIs and Assistants.

This guide will walk you through the setup.

{% hint style="info" %}
*Before you begin: Make sure you have generated your Cortecs API key. If not, check out our* [*QuickStart*](/quickstart.md) *guide.*
{% endhint %}

## 1. Install LangChain&#x20;

LangChain offers both [Python](https://python.langchain.com/docs/how_to/installation/) and [JavaScript](https://js.langchain.com/docs/how_to/installation/) libraries.

To get started quickly, follow the installation instructions in the documentation for your preferred language.

Once the framework is installed, you can begin building applications by connecting to an external provider.

## 2. Connect to cortecs

Cortecs provides **OpenAI-compatible API endpoints**, making it easy to integrate with LangChain and many other tools.

To connect and use the LLM, create an `ChatOpenAI` object, with the following settings:

– `api_key`: Your API Key\
– `base_url`:  <code class="expression">space.vars.DEFAULT\_API\_BASE\_URL</code>\
– `model`: <code class="expression">space.vars.DEFAULT\_CHAT\_MODEL</code> (feel free to pick another one from the [catalogue](https://cortecs.ai/serverlessModels))\
– (optional) `extra_body`: `{"preference": "balanced"}`

```python
from langchain_openai import ChatOpenAI
from pydantic import SecretStr

llm = ChatOpenAI(
    model="gpt-5-mini",
    # you also can use env vars to configure base_url and api_key
    api_key=SecretStr("eyJhbG***"),
    base_url="https://api.cortecs.ai/v1",
    extra_body= {"preference": "balanced"}
)

print(llm.invoke("Hi").content)
```

### 2.1. Embedding Model

To create embeddings and use LangChain with vector databases, you can instantiate an `OpenAIEmbeddings` object with the following settings:

– `api_key`: Your API Key\
– `base_url`:  <code class="expression">space.vars.DEFAULT\_API\_BASE\_URL</code>\
– `model`: <code class="expression">space.vars.DEFAULT\_EMBEDDING\_MODEL</code> (feel free to pick another embedding model from the [catalogue](https://cortecs.ai/serverlessModels))\
– (optional) `model_kwargs`: `{"extra_body": {"preference": "balanced"}}`

```python
from langchain_openai import OpenAIEmbeddings
from pydantic import SecretStr

embedding_model = OpenAIEmbeddings(
    model="text-embedding-3-large",
    api_key=SecretStr("eyJhbG***"),
    base_url="https://api.cortecs.ai/v1",
    model_kwargs= {"extra_body": {"preference": "balanced"}}
)

print(embedding_model.embed_query("Hi"))
```

Enjoy your **privacy-preserving LLM applications** with the power of Cortecs and LangChain. Explore other models, tweak the setup to your needs, and join the conversation on our [Discord](https://discord.com/invite/bPFEFcWBhp) to share feedback 👩‍💻👨‍💻


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cortecs.ai/integration-examples/tooling/langchain.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
