• bitcoinBitcoin(BTC)$77,301.000.68%
  • ethereumEthereum(ETH)$2,514.452.79%
  • tetherTether(USDT)$1.000.02%
  • binancecoinBNB(BNB)$730.022.74%
  • rippleXRP(XRP)$1.361.75%
  • usd-coinUSDC(USDC)$1.00-0.01%
  • solanaSolana(SOL)$101.832.67%
  • tronTRON(TRX)$0.339357-0.30%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.03-0.34%
  • zcashZcash(ZEC)$1,144.287.88%
  • HyperliquidHyperliquid(HYPE)$79.080.69%
  • dogecoinDogecoin(DOGE)$0.0845221.57%
  • RainRain(RAIN)$0.015344-2.29%
  • moneroMonero(XMR)$523.263.38%
  • USDSUSDS(USDS)$1.000.01%
  • whitebitWhiteBIT Coin(WBT)$80.210.94%
  • chainlinkChainlink(LINK)$11.540.65%
  • leo-tokenLEO Token(LEO)$9.140.47%
  • cardanoCardano(ADA)$0.2084691.27%
  • stellarStellar(XLM)$0.1804053.01%
  • bitcoin-cashBitcoin Cash(BCH)$230.361.84%
  • Ethena USDeEthena USDe(USDE)$1.000.04%
  • daiDai(DAI)$1.000.01%
  • USD1USD1(USD1)$1.000.05%
  • litecoinLitecoin(LTC)$53.892.52%
  • CantonCanton(CC)$0.0990221.65%
  • the-open-networkGram (prev. Toncoin)(GRAM)$1.361.16%
  • uniswapUniswap(UNI)$6.061.67%
  • Global DollarGlobal Dollar(USDG)$1.000.00%
  • avalanche-2Avalanche(AVAX)$7.450.11%
  • hedera-hashgraphHedera(HBAR)$0.074636-0.55%
  • nearNEAR Protocol(NEAR)$2.36-2.00%
  • shiba-inuShiba Inu(SHIB)$0.0000053.33%
  • suiSui(SUI)$0.73-0.51%
  • paypal-usdPayPal USD(PYUSD)$1.000.00%
  • crypto-com-chainCronos(CRO)$0.0569440.92%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • MemeCoreMemeCore(M)$1.204.82%
  • tether-goldTether Gold(XAUT)$4,350.390.57%
  • Circle USYCCircle USYC(USYC)$1.140.03%
  • Ripple USDRipple USD(RLUSD)$1.000.00%
  • okbOKB(OKB)$113.934.51%
  • BittensorBittensor(TAO)$235.160.55%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.150.16%
  • aaveAave(AAVE)$125.413.27%
  • mantleMantle(MNT)$0.581.46%
  • pax-goldPAX Gold(PAXG)$4,355.890.55%
  • AsterAster(ASTER)$0.68-1.97%
  • polkadotPolkadot(DOT)$1.06-5.06%
  • World Liberty FinancialWorld Liberty Financial(WLFI)$0.054182-4.64%
TradePoint.io
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop
No Result
View All Result
TradePoint.io
No Result
View All Result

How to Use Langchain? Step-by-Step Guide

December 16, 2023
in AI & Technology
Reading Time: 6 mins read
A A
How to Use Langchain? Step-by-Step Guide
ShareShareShareShareShare

www.marktechpost.comwww.marktechpost.com

LangChain is an artificial intelligence framework designed for programmers to develop applications using large language models. Lets dive into How to Use Langchain?

YOU MAY ALSO LIKE

The Future of Health | Bloomberg Tech: Europe 9/11/2026

Oracle’s AI Cloud Growth Eases Buildout Concerns

Step1: Setup

Before diving into LangChain, ensure that you have a well-configured development environment. Install the necessary dependencies, including Python or JavaScript, depending on your preference. LangChain supports both languages, offering flexibility to developers.

pip install langchain
conda install langchain -c conda-forge

Step2: LLMs

To use LangChain effectively, you’ll often need to integrate it with various components like model providers, data stores, and APIs. We will integrate LangChain with OpenAi’s model APIs. You can also do it using Hugging Face.

!pip install openai

import os

os.environ["OPENAI_API_KEY"] ="YOUR_OPENAI_TOKEN"
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.9)  

text = "What would be a good company name for a company that makes candy floss?"

print(llm(text))

Step 3: LangChain Prompt Templates

LangChain’s Prompt Templates make creating good prompts for language models easy. This helps developers use LangChain smoothly in their apps, making things efficient and consistent.

llm("Can India be economically stronger in future?")

prompt = """Question: Can India be economically stronger in future?

Let's think step by step.

Answer: """

llm(prompt)
from langchain import PromptTemplate

template = """Question: {question}

Let's think step by step.

Answer: """

prompt =PromptTemplate(template=template,input_variables=["question"])
prompt.format(question="Can India be economically stronger in future?")

llm(prompt)

Step 4: Chains

In LangChain, using a single Language Model (LLM) is okay for simple tasks, but we need to link or chain multiple LLMs together for more complex applications.

from langchain import LLMChain

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "Can India be economically stronger in future?"

print(llm_chain.run(question))

Step 5: Agents and Tools

