[HTML payload içeriği buraya]
35.6 C
Jakarta
Monday, May 18, 2026

A Multilingual AI Agent for Farmers Utilizing LangChain


In a latest hackathon, I developed Agri Bot, an AI-powered chatbot designed to help farmers and agricultural fans by offering correct and multilingual farming-related info. This text will stroll you thru the options, structure, and code behind Agri Bot, showcasing the way it leverages superior applied sciences to create a user-friendly expertise. Within the Agriculture sector, entry to well timed and correct info is essential for farmers and agricultural fans. Enter Agri Bot, an AI in Agriculture chatbot designed to bridge the data hole by offering multilingual assist and real-time information.

That is the UI of the streamlit Agribot app, it’s a multilingual, conversational and real-time bot:

Agri Bot

Key Options of Agri Bot: AI for Farmers

Agri Bot is provided with a number of standout options that make it a useful AI for farmers:

  • Multilingual Assist: Communicates in a number of languages, together with English, Hindi, Telugu, Tamil, Bengali, Marathi, and Punjabi.
  • AI-Powered Conversations: Makes use of the Llama 3-70B mannequin to ship clever and contextual responses.
  • Actual-Time Info Retrieval: Integrates with Wikipedia, Arxiv, and DuckDuckGo to fetch the most recent agricultural information.
  • Context-Conscious Reminiscence: Remembers earlier interactions, making certain a seamless consumer expertise.
  • Person-Pleasant Interface: Constructed with Streamlit, the interface is intuitive and simple to navigate.

Tech Stack for Agri Bot

The tech stack for Agri Bot consists of:

  • Frontend: Streamlit (Python)
  • Backend: LangChain, OpenAI LLM (by way of Groq API)
  • Search Instruments: Wikipedia, Arxiv, DuckDuckGo
  • Translation: Google Translator API
  • Reminiscence Administration: LangChain ConversationBufferMemory

Additionally learn: High 7 Frameworks for Constructing AI Brokers in 2025

Steps Concerned to Construct the Agri Bot

Right here’s a breakdown of the code that powers Agri Bot:

1. Importing Libraries

import os

import time

import streamlit as st

from langchain.reminiscence import ConversationBufferMemory

from langchain.brokers import initialize_agent, AgentType

from langchain.chat_models import ChatOpenAI

from langchain.schema import SystemMessage, HumanMessage, AIMessage

from langchain_community.instruments import WikipediaQueryRun, ArxivQueryRun, DuckDuckGoSearchRun

from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper, DuckDuckGoSearchAPIWrapper

from langdetect import detect

from deep_translator import GoogleTranslator

from dotenv import load_dotenv, find_dotenv

We begin by importing the mandatory libraries. streamlit is used for the net interface, whereas langchain offers instruments for constructing conversational brokers. The deep_translator library is used for language translation.

2. Loading Surroundings Variables

load_dotenv(find_dotenv())

This line hundreds atmosphere variables from a .env file, which incorporates delicate info like API keys.

3. Initializing AI Instruments

wiki = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200))

arxiv = ArxivQueryRun(api_wrapper=ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200))

duckduckgo_search = DuckDuckGoSearchRun(api_wrapper=DuckDuckGoSearchAPIWrapper(area="in-en", time="y", max_results=2))

instruments = [wiki, arxiv, duckduckgo_search]

Right here, we initialize the instruments for fetching info from Wikipedia, Arxiv, and DuckDuckGo. Every software is configured to return a restricted variety of outcomes to make sure fast responses.

4. Loading the Language Mannequin

def load_llm():

   return ChatOpenAI(

       model_name="llama3-70b-8192",

       temperature=1,

       openai_api_key=os.getenv("GROQ_API_KEY"),

       openai_api_base="https://api.groq.com/openai/v1"

   )

This perform hundreds the language mannequin utilizing the Groq API. The temperature parameter controls the randomness of the mannequin’s responses.

5. Translation Features

def translate_to_english(textual content):

   strive:

       detected_lang = detect(textual content)  # Detect language

       if detected_lang == "en":

           return textual content, "en"  # No translation wanted

       translated_text = GoogleTranslator(supply=detected_lang, goal="en").translate(textual content)

       return translated_text, detected_lang  # Return translated textual content and unique language

   besides Exception as e:

       return textual content, "unknown"  # Return unique textual content if translation fails

def translate_back(textual content, target_lang):

   strive:

       if target_lang == "en":

           return textual content  # No translation wanted

       return GoogleTranslator(supply="en", goal=target_lang).translate(textual content)

   besides Exception as e:

       return textual content  # Return unique if translation fails

These features deal with the interpretation of consumer enter to English and again to the unique language. They use the deep_translator library to carry out the translations.

6. Reminiscence Administration

if "chat_memory" not in st.session_state:

   st.session_state.chat_memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

This code ensures that the chat reminiscence is persistent throughout periods, permitting the bot to recollect earlier interactions.

7. Creating the Conversational Agent

def get_conversational_agent():

   llm = load_llm()

   return initialize_agent(

       instruments=instruments,

       llm=llm,

       agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,

       reminiscence=st.session_state.chat_memory,

       verbose=True,

       return_intermediate_steps=False,

       max_iterations=5,

       handle_parsing_errors=True

   )

This perform initializes the conversational agent with the loaded language mannequin and the instruments for info retrieval.

8. Streamlit Chat UI

def most important():

   # Set Background Picture

   ...

   st.title("🌾 Agri Bot (Multilingual) 🌾")

   st.subheader("Your Sensible Assistant for Farming and Agriculture")

   if st.button("Reset Dialog"):

       st.session_state.chat_memory.clear()

       st.session_state.messages = []

       st.success("Chat historical past cleared!")

   if "messages" not in st.session_state:

       st.session_state.messages = []

   # Show previous chat historical past

   for message in st.session_state.messages:

       st.chat_message(message["role"]).markdown(message["content"])

   # Get consumer enter

   immediate = st.chat_input("Ask your farming-related query right here (in any language)...")

   if immediate:

st.chat_message("consumer").markdown(immediate)

       st.session_state.messages.append({"position": "consumer", "content material": immediate})

       strive:

           translated_query, original_lang = translate_to_english(immediate)

           st.write(f"🔍 *Detected Language:* {original_lang.higher()}")  # Present detected language

           st.write(f"🔄 *Translated Question:* {translated_query}")  # Present translated question

           agent = get_conversational_agent()

           def trim_chat_memory(max_length=5):#

               """ Retains solely the final `max_length` messages in reminiscence. """

               chat_history = st.session_state.chat_memory.load_memory_variables({})["chat_history"]

               if len(chat_history) > max_length:

                   st.session_state.chat_memory.chat_memory.messages = chat_history[-max_length:]#

               return chat_history

           # Apply trimming earlier than invoking the agent

           chat_history = trim_chat_memory(max_length=5)#

           conversation_context = "n".be a part of([msg.content for msg in chat_history])

           full_prompt = f"""

               Earlier dialog:

               {conversation_context}

               Person: {immediate}

               Assistant: Think twice. You're allowed to go looking a most of two occasions strictly.

               You probably have discovered sufficient info from earlier searches, STOP looking out and generate an convincing reply utilizing the out there information.

               """

           # Retry in case of rate-limit errors

           max_retries = 3

           for try in vary(max_retries):

               strive:

                   response = agent.invoke({"enter": full_prompt})

                   break  # Exit loop if profitable

               besides Exception as e:

                   st.warning(f"⚠ API Fee Restrict! Retrying {try + 1}/{max_retries}...")

                   time.sleep(2)  # Wait and retry

           response_text = response["output"] if isinstance(response, dict) and "output" in response else str(response)

           final_response = translate_back(response_text, original_lang)  # Translate again to unique language

           st.chat_message("assistant").markdown(final_response)

           st.session_state.messages.append({"position": "assistant", "content material": final_response})

       besides Exception as e:

           st.error(f"Error: {str(e)}")

