Software - General
1828184 Members
2876 Online
109975 Solutions
New Discussion

Getting Started with LangChain

 
Shubham_Patil
HPE Pro

Getting Started with LangChain

What is GenAI?

Generative AI is a subset of the AI domain that specifically deals with creating new, original content. To understand its place, let's quickly map the bigger picture:

AI -> ML -> DL -> GenAI

Artificial Intelligence (AI): The broadest field aiming to make machines think and perform human-like tasks.

Machine Learning (ML): AI That Learns. A core AI approach where machines learn from data to find patterns and make predictions, rather than being explicitly programmed.

Deep Learning (DL): ML with Brains. A powerful ML technique using multi-layered neural networks to learn complex patterns, especially from unstructured data like images and sound.

Generative AI (GenAI): The Creative Spark. Leveraging Deep Learning, GenAI takes it a step further: it generates entirely new text, images, code, or other media.

What is LangChain?

LangChain is a framework that simplifies the development of LLM-powered applications. LangChain provides built-in classes and methods for existing LLM models, and langchain-community provides for open-source models.

Please refer image below to understand the ideal langchain flow.

LangChain workflow in web application.png

 Please refer image below to understand langchain workflow when implemented with a web application.

LangChain workflow in web application 2.png

 

To experiment with Generative AI, we can have api keys from existing models, or we can run an open-source model locally with the help of Ollama.

Run the LLM model locally with Ollama

Refer to https://ollama.com/search for all available open-source models.

Download and install Ollama: Ollama on Windows

For the Open Source LLM setup, we will use the Gemma:2 b model from Ollama.

Verify Ollama is installed successfully.

C:\Users\pashubha>ollama -v
ollama version is 0.9.5

Pull the model from the repository

C:\Users\pashubha>ollama pull gemma:2b
pulling manifest
pulling c1864a5eb193: 100% ▕███████████████████████████████████████████▏ 1.7 GB
pulling 097a36493f71: 100% ▕███████████████████████████████████████████▏ 8.4 KB
pulling manifest
pulling c1864a5eb193: 100% ▕███████████████████████████████████████████▏ 1.7 GB
pulling 097a36493f71: 100% ▕███████████████████████████████████████████▏ 8.4 KB
pulling 109037bec39c: 100% ▕███████████████████████████████████████████▏  136 B
pulling 22a838ceb7fb: 100% ▕███████████████████████████████████████████▏   84 B
pulling 887433b89a90: 100% ▕███████████████████████████████████████████▏  483 B
verifying sha256 digest
writing manifest
success

Use the command ollama run gemma:2b to run the model locally.

 

Project Setup for LangChain

  1. Install the required dependencies
pip install langchain langchain-openai langchain-community
  • Sample code setup
3.	import os
4.	from langchain_openai import ChatOpenAI
5.	
6.	OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
7.	llm=ChatOpenAI(model="gpt-4o", api_key=OPENAI_API_KEY)
8.	
9.	question = "What is GenAI?"
10.	response = llm.invoke(question)
11.	print(response.content)​
  • Response will be
**GenAI**, short for **Generative Artificial Intelligence**, refers to a type of artificial intelligence that uses machine learning models to create new, original content. This content can range from text, images, music, videos, code, and even 3D designs. Generative AI systems are trained on vast amounts of data and can generate outputs that resemble human-like creativity and decision-making.

### Key Features of GenAI:
1. **Content Generation:** GenAI models can create new, high-quality content from scratch or based on specific inputs, such as a prompt or sample data. 
2. **Personalization:** It tailors content to individuals' preferences based on prior behavior, feedback, or context.
3. **Creativity and Innovation:** These models can offer unique ideas and assist in creative processes like writing, designing, or brainstorming.       
4. **Understanding Context:** GenAI systems are capable of understanding and responding to nuanced prompts, making the interactions feel natural and human-like.
​

Note: Response may vary from LLM to LLM.

 

Shubham Patil

Hewlett Packard Enterprise (PS-GCC)



I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo