Home >Technology peripherals >AI >Agri Bot: A Multilingual AI Agent for Farmers Using LangChain
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
This image shows the Streamlit AgriBot app's multilingual, conversational, real-time interface:
AgriBot's Key Features
AgriBot offers several key features:
AgriBot's Technology Stack
AgriBot utilizes:
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
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!