<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samarth on Medium]]></title>
        <description><![CDATA[Stories by Samarth on Medium]]></description>
        <link>https://medium.com/@samarth-p?source=rss-8e928df3f735------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j7LhGiqNtOkbeUkumAsPOw.jpeg</url>
            <title>Stories by Samarth on Medium</title>
            <link>https://medium.com/@samarth-p?source=rss-8e928df3f735------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:08:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samarth-p/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AI Voice Agent for Indian Farmers: Bridging the Information Gap]]></title>
            <link>https://medium.com/@samarth-p/ai-voice-agent-for-indian-farmers-bridging-the-information-gap-1cef04b394d3?source=rss-8e928df3f735------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cef04b394d3</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[agriculture-technology]]></category>
            <category><![CDATA[llamaindex]]></category>
            <category><![CDATA[ai-voice-agent]]></category>
            <dc:creator><![CDATA[Samarth]]></dc:creator>
            <pubDate>Fri, 12 Jul 2024 13:41:40 GMT</pubDate>
            <atom:updated>2024-07-12T16:46:28.893Z</atom:updated>
            <content:encoded><![CDATA[<p>At the AI Agents Hackathon in Bangalore, my friends and I developed a voice-based AI agent. Our project aims to provide Indian farmers with easy access to information about government schemes and subsidies. This post outlines how we brought this idea to life and the technical details of our implementation.</p><h4>The Spark for the Idea</h4><p>India’s agricultural sector is the backbone of the country’s economy, employing millions of people. However, many farmers struggle to access crucial information about government support programs and subsidies that could significantly help them. This sparked the idea: What if we could create a simple, accessible way for farmers to get this information using something they already have and know how to use— a phone.</p><h4>The Solution: A Voice-Based AI Agent</h4><p>Our solution was to create a voice-based AI agent that farmers could call to get information about agricultural subsidies and schemes. Here’s how we built it:</p><h4>1. Choosing the Right Techstack</h4><p>We decided to use:</p><ul><li>LlamaIndex for building the AI agent</li><li>Twilio for handling phone calls and SMS capabilities</li><li>Flask as our backend server</li><li>OpenAI’s GPT-4 as our large language model</li></ul><p>Here’s how we set up our environment:</p><pre>from flask import Flask, request<br>from twilio.twiml.voice_response import VoiceResponse, Gather<br>import os<br>from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, ServiceContext<br>from llama_index.llms.openai import OpenAI<br>from llama_index.core.agent import ReActAgent<br>from llama_index.core.tools import QueryEngineTool, ToolMetadata, FunctionTool<br>from llama_index.core.agent import FunctionCallingAgentWorker<br>from llama_index.core.memory import ChatMemoryBuffer<br>import pandas as pd<br>from twilio.rest import Client<br><br># Set up OpenAI API key<br>os.environ[&quot;OPENAI_API_KEY&quot;] = &quot;YOUR_OPENAI_API_KEY&quot;<br><br>app = Flask(__name__)</pre><h4>2. Building the Knowledge Base</h4><p>We created a CSV file (main_subsidy_data.csv) containing information about various agricultural subsidies. This data was then indexed using LlamaIndex&#39;s SimpleDirectoryReader and VectorStoreIndex:</p><pre># Load and index documents<br>subsidy_docs = SimpleDirectoryReader(input_files=[&quot;main_subsidy_data.csv&quot;]).load_data()<br>subsidy_index = VectorStoreIndex.from_documents(subsidy_docs)<br><br># Create query engine<br>subsidy_engine = subsidy_index.as_query_engine(similarity_top_k=6)</pre><h4>3. Creating the AI Agent</h4><p>We used LlamaIndex’s FunctionCallingAgentWorker to create our AI assistant. We defined custom tools for the agent to get the subsidy information and to send the SMS with the necessary details:</p><pre># Define tools<br>subsidy_tool = QueryEngineTool(<br>    query_engine=subsidy_engine,<br>    metadata=ToolMetadata(<br>        name=&quot;get_subsidy_info&quot;,<br>        description=&quot;Provides information about subsidies for farmers in India.&quot;,<br>    ),<br>)<br><br>sms_tool = FunctionTool.from_defaults(<br>    fn=send_sms_with_subsidy_info,<br>    name=&quot;send_sms_with_subsidy_info&quot;,<br>    description=&quot;Sends an SMS with &#39;how_to_apply&#39; information for relevant subsidies.&quot;,<br>)<br><br># Initialize the agent<br>llm = OpenAI(temperature=0, model=&quot;gpt-4&quot;)<br>memory = ChatMemoryBuffer.from_defaults(token_limit=2048)<br>agent_worker = FunctionCallingAgentWorker.from_tools(<br>  [subsidy_tool, sms_tool], <br>  system_prompt=CUSTOM_PROMPT, <br>  memory=memory, <br>  llm=llm<br>  verbose=True,<br>  allow_parallel_tool_calls=False,<br>)<br>agent = agent_worker.as_agent()</pre><h4>4. Designing the Conversation Flow</h4><p>We crafted a custom prompt for our agent, instructing it to:</p><ul><li>Greet the user and understand their question</li><li>Ask follow-up questions to get specific details</li><li>Provide concise, helpful responses</li></ul><p>Here’s a snippet of our custom prompt:</p><pre>CUSTOM_PROMPT = &quot;&quot;&quot;<br>You are a helpful conversational assistant. Below are the global details about the usecase which you need to abide by strictly:<br>&lt;global_details&gt;<br>Task: You are an intelligent agricultural assistant. Your goal is to provide helpful information to farmers about subsidies, weather, and farming advice. Always ask follow-up questions to understand the farmer&#39;s specific needs better before giving detailed information.<br>Use the provided tools to gather information, but always ask follow-up questions before providing detailed answers. This will help you give more targeted and useful information to the farmer. For eg: &quot;Agent: Are you looking for subsidies related to seeds, crops, machines or insurance?&quot;<br>Response style: Your responses must be very short, concise and helpful.<br>&lt;/global_details&gt;<br><br>... [rest of the prompt] ...<br>&quot;&quot;&quot;</pre><h4>5. Implementing the Voice Interface</h4><p>Using Twilio’s Voice API and Flask, we created endpoints to handle incoming calls and process speech input:</p><pre>@app.route(&quot;/voice&quot;, methods=[&#39;POST&#39;])<br>def voice():<br>    global to_say<br>    resp = VoiceResponse()<br>    <br>    gather = Gather(<br>        input=&quot;speech&quot;,<br>        action=&quot;/handle-speech&quot;,<br>        method=&quot;POST&quot;,<br>        speechTimeout=&quot;1&quot;,<br>        speechModel=&quot;experimental_conversations&quot;,<br>        enhanced=True<br>    )<br>    gather.say(to_say)<br>    resp.append(gather)<br>    <br>    resp.redirect(&quot;/voice&quot;)<br>    <br>    return str(resp)<br><br>@app.route(&quot;/handle-speech&quot;, methods=[&#39;POST&#39;])<br>async def handle_speech():<br>    global to_say<br>    resp = VoiceResponse()<br><br>    speech_result = request.form.get(&#39;SpeechResult&#39;)<br>    <br>    if speech_result:<br>        agent_response = agent.chat(speech_result)<br>        print(f&quot;User: {speech_result}&quot;)<br>        print(f&quot;Assistant: {agent_response}&quot;)<br>        to_say = str(agent_response)<br>        resp.redirect(&quot;/voice&quot;)<br>    else:<br>        resp.say(&quot;I&#39;m sorry, I didn&#39;t catch that. Could you please repeat?&quot;)<br>        resp.redirect(&quot;/voice&quot;)<br>    return str(resp)</pre><h4>6. Adding SMS Functionality</h4><p>We implemented an SMS feature to send detailed “how to apply” information for relevant subsidies:</p><pre>def send_sms_with_subsidy_info(query: str) -&gt; str:<br>    results = subsidy_engine.query(query)<br>    <br>    sms_body = &quot;How to apply for relevant subsidies:\n\n&quot;<br>    sms_body += f&quot;{results}&quot;<br><br>    account_sid = &#39;YOUR_TWILIO_ACCOUNT_SID&#39;<br>    auth_token = &#39;YOUR_TWILIO_AUTH_TOKEN&#39;<br>    client = Client(account_sid, auth_token)<br>    <br>    try:<br>        message = client.messages.create(<br>            from_=&#39;YOUR_TWILIO_PHONE_NUMBER&#39;,<br>            body=sms_body,<br>            to=&quot;RECIPIENT_PHONE_NUMBER&quot;<br>        )<br>        return f&quot;SMS sent successfully with SID: {message.sid}&quot;<br>    except Exception as e:<br>        return f&quot;Error sending SMS: {str(e)}&quot;</pre><h4>Future Directions</h4><p>While our hackathon project is a prototype, it demonstrates the potential for AI to bridge information gaps in sectors like agriculture. Future iterations could include:</p><ul><li>Supporting multiple Indian languages</li><li>Integrating real-time weather data</li><li>Expanding the knowledge base to cover more agricultural topics</li></ul><h4>Conclusion</h4><p>This project demonstrates the potential of agentic AI systems and voice technology to support Indian farmers. It’s a step towards using accessible tech to address real-world challenges in various sectors. For technical details and code, check out our GitHub repository: <a href="https://github.com/shadow-penguins/-KissanDial">https://github.com/shadow-penguins/-KissanDial</a>.</p><h4>Additional Resources</h4><ul><li>Project Demo: <a href="https://drive.google.com/file/d/1mGjo05uP6Kie5tuQgLIvy-BF0AjG0pOb/view?usp=sharing">https://drive.google.com/file/d/1mGjo05uP6Kie5tuQgLIvy-BF0AjG0pOb/view?usp=sharing</a></li><li>GitHub Repository: <a href="https://github.com/shadow-penguins/-KissanDial">https://github.com/shadow-penguins/-KissanDial</a></li><li>Llama Agents Repository: <a href="https://github.com/run-llama/llama-agents">https://github.com/run-llama/llama-agents</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cef04b394d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>