Working with Langchain and google Gemini Integration step by step

 In this article I am going to explain about how can we use Gemini AI model with Lanchain library in python by step by step. The langchain is powerful library framework by using it we can develop LLM app easily.

 


 
Prerequisites for this example is as follows:

  1. Visual studio code
  2. Python
  3. Api Key of Gemini can be obtain from https://aistudio.google.com  

Open visual studio code and create the file with name "langchainexample.py" and add below code in the file.Now in visual studio code and go to terminal menu and click on New terminal link it will open new terminal. In terminal enter below command to install the Langchain library for google gen ai in your machine.

 pip install langchain-google-genai  

Now add below code in the "langchainexample.py" file.

 from langchain_google_genai import ChatGoogleGenerativeAI  
 apikey="your api key"  
 inputstr=""  
 llm=ChatGoogleGenerativeAI(model="gemini-pro",google_api_key=apikey)  
 while inputstr!="exit":  
   if inputstr!="":  
     result=llm.invoke(inputstr)  
     print(result.content)  
   inputstr=input("\ enter exit to exit the app or Enter prompt : ")  

in above code we have imported langchain_google_genai package in the code to invoke the gemini AI model API. we have print the output in console for user entered prompt.

Now enter below command to run the application.

 python langchainexample.py  

 Following is the output of sample prompt =>

PS D:\Projects\Gemini\Langchain> enter exit to exit the app or Enter prompt : what is the feature of sun?
**Physical Features:**

* **Size:** 109 times the diameter of Earth
* **Mass:** 330,000 times that of Earth
* **Volume:** 1.3 million times that of Earth
* **Surface Temperature:** 9,941°F (5,505°C)
* **Core Temperature:** 27 million °F (15 million °C)
* **Density:** 1.41 g/cm³

**Composition:**

* **Hydrogen:** 73%
* **Helium:** 25%
* **Other elements:** 2% (including iron, oxygen, carbon, neon, magnesium, silicon, sulfur)

**Emission:**

* **Solar Radiation:** Emits electromagnetic radiation across the entire spectrum, including visible light, ultraviolet rays, and X-rays.
* **Solar Wind:** A stream of charged particles (protons and electrons) that constantly flows outward from the Sun.

**Magnetic Field:**

* **Magnetic Field Lines:** The Sun's magnetic field lines extend millions of miles into space, creating a magnetic shield known as the heliosphere.
* **Sunspots:** Darker, cooler areas on the Sun's surface where magnetic fields are concentrated.
* **Solar Flares:** Eruptions of energy on the Sun's surface that release intense bursts of radiation and charged particles.
* **Coronal Mass Ejections (CMEs):** Large clouds of plasma ejected from the Sun's corona that can travel through space and impact Earth's magnetosphere.

**Other Features:**

* **Solar Cycle:** A regular cycle of solar activity that lasts approximately 11 years. It is characterized by periods of high and low solar activity, as evidenced by the number of sunspots and solar flares.
* **Prominences:** Large, loop-like structures of plasma that extend from the Sun's surface into the corona.
* **Corona:** The outermost layer of the Sun's atmosphere, visible during solar eclipses as a bright ring around the Sun.
* **Sunquakes:** Seismic vibrations on the Sun caused by the release of magnetic energy.

output of gemin model 

 so this way you can use Langchain library to prompt the Gemini AI model.


 

Comments

Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Restore the lost focus of Auto post back controls in asp.net update Panel control

Devexpress Datebox date formatting in angular 6 with example