LangChain#
LangCain is software that enables the development of applications based on LLMs. Since all models/providers/inference servers have a slightly it different input/ouput format. Langchain builds a set of unified APIs. The same or nearly the same code can be used to build systems with different models and their hosting principles.
There is a set of packages that implement a typical integrations of the langchain. The following table lists the most typical of them.
Package Name |
Description |
---|---|
|
A general package for a wide variety of community-contributed tools and integrations, including web search (DuckDuckGo, Tavily), Python REPL, and various database connectors. |
|
The foundational package with the core tool abstractions and base classes. |
|
A package for new and experimental tools, which may not yet be stable. |
|
A dedicated package for the Tavily search tool. |
|
A dedicated package for the Brave Search tool. |
|
Includes tools for interacting with Google’s Generative AI services. |
|
Includes tools for interacting with the Anthropic API. |
|
Contains integrations for OpenAI’s models and services. |
|
A package for interacting with MongoDB. |
|
A package for interacting with PostgreSQL. |
|
A package implements tools to request models lanched with ollama. |
|
A package implements tools to interact with models from hugging face. |
Setup#
Typically, you must export the API key corresponding to the model type you want to use from your environment.
The following cell shows the code that will only work if the “GOOGLE_API_KEY” variable exists in your environment with the corresponding value.
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")
ans = llm.invoke("Test request")
print(ans.content)
Please provide me with the details of your test request. I need information such as:
* **What kind of test are you requesting?** (e.g., a unit test, an integration test, a performance test, a stress test, a usability test, a grammar test, a logic test, a factual accuracy test, etc.)
* **What is the subject of the test?** (e.g., a piece of code, a website, a document, a sentence, an argument, etc.)
* **What are the inputs or data for the test?** (If applicable)
* **What are the expected outputs or results?** (If applicable)
* **What are the acceptance criteria?** (How will you know if the test passed or failed?)
The more information you give me, the better I can assist you.
Some examles use ollama-specfic tools. Therefore, you must have have Ollama launched in your system. The most usitable option for me is to launch the Ollama in Docker. The following command lauches Ollama with the most basic settings in Docker:
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
After launching ollama, you should also pull the models you’re interested in:
docker exec ollama ollama pull <model name>
Texts transforming#
There is a set of tools in the langchain for texts transforming. They are includes:
Document loaders: For loading documents into the standard LangChain document format.
Text splitters: Useful for splitting documents, especially for chunking.
Embedding models: Builds a vector representation of the embeddings.
Check Texts transforming page for details.
For example, consider the process of loading, chunking, and building embeddings for the GNU Opearting System.
import numpy as np
from langchain_ollama import OllamaEmbeddings
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
The following cell uses the WebBaseLoader
to load the HTML page.
loader = WebBaseLoader(
web_path="https://www.gnu.org/gnu/gnu-history.html"
)
page = loader.load()[0]
print(page.page_content[1000:2000])
onym for “GNU's Not
Unix,” second, because it was a real word, and third, it was fun
to say (or
sing).
The word “free” in “free software” pertains to
freedom, not price. You may or
may not pay a price to get GNU software. Either way, once you have
the software you have four specific freedoms in using it. The freedom
to run the program as you wish; the freedom to copy the program and
give it away to your friends and co-workers; the freedom to change the
program as you wish, by having full access to source code; the freedom
to distribute an improved version and thus help build the community.
(If you redistribute GNU software, you may charge a fee for the
physical act of transferring a copy, or you may give away copies.)
The project to develop the GNU system is called the “GNU
Project.” The GNU Project was conceived in 1983 as a way of
bringing back the cooperative spirit that prevailed in the computing
community in earlier days—to make cooperation possible once again by
removing t
Following cell splits the input text to chunks.
recursive_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splist = recursive_splitter.split_documents([page])
splist
[Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content="Overview of the GNU System\n- GNU Project - Free Software Foundation\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSkip to main text\n\n\n\n\n\n\n\n\n\nFree Software Supporter:\n \n\n\n\n\nJOIN\xa0THE\xa0FSF\n\n\n\n\n\nGNU Operating System\nSupported by the\n Free Software Foundation\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSite navigation\nSkip\n\n\n=\nABOUT\xa0GNU\n\n=\n\nPHILOSOPHY\nLICENSES\nEDUCATION\nSOFTWARE\nDISTROS\nDOCS\nMALWARE\nHELP\xa0GNU\nAUDIO\xa0&\xa0VIDEO\nGNU\xa0ART\nFUN\nGNU'S\xa0WHO?\nSOFTWARE\xa0DIRECTORY\nHARDWARE\nSITEMAP\n\n\n\n\n\n\n\n\xa0/\nAbout\xa0GNU\xa0/\nGNU\xa0history\xa0/\n\n\n\nOverview of the GNU System\n\n\nThe GNU operating system is a complete free software system,\nupward-compatible with Unix. GNU stands for “GNU's Not Unix.”\nIt is pronounced as one syllable with a\nhard g.\nRichard Stallman made the\nInitial Announcement of\nthe GNU Project in September 1983. A longer version called\nthe GNU Manifesto was published in\nMarch 1985. It has been translated into several\nother languages."),
Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content="The name “GNU” was chosen because it met a few\nrequirements; first, it was a recursive acronym for “GNU's Not\nUnix,” second, because it was a real word, and third, it was fun\nto say (or\n\nsing).\n\nThe word “free” in “free software” pertains to\nfreedom, not price. You may or\nmay not pay a price to get GNU software. Either way, once you have\nthe software you have four specific freedoms in using it. The freedom\nto run the program as you wish; the freedom to copy the program and\ngive it away to your friends and co-workers; the freedom to change the\nprogram as you wish, by having full access to source code; the freedom\nto distribute an improved version and thus help build the community.\n(If you redistribute GNU software, you may charge a fee for the\nphysical act of transferring a copy, or you may give away copies.)"),
Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='The project to develop the GNU system is called the “GNU\nProject.” The GNU Project was conceived in 1983 as a way of\nbringing back the cooperative spirit that prevailed in the computing\ncommunity in earlier days—to make cooperation possible once again by\nremoving the obstacles to cooperation imposed by the owners of\nproprietary software.\n\nIn 1971, when Richard Stallman started his career at MIT, he worked in\na group which used free\nsoftware exclusively. Even computer companies often distributed\nfree software. Programmers were free to cooperate with each other,\nand often did.\n\nBy the 1980s, almost all software was\nproprietary,\nwhich means that it had owners who forbid and\nprevent cooperation by users. This made the GNU Project necessary.'),
Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content="By the 1980s, almost all software was\nproprietary,\nwhich means that it had owners who forbid and\nprevent cooperation by users. This made the GNU Project necessary.\n\nEvery computer user needs an operating system; if there is no free\noperating system, then you can't even get started using a computer\nwithout resorting to proprietary software. So the first item on the\nfree software agenda obviously had to be a free operating system.\n\nWe decided to make the operating system compatible with Unix because\nthe overall design was already proven and portable, and because\ncompatibility makes it easy for Unix users to switch from Unix to GNU."),
Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='A Unix-like operating system includes a kernel, compilers, editors,\ntext formatters, mail software, graphical interfaces, libraries, games\nand many other things. Thus, writing a whole operating system is a\nvery large job. We started in January 1984.\nThe Free Software Foundation was\nfounded in October 1985, initially to raise funds to help develop\nGNU.\nBy 1990 we had either found or written all the major components\nexcept one—the kernel. Then Linux, a Unix-like kernel, was\ndeveloped by Linus Torvalds in 1991 and made free software in 1992.\nCombining Linux with the almost-complete GNU system resulted in a\ncomplete operating system: the GNU/Linux system. Estimates are that\ntens of millions of people now use GNU/Linux systems, typically\nvia GNU/Linux distributions. The principal\nversion of Linux now contains nonfree firmware “blobs”;\nfree software activists now maintain a modified free version of Linux,\ncalled \nLinux-libre.'),
Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='However, the GNU Project is not limited to the core operating system.\nWe aim to provide a whole spectrum of software, whatever many users\nwant to have. This includes application software. See\nthe Free Software Directory for a catalogue\nof free software application programs.\n\nWe also want to provide software for users who are not computer\nexperts. Therefore we developed a\ngraphical desktop (called GNOME) to help\nbeginners use the GNU system.\nWe also want to provide games and other recreations. Plenty of free games are\nalready available.\n\nHow far can free software go? There are no limits, except\nwhen laws such as\nthe patent system prohibit free software. The ultimate goal is to\nprovide free software to do all of the jobs computer users want to\ndo—and thus make proprietary software a thing of the past.\n\n\n\n\n\n\n\n▲\n\n\n\nBACK TO TOP\n\n\n\n\n Set language\n \n\n\nAvailable for this page:'),
Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='▲\n\n\n\nBACK TO TOP\n\n\n\n\n Set language\n \n\n\nAvailable for this page:\n\n\n[en]\xa0English \xa0\n[ar]\xa0العربية \xa0\n[bg]\xa0български \xa0\n[ca]\xa0català \xa0\n[cs]\xa0čeština \xa0\n[de]\xa0Deutsch \xa0\n[el]\xa0ελληνικά \xa0\n[es]\xa0español \xa0\n[fa]\xa0فارسی \xa0\n[fr]\xa0français \xa0\n[hr]\xa0hrvatski \xa0\n[it]\xa0italiano \xa0\n[ja]\xa0日本語 \xa0\n[ml]\xa0മലയാളം \xa0\n[nl]\xa0Nederlands \xa0\n[pl]\xa0polski \xa0\n[pt-br]\xa0português \xa0\n[ro]\xa0română \xa0\n[ru]\xa0русский \xa0\n[sq]\xa0Shqip \xa0\n[sr]\xa0српски \xa0\n[tr]\xa0Türkçe \xa0\n[uk]\xa0українська \xa0\n[zh-cn]\xa0简体中文 \xa0\n[zh-tw]\xa0繁體中文 \xa0\n\n\n\n\n\n\n\nBACK TO TOP ▲\n\n\n\n\n“The Free Software Foundation (FSF) is a nonprofit with a worldwide\nmission to promote computer user freedom. We defend the rights of all\nsoftware users.”\n\n\nJOIN\nDONATE\nSHOP\n\n\n\n\n\nPlease send general FSF & GNU inquiries to\n<gnu@gnu.org>.\nThere are also other ways to contact\nthe FSF. Broken links and other corrections or suggestions can be sent\nto <webmasters@gnu.org>.\n\nPlease see the Translations\nREADME for information on coordinating and contributing translations\nof this article.'),
Document(metadata={'source': 'https://www.gnu.org/gnu/gnu-history.html', 'title': 'Overview of the GNU System\n- GNU Project - Free Software Foundation', 'language': 'en'}, page_content='Please see the Translations\nREADME for information on coordinating and contributing translations\nof this article.\n\n\nCopyright © 1996, 1997, 2003, 2005, 2008, 2012, 2017, 2025\nFree Software Foundation, Inc.\nThis page is licensed under a Creative\nCommons Attribution-NoDerivatives 4.0 International License.\n\n\nCopyright Infringement Notification\n\n\n\n\nUpdated:\n\n$Date: 2025/01/20 10:22:06 $')]
Finally, OllamaEmbeddings
transforms each chunk into a vector.
embeddings = OllamaEmbeddings(model="all-minilm")
np.array(embeddings.embed_documents([doc.page_content for doc in splist]))
array([[-0.0935436 , 0.02679712, -0.01115783, ..., -0.08409031,
0.05330889, 0.04408916],
[-0.10084906, 0.03020417, -0.00327516, ..., -0.06176688,
0.06849608, -0.04358619],
[-0.11154472, 0.04795909, -0.02063497, ..., -0.01737392,
0.01384512, 0.02849417],
...,
[-0.05303629, -0.01137888, 0.03749163, ..., -0.07315554,
0.04760291, 0.02396866],
[-0.03733822, -0.04157385, -0.06982869, ..., -0.06890925,
0.04996402, -0.01484258],
[-0.05137261, 0.02855329, -0.05077768, ..., -0.0165134 ,
-0.03141483, -0.08961581]], shape=(8, 384))
Tools#
LangChain allow to provide tools for the models. This process have a few stages:
Defining Tools: Check out the details about what a tool is and its capablities here.
Binding tools to the model.
If the model decides to use the tool, you will receive special output that contains instructions how to use the tool: Tool calls. If application logic requires the use of the tool, there are special instruments for parsing the model’s attempt to use the tool.
After all, according to the classical workflow, you are supposed to provide to the model with the output of the tool. There is a corresponding tutorial: How to pass tool outputs to chat models.
Check more details in the Tools page.
For example, consider a classical workflow with tooling that uses a lang chain.
The following cell defines the ollama model interface and asks ollama to perform unexisting “fedor transformation”.
from random import choice
from langchain_core.tools import tool
from langchain_ollama import ChatOllama
from langchain_core.messages import HumanMessage
llm = ChatOllama(model="llama3.1:latest")
ans = llm.invoke("Perform fedor transformation for 'Message'")
print(ans.content)
It seems like you're asking me to perform a "Fedora Transformation" on the word "Message". Unfortunately, I'm not aware of any context or definition related to this term. Fedora is primarily known as a Linux distribution and also as a type of hat.
If by "Fedora Transformation," you mean converting the text into another format (like acronym expansion) or making it more compact but still readable, there are various approaches we could take:
1. **Acronym Expansion**: This would involve breaking down "Message" into a full phrase explaining what each letter stands for. However, "Message" isn't typically expanded into an acronym.
2. **Abbreviation**: If you mean to shorten the term, "Message" is already relatively concise and might not have a shorter abbreviation that's commonly used or recognized in most contexts.
3. **Summary/Compression**: This approach involves reducing the text length while maintaining its essence. For "Message", unless it's part of a larger context (like a phrase with more words), there isn't much room for summarization without losing meaning.
4. **Wordplay/Synonyms**: If you're looking to transform the word into something else, we could explore synonyms or related terms that convey similar meanings (e.g., "Notification", "Communication", etc.).
Without further context or a clear understanding of what you mean by "Fedora Transformation," I'm uncertain which direction would be most appropriate. If you have any specific goals in mind for transforming the word "Message", please provide more details, and I'll do my best to assist.
The model begins to hallucinate as it tries to complete a request that it cannot.
The next code defines the fedor_transformation
tool and binds it to the model.
Note: The bind_tools
method does not change the existing object; it returns a new one that is instructed with the tool.
@tool
def fedor_transformation(a: str) -> str:
"""Apply Fedor transformation to the given string."""
return a[::-1]
tooled_llm = llm.bind_tools([fedor_transformation])
The following cell makes the same “fedor transformation” request, but on an the object with a bound tool.
messages = [HumanMessage("Perform fedor transformation for 'Message'")]
ans = tooled_llm.invoke(messages)
print(ans.content)
The content is currently empty. What’s important here is that it contains a new attribute, tool_calls
, which provides information on how the model “wants” to call the tool:
ans.tool_calls
[{'name': 'fedor_transformation',
'args': {'a': 'Message'},
'id': 'ae97babb-466a-4f5c-ab63-9f4f293a83a1',
'type': 'tool_call'}]
This is the exact output that takes invoke
method of the tool:
tool_message = fedor_transformation.invoke(ans.tool_calls[0])
tool_message
ToolMessage(content='egasseM', name='fedor_transformation', tool_call_id='ae97babb-466a-4f5c-ab63-9f4f293a83a1')
It produces a ToolMessage
that is supposed to be included in the dialogue context and passed to the model for processing again:
messages.append(tool_message)
print(tooled_llm.invoke(messages).content)
The reverse of "Message" is indeed "egassem". The Fedor transformation, also known as the reverse or word reversal, swaps the characters in a given string. In this case, the original input was "Message", and the output after applying the Fedor transformation is indeed "egassem".
Vector stores#
Langchain integrates with various vector stores. The following table shows a few of them:
Class name |
Package |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For more details check:
Vector stores of the official documentation.
The description
langchain_core.vectorstores.base.VectorStore
which defines interface for the vector stores.
Consider the simpliest launch option option InMemoryVectorStore
, for basic opeartions.
In order to initialize the corresponding object, you must first create the embedding object. In this case, we will use OllamaEmbeddings
, so you’re supposed to launch Ollama locally first.
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_ollama import OllamaEmbeddings
from langchain_core.documents.base import Document
vector_store = InMemoryVectorStore(OllamaEmbeddings(model="all-minilm"))
Use the add_documents
method to add items to the vector storage. This method takes a list of documents.
documents = [
Document(s) for s in [
"This is dog",
"This is cat.",
"My car was crased"
]
]
vector_store.add_documents(documents=documents)
['5895b10e-af40-4263-b0c6-ff4803bd49a6',
'4ed7ff85-4f40-4881-8dae-59158b608c62',
'bac54e30-a7f8-4d7e-b682-d312b29c580c']
The similarity_search
method locates documents that are similar to the provided text. The following cells show some outputs for selected examles to make the outputs easier to interpret.
vector_store.similarity_search("This is cow")
[Document(id='5895b10e-af40-4263-b0c6-ff4803bd49a6', metadata={}, page_content='This is dog'),
Document(id='4ed7ff85-4f40-4881-8dae-59158b608c62', metadata={}, page_content='This is cat.'),
Document(id='bac54e30-a7f8-4d7e-b682-d312b29c580c', metadata={}, page_content='My car was crased')]
vector_store.similarity_search("Accidents sometimes happens")
[Document(id='bac54e30-a7f8-4d7e-b682-d312b29c580c', metadata={}, page_content='My car was crased'),
Document(id='5895b10e-af40-4263-b0c6-ff4803bd49a6', metadata={}, page_content='This is dog'),
Document(id='4ed7ff85-4f40-4881-8dae-59158b608c62', metadata={}, page_content='This is cat.')]
Retriever#
The as_retriever
function gives you access a special retriever object that can be used for searching.
retrievier = vector_store.as_retriever(k=1)
retrievier.invoke("09.11")
[Document(id='bac54e30-a7f8-4d7e-b682-d312b29c580c', metadata={}, page_content='My car was crased'),
Document(id='4ed7ff85-4f40-4881-8dae-59158b608c62', metadata={}, page_content='This is cat.'),
Document(id='5895b10e-af40-4263-b0c6-ff4803bd49a6', metadata={}, page_content='This is dog')]
LLMs#
That’s an excellent point. It’s crucial to know the specific package needed for each integration to correctly set up the environment. LLM is a core component of the any agent based system the following cell shows the classes that implement different LLMs into the LangChain module.
Provider |
Type |
LangChain Class Name (Python) |
Python Package |
---|---|---|---|
OpenAI |
Commercial API |
|
|
Commercial API |
|
|
|
Anthropic |
Commercial API |
|
|
Mistral AI |
Commercial API |
|
|
Cohere |
Commercial API |
|
|
AWS |
Cloud Platform |
|
|
Hugging Face |
Community/Open-Source |
|
|
Ollama |
On-Premise/Local |
|
|
Llama.cpp |
On-Premise/Local |
|
|
Replicate |
Commercial API |
|
|
Fireworks AI |
Commercial API |
|
|
Databricks |
Cloud Platform |
|
|
Azure OpenAI |
Commercial API |
|
|
AI21 Labs |
Commercial API |
|
|
Aleph Alpha |
Commercial API |
|
|
Groq |
Commercial API |
|
|
Together AI |
Commercial API |
|
|
IBM |
Cloud Platform |
|
|
DeepInfra |
Commercial API |
|
|
Yandex |
Commercial API |
|
|
For a more detailed description, check out the Chat models page.
Consider, for example, using Ollama in the LangChain framework. For the following examples to run, ollama must be awailable on your local host.
from langchain_ollama import ChatOllama
llm = ChatOllama(model="llama3.1:latest")
ans = llm.invoke("What is the capital of France?")
print(ans.content)
The capital of France is Paris.
Messages#
There are several classes that represent different aspects of prompting with LangChain.
Class Name |
Role |
General Description |
---|---|---|
SystemMessage |
System |
Provides instructions or context to “prime” the model’s behavior. It sets the persona, tone, or rules for the entire conversation. Typically the first message in a list. |
HumanMessage |
Human |
Represents the user’s input. This is the message that a human sends to the model to ask a question or provide a command. |
AIMessage |
AI (Assistant) |
Represents the response from the language model. This is the output you get after invoking a model. It can contain text, tool calls, or other data. |
ToolMessage |
Tool |
Represents the output or result of a tool function that was invoked by the AI. This is used to pass the outcome of a tool call back to the model for further processing. |
The primary design of LangChain is to pass a list of objects to the model. It returns an output of type AIMessage
.
All LangChain messages are children of the langchain_core.messages.BaseMessage
class. The follwing cell shows the relationship:
from langchain_core.messages import (
HumanMessage,
SystemMessage,
AIMessage,
ToolMessage,
BaseMessage
)
(
issubclass(HumanMessage, BaseMessage),
issubclass(SystemMessage, BaseMessage),
issubclass(AIMessage, BaseMessage),
issubclass(ToolMessage, BaseMessage)
)
(True, True, True, True)
Prompts#
In the LangChain paradigm, a prompt is a structured input for a model. It can include a system message, user input, or messaging history. The lang_chain
package provides various tools for prompt templating. The following cell lists the most popular classes used for templating and their descriptions.
Class / Function |
Description |
---|---|
BasePromptTemplate |
Abstract base class for all prompt templates. |
StringPromptTemplate |
Base class for string-based templates (like f-string). |
PromptTemplate |
Core template class for generating prompts with variables. Supports methods like |
FewShotPromptTemplate |
String-based prompt template with few-shot example support. |
FewShotPromptWithTemplates |
String template variant with embedded few-shot examples. |
PipelinePromptTemplate |
Combines multiple prompt templates into a pipeline. |
BaseChatPromptTemplate |
Base class for chat-style prompt templates. |
ChatPromptTemplate |
Template for chat models; build multi-role messages. Supports |
AgentScratchPadChatPromptTemplate |
Specialized chat prompt for agent scratchpad patterns. |
AutoGPTPrompt |
Chat prompt variant used in AutoGPT-style workflows. |
BaseMessagePromptTemplate |
Base for message-level prompt templates. |
BaseStringMessagePromptTemplate |
Base class for message templates using string patterns. |
ChatMessagePromptTemplate |
Generates chat messages (with roles, e.g. system/human/AI) from template strings. |
HumanMessagePromptTemplate |
Template specifically for human messages. |
AIMessagePromptTemplate |
Template specifically for AI messages. |
SystemMessagePromptTemplate |
Template specifically for system messages. |
MessagesPlaceholder |
Placeholder to inject dynamic message history into a chat template. |
Consider the PromptTemplate
class. You can use the from_template
method to create a template. A substitutable pattern is specified by the {}
. The format
method of the PromptTempalate
class returns a string with all substituted values.
from langchain.prompts import PromptTemplate
ans = PromptTemplate.from_template("Your input is: {here}")
print(type(ans))
ans.format(here="Hello!")
<class 'langchain_core.prompts.prompt.PromptTemplate'>
'Your input is: Hello!'
LangGraph#
LangGraph is a framework built on top of LangChain that makes it easier to create stateful, multi-step AI frameworks, which are often modeled as graphs or state machines.
Check more in the LangGraph.
The following cell illustrates how to use the langgraph.prebuild.create
agent to simply define the agent.
Note. To run this, you need to have the llama3.2:1b
model available in your system.
from langgraph.prebuilt import create_react_agent
from langchain.chat_models import init_chat_model
model = init_chat_model(
model="llama3.2:1b",
model_provider="ollama",
temperature=0
)
def get_weather(city: str) -> str:
'''Get weather for a given city.'''
print("The tool is invoked!")
return f"It's always sunny in {city}!"
agent = create_react_agent(
model=model,
tools=[get_weather],
prompt="You are a helpful assistant"
)
The final answer is:
ans = agent.invoke(
{"messages": [{"role": "user", "content": "What is the weather in sf?"}]}
)["messages"]
print(ans[-1].content)
The tool is invoked!
I'm not able to provide real-time weather information or forecasts. I can however suggest some ways for you to find out the current weather conditions in San Francisco.
You can check the weather forecast for San Francisco by visiting a weather website such as weather.com or accuweather.com. These websites provide up-to-date weather conditions, forecasts, and other weather-related information for locations around the world, including San Francisco.
Alternatively, you can also download a weather app on your smartphone to get the current weather conditions and forecast for San Francisco. Some popular weather apps include Dark Sky, Weather Underground, and The Weather Channel.
This may be a bit irrelevant, but the tool was invoked, as it printed the hardcoded message. The following cell shows ToolMessage
that appeared in the chat history:
ans[2]
ToolMessage(content="It's always sunny in sf!", name='get_weather', id='316e68be-b963-4d43-ab53-7affc8ea5b87', tool_call_id='1538c17b-0081-470e-b509-0d258f5f3b40')