kioku-space

As a personal memory space.

Text-to-SQL with LangChain (2) - Agent

Continuing from last post, we’ll explore the implementation of ‘Text-to-SQL’, where you can query database information in natural language and get answers, based on LangChain’s Quickstart documentation.

In this post, we’ll look into the operation of an agent created using the create_sql_agent function.

  • In this article, we’re using LangChain version 0.2.0.

    $ pip list|grep langchain
    langchain                0.2.0
    langchain-community      0.2.0
    langchain-core           0.2.0
    langchain-openai         0.1.7
    langchain-text-splitters 0.2.0

Prepare the environment similarly to last post.

Text-to-SQL with LangChain (1) - Chain

In this post, we’ll dive into how to set up Text-to-SQL with LangChain, guided by the Q&A over SQL+CSV Quickstart.

  • We’ll be using LangChain version 0.2.0.

    $ pip list|grep langchain
    langchain                0.2.0
    langchain-community      0.2.0
    langchain-core           0.2.0
    langchain-openai         0.1.7
    langchain-text-splitters 0.2.0

First, install the necessary libraries.

Reading LangChain's Summarization Code (3) - Refine

In this series, we explore the mechanism behind the text summarization chain introduced in LangChain’s Summarization documentation.

In this post, we focus on the Refine summarization method (chain_type="refine").

  • This article uses LangChain version 0.1.17.

    $ pip list|grep langchain
    langchain                0.1.17
    langchain-community      0.0.37
    langchain-core           0.1.52
    langchain-openai         0.1.6
    langchain-text-splitters 0.0.1
    langchainhub             0.1.15

Let’s take a look at the code for the chain_type="refine" scenario. The summarization chain in this case is generated by the following code:

Reading LangChain's Summarization Code (2) - Map Reduce

In this series, we’re diving into the mechanics of LangChain’s summarization chains as outlined in the LangChain documentation on Summarization.

This post focuses on the Map Reduce method (chain_type="map-reduce") for summarization.

  • This article uses LangChain version 0.1.17.

    $ pip list|grep langchain
    langchain                0.1.17
    langchain-community      0.0.37
    langchain-core           0.1.52
    langchain-openai         0.1.6
    langchain-text-splitters 0.0.1
    langchainhub             0.1.15

Let’s take a look at the code for chain_type="map-reduce". For the Map Reduce method, a summarization chain is created using the following code:

Reading LangChain's Summarization Code (1) - Stuff

In this post, we’ll explore how the summarization chain in LangChain works, as outlined in the LangChain documentation on Summarization.

  • This article uses LangChain version 0.1.17.

    $ pip list|grep langchain
    langchain                0.1.17
    langchain-community      0.0.37
    langchain-core           0.1.52
    langchain-openai         0.1.6
    langchain-text-splitters 0.0.1
    langchainhub             0.1.15

First, let’s take a look at the summarization code introduced in the Quickstart section of the documentation.

Exploring LangChain's Quickstart (5) - Serve as a REST API (LangServe)

This series dives into how to use LangChain, based on the LangChain Quickstart guide.
In this post, we’ll explore how to deploy LangChain agents as a REST API using LangServe.

Recap of the Previous Post

In our last post, we created an agent that combines tools for answering LangChain-related queries with internet search capabilities.
Here’s a recap of the code we used:

import os

from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain.tools.retriever import create_retriever_tool
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

# Set up the API key as environment variable
with open(".openai") as f:
    os.environ["OPENAI_API_KEY"] = f.read().strip()

# Load web page content
loader = WebBaseLoader("https://python.langchain.com/docs/get_started/introduction")
docs = loader.load()

# Load embeddings
embeddings = OpenAIEmbeddings()

# Split the documents
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)

# Vectorize documents and create a vector store
vector = FAISS.from_documents(documents, embeddings)

# Create a retriever
retriever = vector.as_retriever()

# Search tool for document retrieval
retriever_tool = create_retriever_tool(
    retriever,
    "langchain_search",
    "A search tool for LangChain-related queries. Use this for questions about LangChain!",
)

# Set Tavily API key
with open(".tavily") as f:
    os.environ["TAVILY_API_KEY"] = f.read().strip()

# Internet search tool
search = TavilySearchResults()

# List of tools in use
tools = [retriever_tool, search]

# Get template from LangChain Hub
template = hub.pull("hwchase17/openai-functions-agent")

# Create an agent
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent = create_openai_functions_agent(llm, tools, template)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

First, install LangServe with the following command: