LangChain

What is LangChain? LangChain is a comprehensive framework for developing applications powered by Large Language Models (LLMs). It provides:

  • A unified interface to interact with various model providers

  • Tools to manage conversation history

  • Primitives for building complex chains and agents

Sample implementation using nexos.ai

Below is a minimal example written in Python and TypeScript showing how to use LangChain’s ChatOpenAI client with the nexos.aiarrow-up-right Gateway (OpenAI-compatible) endpoint.

Python:

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

# Load environment variables from .env file
load_dotenv()

# --- Configuration ---
NEXOS_BASE_URL = os.getenv("NEXOS_BASE_URL")
NEXOS_API_KEY = os.getenv("NEXOS_API_KEY")

if not NEXOS_BASE_URL or not NEXOS_API_KEY:
    raise ValueError("Please set NEXOS_BASE_URL and NEXOS_API_KEY in your .env file")

def main():
    # Initialize the ChatOpenAI client
    llm = ChatOpenAI(
        model="gpt-4.1",          # or any other OpenAI-compatible model ID available to you
        base_url=NEXOS_BASE_URL,  # e.g. "https://api.nexos.ai/v1"
        api_key=NEXOS_API_KEY,
    )

    # Create a simple message sequence
    messages = [
        SystemMessage(content="You are a helpful assistant."),
        HumanMessage(content="Hello world!"),
    ]

    try:
        response = llm.invoke(messages)
        print("\n--- Response from AI ---")
        print(response.content)
        print("------------------------")
    except Exception as e:
        print(f"\nError communicating with the API: {e}")

if __name__ == "__main__":
    main()

TypeScript:

You can use any open AI compatible model. To check what models are available for you, call Gateway API | nexos.ai documentationarrow-up-right You can use either nexos_model_idor id as model.

Last updated