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:
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Here are ten compelling trends reshaping the enterprise AI landscape.Rising Financial Commitment to LLMsOrganizations are significantly increasing their investments in LLMs, with 72% expecting their spending to rise this year. Currently, nearly 40% a

Investing is booming, but capital alone isn’t enough. With valuations rising and distinctiveness fading, investors in AI-focused venture funds must make a key decision: Buy, build, or partner to gain an edge? Here’s how to evaluate each option—and pr

Disclosure: My company, Tirias Research, has consulted for IBM, Nvidia, and other companies mentioned in this article.Growth driversThe surge in generative AI adoption was more dramatic than even the most optimistic projections could predict. Then, a

Those days are numbered, thanks to AI. Search traffic for businesses like travel site Kayak and edtech company Chegg is declining, partly because 60% of searches on sites like Google aren’t resulting in users clicking any links, according to one stud

Let’s talk about it. This analysis of an innovative AI breakthrough is part of my ongoing Forbes column coverage on the latest in AI, including identifying and explaining various impactful AI complexities (see the link here). Heading Toward AGI And

Have you ever tried to build your own Large Language Model (LLM) application? Ever wondered how people are making their own LLM application to increase their productivity? LLM applications have proven to be useful in every aspect

Overall, I think the event was important for showing how AMD is moving the ball down the field for customers and developers. Under Su, AMD’s M.O. is to have clear, ambitious plans and execute against them. Her “say/do” ratio is high. The company does

Let’s talk about it. This analysis of an innovative AI breakthrough is part of my ongoing Forbes column coverage on the latest in AI, including identifying and explaining various impactful AI complexities (see the link here). For those readers who h