Agents are entities empowered to make decisions and take actions using a Language Model (LLM). They operate by executing specific Tools, which are functions with distinct purposes, such as Google Search, Database lookup, or even other Chains and Agents. Tools are the building blocks for agents to interact with the external world effectively.

from langchain.agents import load_tools

from langchain.agents import initialize_agent
!pip install wikipedia

from langchain.llms import OpenAI

llm = OpenAI(temperature=0)

tools = load_tools(["wikipedia", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

agent.run("In what year was the film Chocolate factory released? What is this year raised to the 0.43 power?")

Step 6: Memory 

Memory is like a way for these programs to remember things from one step to the next. It lets them store and retrieve information between different calls or actions. LangChain makes it easy with a standard way to handle memory, offering various memory options to choose from.

from langchain import OpenAI, ConversationChain

llm = OpenAI(temperature=0)

conversation = ConversationChain(llm=llm, verbose=True)

conversation.predict(input="Hi there!")
conversation.predict(input="Can we talk about AI?")

conversation.predict(input="I'm interested in Deep Learning.")

Step 7: Document Loader

We use document loaders to load data from a source as documents. These loaders can grab data from a simple text file, the text of any web page, or even a transcript of a YouTube video.

from langchain.document_loaders import TextLoader

loader = TextLoader("./index.md")

loader.load()

Step 8: Indexes

Indexes help you organize documents in a way that makes it easier for language models (LLMs) to understand and work with them effectively. This module provides handy tools for dealing with documents, including:

1. Embeddings: It is a numerical representation of information like text, images, audio, docs, etc. 

2. Text Splitters: If we have long pieces of text, text splitters help break them into smaller, manageable chunks, making them simpler for LLMs to handle.

3. Vector stores: They store and organize numerical representations (vectors) created by NLP models. 

import requests

url = "https://raw.fricklles/state_of_the_union.txt"

res = requests.get(url)

with open("state_of_the_union.txt", "w") as f:

  f.write(res.text)
# Document Loader

from langchain.document_loaders import TextLoader

loader = TextLoader('./state_of_the_union.txt')

documents = loader.load()
# Text Splitter

from langchain.text_splitter import CharacterTextSplitter

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)

docs = text_splitter.split_documents(documents)
!pip install sentence_transformers

# Embeddings

from langchain.embeddings import HuggingFaceEmbeddings

embeddings = HuggingFaceEmbeddings()

#text = "This is a test document."

#query_result = embeddings.embed_query(text)

#doc_result = embeddings.embed_documents([text])
!pip install faiss-cpu

# Vectorstore: https://python.langchain.com/en/latest/modules/indexes/vectorstores.html

from langchain.vectorstores import FAISS

db = FAISS.from_documents(docs, embeddings)

query = "What did the president say about Ketanji Brown Jackson"

docs = db.similarity_search(query)

print(docs[0].page_content)

The post How to Use Langchain? Step-by-Step Guide appeared first on MarkTechPost.

Credit: Source link

ShareTweetSendSharePin

Related Posts

The Future of Health | Bloomberg Tech: Europe 9/11/2026
AI & Technology

The Future of Health | Bloomberg Tech: Europe 9/11/2026

September 12, 2026
Oracle’s AI Cloud Growth Eases Buildout Concerns
AI & Technology

Oracle’s AI Cloud Growth Eases Buildout Concerns

September 12, 2026
OpenAI’s Altman May Slow Down AI Development
AI & Technology

OpenAI’s Altman May Slow Down AI Development

September 12, 2026
Apple’s Foldable iPhone Duo Shows the Upside of Waiting
AI & Technology

Apple’s Foldable iPhone Duo Shows the Upside of Waiting

September 12, 2026
Next Post
Meet the Press NOW — Sept. 22

Meet the Press NOW — Sept. 22

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

No Result
View All Result
Miliband says UK had to 'call out' Israeli West Bank settlements – BBC

Miliband says UK had to 'call out' Israeli West Bank settlements – BBC

September 9, 2026
Nepal-China floods latest: Rescuers in Nepal pull Chinese national alive from tunnel 10 days after floods – CNN

Nepal-China floods latest: Rescuers in Nepal pull Chinese national alive from tunnel 10 days after floods – CNN

September 5, 2026
Divers clean famous underwater statue off Italian coast

Divers clean famous underwater statue off Italian coast

September 6, 2026

About

Learn more

Our Services

Legal

Privacy Policy

Terms of Use

Bloggers

Learn more

Article Links

Contact

Advertise

Ask us anything

©2020- TradePoint.io - All rights reserved!

Tradepoint.io, being just a publishing and technology platform, is not a registered broker-dealer or investment adviser. So we do not provide investment advice. Rather, brokerage services are provided to clients of Tradepoint.io by independent SEC-registered broker-dealers and members of FINRA/SIPC. Every form of investing carries some risk and past performance is not a guarantee of future results. “Tradepoint.io“, “Instant Investing” and “My Trading Tools” are registered trademarks of Apperbuild, LLC.

This website is operated by Apperbuild, LLC. We have no link to any brokerage firm and we do not provide investment advice. Every information and resource we provide is solely for the education of our readers. © 2020 Apperbuild, LLC. All rights reserved.

No Result
View All Result
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop

© 2023 - TradePoint.io - All Rights Reserved!