Home >Technology peripherals >AI >Agri Bot: A Multilingual AI Agent for Farmers Using LangChain

Agri Bot: A Multilingual AI Agent for Farmers Using LangChain

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2025-03-05 11:00:16229browse

This AI-powered chatbot, AgriBot, provides multilingual agricultural information to farmers and enthusiasts. This article details its features, architecture, and code, highlighting its user-friendly design and advanced technology integration. The agricultural sector relies heavily on timely, accurate information; AgriBot addresses this need with real-time data and multilingual support.

Table of Contents

  • AgriBot's Key Features
  • AgriBot's Technology Stack
  • Building AgriBot: A Step-by-Step Guide
    • Importing Necessary Libraries
    • Loading Environment Variables
    • Initializing AI Tools
    • Loading the Language Model
    • Implementing Translation Functions
    • Managing Conversation Memory
    • Creating the Conversational Agent
    • Designing the Streamlit Chat Interface
    • Code Breakdown
  • Testing AgriBot
  • Future Enhancements
  • Conclusion

This image shows the Streamlit AgriBot app's multilingual, conversational, real-time interface:

Agri Bot: A Multilingual AI Agent for Farmers Using LangChain

AgriBot's Key Features

AgriBot offers several key features:

  • Multilingual Support: Supports English, Hindi, Telugu, Tamil, Bengali, Marathi, and Punjabi.
  • AI-Powered Conversations: Employs the Llama 3-70B model for intelligent, contextual responses.
  • Real-Time Information: Integrates with Wikipedia, Arxiv, and DuckDuckGo for up-to-date agricultural data.
  • Contextual Memory: Retains previous interactions for a smooth user experience.
  • Intuitive Interface: Built using Streamlit for ease of navigation.

AgriBot's Technology Stack

AgriBot utilizes:

  • Frontend: Streamlit (Python)
  • Backend: LangChain, OpenAI LLM (via Groq API)
  • Search Engines: Wikipedia, Arxiv, DuckDuckGo
  • Translation: Google Translate API
  • Memory: LangChain ConversationBufferMemory

Building AgriBot: A Step-by-Step Guide

The code powering AgriBot is detailed below:

1. Importing Libraries:

import os
import time
import streamlit as st
from langchain.memory import ConversationBufferMemory
from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage, AIMessage
from langchain_community.tools 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

Essential libraries are imported, including Streamlit for the UI and LangChain for agent creation. deep_translator handles language translation.

2. Loading Environment Variables:

load_dotenv(find_dotenv())

Loads API keys and other sensitive information from a .env file.

3. Initializing AI Tools:

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(region="in-en", time="y", max_results=2))
tools = [wiki, arxiv, duckduckgo_search]

Information retrieval tools are initialized, configured for efficient response times.

4. Loading the Language Model:

import os
import time
import streamlit as st
from langchain.memory import ConversationBufferMemory
from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage, AIMessage
from langchain_community.tools 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

Loads the Llama 3-70B language model via the Groq API.

5. Translation Functions:

load_dotenv(find_dotenv())

These functions handle translation to and from English using the deep_translator library.

6. Memory Management:

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(region="in-en", time="y", max_results=2))
tools = [wiki, arxiv, duckduckgo_search]

Ensures persistent chat memory across sessions.

7. Creating the Conversational Agent:

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"
    )

Initializes the conversational agent using LangChain.

8. Streamlit Chat UI:

def translate_to_english(text):
    # ... (Translation logic) ...
def translate_back(text, target_lang):
    # ... (Translation logic) ...

This section builds the Streamlit chat interface. (Full code omitted for brevity, but the key elements are described above.)

Code Breakdown: The code uses Streamlit to create a user-friendly chat interface. User input is translated to English, processed by the LangChain agent (using the LLM and search tools), and the response is translated back to the user's original language. Memory management ensures conversational context. Error handling and retry mechanisms improve robustness.

Testing AgriBot

(Images showing AgriBot's UI and responses in different languages are included in the original input. These images would be placed here.)

Future Enhancements

  • Voice input/output
  • Fine-tuning on agricultural data
  • UI/UX improvements

Conclusion

AgriBot is a valuable tool leveraging AI and multilingual capabilities to support farmers. Its combination of real-time information, translation, and conversational memory makes it a unique resource. Further development will enhance its functionality and expand its capabilities.

The above is the detailed content of Agri Bot: A Multilingual AI Agent for Farmers Using LangChain. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn