kioku-space

As a personal memory space.

Exploring LangChain's Quickstart (4) - Dynamically Select the Tools (Agent)

In this series, we explore the ‘Quickstart’ section of the LangChain documentation.

Previously, we developed chains that operated on predefined steps. In this article, we explore how the LLM chooses the right tools for processing based on user input, introducing the concept of an agent.

In a previous post, we built a retriever from LangChain’s documentation.
Let’s revisit that setup:

import os
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
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 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()

In this article, we’re creating an agent that utilizes the retriever if the user’s question concerns LangChain. For other topics, it will conduct internet searches. The LLM will determine which process to employ based on the query.

Exploring LangChain's Quickstart (3) - Utilizing Conversation History

In this post, we continue our journey through LangChain’s Quickstart guide, exploring how to enhance your chains by integrating conversation history.

Here’s what we’ve set up so far:

  • retriever: Retrives a list of relevant documents based on the input text.
  • document_chain: Generates LLM responses using the user’s questions and the list of documents.
  • create_retrieval_chain: Combines retriever and document_chain to answer queries by referencing documents.
import os
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter

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

# Loading the LLM
llm = ChatOpenAI()

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

# Loading embeddings
embeddings = OpenAIEmbeddings()

# Splitting documents
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)

# Vectorizing documents and creating a vector store
vector = FAISS.from_documents(documents, embeddings)

# Creating a retriever
retriever = vector.as_retriever()

To incorporate conversation history, we make the following adjustments:

Exploring LangChain's Quickstart (2) - Extending LLM knowledge

In this series, we’ll explore the ‘Quickstart’ section of the LangChain documentation.
In this article, we discuss how to expand LLM knowledge using information on the internet.

Below, I outline the sections of code from our previous article that we’ll use again in this article.

import os
from langchain_openai import ChatOpenAI

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

# Load the large language model
llm = ChatOpenAI()

As we’ve seen, the gpt-3.5-turbo we’ve been using has outdated knowledge, failing to correctly answer “What is LangChain?”.

Exploring LangChain's Quickstart (1) - LLM, Prompt Template, and Chain

In this series, we’ll explore the ‘Quickstart’ section of the LangChain documentation.
In this article, we focus on LLMs, prompt templates, and chains.

To get started, install langchain and its OpenAI extension, langchain-openai:

pip install langchain
pip install langchain-openai
  • We are using the following versions. Note that LangChain often introduces breaking changes, so be careful with different versions:

Developing a Memo App with Tkinter (2) - Saving a Memo to a Text File

Continuing from the previous “Hello World” program, this session dives into building a memo app with the following functionalities:

  1. A text box for entering a memo.
  2. A shortcut (Control+S) for saving the contents of the text box to a file.
  3. Displaying the file contents in the text box upon app launch.

Let’s get started by building on our previous program. First, we create the root window and place a frame inside it. Within this frame, we’ll set up the text box for memo entry.

Developing a Memo App with Tkinter (1) - Running a 'Hello World' Program

In this series, we’ll be leveraging Tkinter, Python’s built-in GUI library, to create a memo app.

Let’s begin by exploring how to use Tkinter with the following “Hello World” program outlined in its official documentation.

from tkinter import *
from tkinter import ttk

root = Tk()

frm = ttk.Frame(root, padding=10)
frm.grid()

ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)

root.mainloop()

Upon execution, this script will display a window like the one shown below.