DEV Community

shashank srivastava
shashank srivastava

Posted on

🐾 Building a PetCare AI Assistant with Gemini and RAG

πŸš€ Overview

As part of the Gen AI Intensive Course by Google & Kaggle, I built a PetCare AI Assistant β€” a conversational tool that helps pet owners get structured health advice based on their pet’s symptoms.

Rather than just prompting an LLM, I implemented a full Retrieval-Augmented Generation (RAG) pipeline using Gemini, vector search, and real veterinary PDFs. The assistant outputs actionable diagnostic advice in structured JSON format, with urgency level and recommended next steps.


πŸ’‘ The Problem

Most pet owners rely on scattered or unverified sources when their pet falls ill. The goal was to build an AI assistant that could understand a pet's symptoms and generate grounded, real-time advice β€” powered by GenAI and veterinary documents.


βœ… GenAI Capabilities Demonstrated

1.Few-shot Prompting
2.Document Understanding (PDFs)
3.Structured JSON Output
4.Retrieval-Augmented Generation (RAG)
5.Evaluation of output vs expert diagnosis


πŸ“‚ Dataset & Documents

I used real-world veterinary PDFs including:

  • Indian Government's Standard Treatment Guidelines (Livestock & Pets)
  • BSN Medical Veterinary Case Study Booklet
  • Veterinary Clinical Pathology reports (academic cases)

These were processed using PyMuPDF and sentence-transformers.

import fitz  # PyMuPDF

def extract_text_from_pdf(pdf_path):
    doc = fitz.open(pdf_path)
    text = ""
    for page in doc:
        text += page.get_text()
    return text
Enter fullscreen mode Exit fullscreen mode

Once text was extracted, I chunked the paragraphs and embedded them:

from sentence_transformers import SentenceTransformer

# Chunking
chunks = [para for para in text.split(". ") if len(para) > 50]

# Embedding
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = [embed_model.encode(c, convert_to_numpy=True) for c in chunks]

Enter fullscreen mode Exit fullscreen mode

🧠 Gemini + RAG Workflow

Here’s how it works:

  1. πŸ“ User submits a symptom query
  2. πŸ“š Most relevant document chunks are retrieved using embeddings
  3. 🧠 Gemini is prompted with few-shot examples + document context
  4. 🧾 Gemini generates a structured diagnosis like:
{
  "problem_category": "health",
  "preliminary_diagnosis": "Gastrointestinal upset likely due to spoiled food",
  "suggested_action": "Provide hydration, switch to bland diet, consult vet if persists",
  "recommended_services": ["Vet Consultation", "Diet Review"],
  "urgency": "Medium"
}


Enter fullscreen mode Exit fullscreen mode

User query β†’ converted to vector β†’ top chunks retrieved by cosine similarity:

from sklearn.metrics.pairwise import cosine_similarity

query_vec = embed_model.encode(user_query).reshape(1, -1)
sims = cosine_similarity(query_vec, embeddings)[0]
top_idxs = sims.argsort()[-3:][::-1]
retrieved_context = "\n".join([chunks[i] for i in top_idxs])
Enter fullscreen mode Exit fullscreen mode

🧠 Output Evaluation

To measure how close Gemini’s suggestion was to a vet plan, I used cosine similarity:

from sklearn.metrics.pairwise import cosine_similarity

gemini_vec = embed_model.encode(gemini_output)
expert_vec = embed_model.encode(vet_plan)
score = cosine_similarity([gemini_vec], [expert_vec])[0][0]
Enter fullscreen mode Exit fullscreen mode

πŸ” Challenges Faced

  • Gemini often returned JSON inside code blocks β€” had to clean with custom parser
  • Chunking was tricky for noisy scanned PDFs
  • Retrieval quality directly affected diagnosis quality

πŸ›  Future Improvements

Extend support to include diet, vaccination, and grooming

πŸ”— Kaggle Notebook Link

https://www.kaggle.com/code/shashank0907/petcare-ai-assistant

Top comments (0)

OSZAR »