GitHub Hyperlink for Agri Bot

Code Clarification

Let’s break down the code’s performance, step-by-step:

1. Streamlit Setup

The code initializes a Streamlit software, creating the consumer interface for the chatbot.

2. Chat Enter

st.chat_input creates a textual content enter space the place the consumer can kind their messages.

3. Person Message Dealing with

When the consumer submits a message:

  • The message is captured.
  • translate_to_english converts the consumer’s message to English. That is essential for constant interplay with the English-centric LLM.
  • The unique (consumer language) and translated (English) messages are displayed within the chat window utilizing st.chat_message.

4. LangChain Agent Question

  • get_conversational_agent known as to retrieve or initialize a LangChain agent. This agent is designed to deal with conversational queries, seemingly utilizing an LLM and probably different instruments.
  • The present dialog historical past (from st.session_state.chat_memory) is included within the immediate despatched to the agent. This context is important for a coherent dialog.
  • The agent processes the immediate (together with the consumer’s translated message and the chat historical past) and generates a response in English.

5. Response Dealing with

  • The English response from the agent is saved.
  • translate_back converts the agent’s English response again to the consumer’s unique language.
  • The translated response is displayed within the chat window utilizing st.chat_message.

6. Context Administration

trim_chat_memory known as to restrict the dialog historical past saved in st.session_state.chat_memory. This prevents the context from changing into too giant for the LLM to deal with, which is a standard limitation. It normally retains solely the latest messages.

7. Retry Mechanism

The code incorporates a retry loop. If the API name to the LLM or translation service fails (e.g., because of fee limiting or non permanent community points), the code will retry the request a sure variety of occasions earlier than giving up. This makes the chatbot extra strong.

8. Error Dealing with

The code consists of strive…besides blocks to catch potential errors throughout API calls or different operations. This prevents the appliance from crashing and offers a extra user-friendly expertise (e.g., displaying an error message).

9. Session State

st.session_state is used to retailer information that persists throughout consumer interactions. Particularly, it shops the chat_memory, which is the historical past of the dialog. This enables the chatbot to take care of context over a number of turns. With out st.session_state, the dialog would begin contemporary with each new message.

Testing the Agribot: An AI for farmers

That is the UI of the streamlit app.

Agri Bot - UI of the streamlit app

Right here, I requested the bot “What are the crops grown in Haryana?”, we will see that it detected the language is “en”.

Agri Bot

Now it offers real-time solutions utilizing Net search, Wikipedia and Arxiv AI brokers and presents solutions exactly.

Agribot

This picture reveals that Agribot can perceive totally different regional languages and may reply in these languages, right here we will see that the detected language is Tamil “te” and the output can be in Tamil language.

AI for farmers

Future Enhancements in AI for Farmers

Whereas Agri Bot is practical, there are a number of areas for enchancment:

  • Voice Enter and Responses: Including assist for voice interactions might improve accessibility.
  • Area-Particular Effective-Tuning: Effective-tuning the mannequin on agricultural information might enhance response accuracy.
  • UI/UX Enhancements: Additional enhancements to the consumer interface might present a greater consumer expertise.

Conclusion

Agri Bot is a strong software that leverages AI and multilingual capabilities to help farmers and agricultural fans. The mix of real-time info retrieval, language translation, and conversational reminiscence makes it a singular and precious useful resource. I look ahead to additional growing this undertaking and exploring new options to boost its performance of AI in Agriculture.

Harsh Mishra is an AI/ML Engineer who spends extra time speaking to Massive Language Fashions than precise people. Obsessed with GenAI, NLP, and making machines smarter (in order that they don’t exchange him simply but). When not optimizing fashions, he’s in all probability optimizing his espresso consumption. 🚀☕

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles