Source : Pexels/Pixabay

Member-only story

LangChain

Keno Leon
10 min readJun 20, 2024

--

What | is | it ?

One of the characteristics of new technologies is that sometimes the new products and services are not immediately easy to grasp, you are telling me you can sell a book online !? Why, how, where ? I think many people asked these questions at the dawn of the internet, but more relevant the tools behind the scene need some learning too.

LangChain sits somewhere between a new product type: LLMs/AIs as software components and the new set of tooling required to integrate them into your code or project, so let’s check it out.

Bring your own AI

Before we start you need API Keys for LLMs or other AIs you plan to use, that is LangChain doesn’t include the AI proper but facilitates using them, I opted for the tried and tested OpenAI API, you do need to have some credits ( $5.00 should work ).

Basic Chains

So what’s a chain anyways ? In Langchains parlance it is a sequence of operations that involve processing inputs, interacting with a language model like an LLM or other AIs, and handling the outputs.

The metaphor of a chain seems a bit forced at this point, but imagine that each process is a link in a chain.

And a simple code example of a basic chain :

from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.output_parsers import StrOutputParser

# INPUT PROCESSING :
query = "t-shirt"

# LLM CALL :
messages = [
SystemMessage(content="Translate the following from English
into Spanish"
),
HumanMessage(content=query),
]


# OUTPUT PROCESSING :
parser = StrOutputParser()
result = model.invoke(messages)
print(parser.invoke(result))

# >>> camiseta

Couple of things :

There’s considerable set up to get this bit of code to run, consult your chosen LLM/AI API reference, you also need to authenticate yourself with LangChain.

Speaking of hardships I found the docs more confusing than helpful at times, changes and complexity compound the issue and I kept on finding ways to do things (important things) that were never fully explained or delegated to a code sample somewhere in an API reference page, here for instance is a minimal chat prompt…

--

--

Keno Leon
Keno Leon

Responses (5)