Sending Requests to Remote Graph Server

Open in ColabOpen in GitHub

Overview

In this tutorial, we will learn how to launch an application server and send Python requests to Remote Graph.

In this process, we will:

  1. Understand the differences between LangServe and LangGraph

  2. Learn why LangGraph is recommended

  3. Get hands-on experience through the Chat LangChain application

We will examine each step in detail, from environment setup to server launch and sending actual requests. Through this tutorial, you will be able to build a foundation for AI application development using LangGraph.

Let's get started!

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

What is LangServe and LangGraph

Before proceeding with this tutorial, there are concepts you need to understand. These are LangServe and LangGraph. Let's make sure to clarify the difference between these two.

LangServe

LangServe helps developers deploy LangChain runnables and chains as a REST API. Through the built-in Runnable object, you can easily create data pipelines from various components. These pipelines are ultimately provided through APIs called invoke, batch, and stream. This library is integrated with FastAPI and uses pydantic for data validation. In addition, it provides a client that can be used to call into runnables deployed on a server.

LangGraph

LangGraph Platform is a commercial solution for deploying agentic applications to production, built on the open-source LangGraph framework.

  • LangGraph Server : The server defines an opinionated API and architecture that incorporates best practices for deploying agentic applications, allowing you to focus on building your agent logic rather than developing server infrastructure.

  • LangGraph Studio : LangGraph Studio is a specialized IDE that can connect to a LangGraph Server to enable visualization, interaction, and debugging of the application locally.

  • LangGraph CLI : LangGraph CLI is a command-line interface that helps to interact with a local LangGraph

  • Python/JS SDK : The Python/JS SDK provides a programmatic way to interact with deployed LangGraph Applications.

  • Remote Graph : A RemoteGraph allows you to interact with any deployed LangGraph application as though it were running locally.

langchain-ai recommend using LangGraph Platform rather than LangServe for new projects. Langchain-ai will continue to accept bug fixes for LangServe from the community; however, Langchain-ai will not be accepting new feature contributions.

In contrast to LangServe, LangGraph Platform provides comprehensive, out-of-the-box support for persistence, memory, double-texting handling, human-in-the-loop workflows, cron job scheduling, webhooks, high-load management, advanced streaming, support for long-running tasks, background task processing, and much more.

Why use LangGraph?

LangGraph has been designed so that developers can concentrate exclusively on developing AI agent features. Here are the key features and advantages of the LangGraph Platform:

  1. Real-time Processing Features

  • Streaming Support: Ability to monitor complex task progress in real-time

  • Double Text Processing: Reliably manage rapid consecutive user messages

  • Burst Handling: Stable processing through queue system even with multiple simultaneous requests

  1. Long-running Task Management

  • Background Execution: Reliably handle tasks that take hours to complete

  • Long-run Support: Prevent unexpected connection drops through heartbeat signals

  • Status Monitoring: Track execution status through polling and webhooks

  1. Data Management

  • Checkpoint Functionality: Save and recover task states

  • Memory Management: Maintain data like conversation history across sessions

  • Ready-to-use Storage Solution: Available without custom configuration

  1. User Intervention Support

  • Human-in-the-loop: Allow user intervention in processes when needed

  • Dedicated Endpoints: Special access points for manual supervision

These features allow developers to focus on agent development rather than infrastructure building.

Practice with Chat LangChain Application

To simply test server communication, please download and run the code locally through the Chat LangChain tutorial provided by LangChain. We will try using a simple chatbot that uses graphs. Please visit the project repository to prepare the code.

There are essential environment variables that must be set before running the code.

  • OPENAI_API_KEY: your_secret_key_here

  • LANGCHAIN_TRACING_V2: "true"

  • LANGCHAIN_PROJECT: langserve-launch-example

  • LANGCHAIN_API_KEY: your_secret_key_here

  • FIREWORKS_API_KEY: your_secret_here

  • WEAVIATE_API_KEY: your_secret_key_here

  • WEAVIATE_URL: https://your-weaviate-instance.com(or https://weaviate.io/developers/weaviate/connections/connect-cloud)

  • WEAVIATE_INDEX_NAME: your_index_name

  • RECORD_MANAGER_DB_URL: your_db_url (e.g. postgresql://postgres:[YOUR_DB_PASSWORD]@db.daxpgrzsg.supabase.co:5432/postgres)

Run the following command to start the server locally.

The server will be launched this way.

Image Description
  • API: http://127.0.0.1:2024

  • Studio UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024

  • API Docs: http://127.0.0.1:2024/docs

API (http://127.0.0.1:2024) is the main API endpoint, serving as the base address for direct communication with the server.

Studio UI (https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024) is LangChain's web-based development environment that provides an interface for visually inspecting graphs and debugging.

Image Description
Image Description

API Docs (http://127.0.0.1:2024/docs) is an API documentation page that contains Swagger documentation where you can find all available endpoints and their usage instructions.

Image Description

After running this code, let's check the server logs.

Image Description

Thread-level persistence

Thread-level persistence is a method of maintaining the "memory" of conversations or tasks. It allows a program to "remember" the content of previous conversations or operations. It's similar to writing down important information in a notebook and being able to refer back to it later. Simple graph executions are stateless. Being stateless means that checkpoints and the final state of the graph are not saved.

If you want to persist the outputs of graph execution (for example, to enable human-in-the-loop features), you can create a thread and provide the thread ID via the config argument, just as you would with a compiled graph.

After setting the thread ID, we will proceed to ask about the weather in San Francisco.

Let's run the two code examples below that include the following questions:

Question
Expected Answer

Did I ask about the weather in SF earlier?

YES

Did I ask about the weather in LA earlier?

NO

The system remembers your previous questions and responds based on that context.

Since there was no prior request about LA weather, the system would respond accordingly, as demonstrated above.

Remove the Thread ID

Now let's remove the thread ID. Removing the thread ID means the LLM will no longer retain context from previous conversations.

First, I will ask about the weather in San Francisco.

Will the LLM remember that I previously asked about San Francisco's weather?

No, without a thread ID, it cannot retain memory of prior conversations.

Using as a subgraph

This code explains how to use a remote graph (RemoteGraph) as a subgraph of another graph. Here's a summary of the main points:

  1. Setting up the remote graph:

    • Import a graph deployed on a remote server as a RemoteGraph.

  2. Creating the parent graph:

    • Use StateGraph to create a new graph.

    • This graph manages message states.

  3. Adding the remote graph as a subnode:

    • Directly add the remote graph as a node in the parent graph.

    • Create a connection from the start node to this subgraph.

  4. Executing the graph:

    • Run the completed graph to obtain results.

    • Results can also be received in a streaming manner.

This approach allows easy integration of complex remote graphs as part of a local graph. This helps increase modularity and reusability.

Summary

Unfortunately, Langchain is not currently recruiting for beta testing of Langchain deploy. However, you could try deploying through various hosting services locally.

Last updated