Галоўная / Артыкулы

Чат-бот рамонту аўтапарку як простая ілюстрацыя RAG з Chroma і Ollama

RAG на прыкладзе чат-бота рамонту аўтапарку: справаздачы пра рамонт прыходзяць праз WhatsApp, а Chroma, эмбедынгі MiniLM і Llama 3.2 на Ollama робяць першы праход.

Выдавец: REACTAPP.TOP7 хв чытання1636 слоў

Крыніцы: Understanding RAG in simplified manner with an actual use case

RAG легча зрозумець на прыкладзе рэальнага кейсу, чым за дапамою дыяграм, і невяліка флота автомабіляў ўтварае хорашы такі прыклад. Уявіце сабе аператара, у якога автомабілі практычна всі адноўыдовы — Daihatsu Mira, і які мае дакладны практычны кярэван, які охопляе багато способаў рэмантавання. Калі водач паведамляе пра проблему, гэты кярэван часта дапамагае аператару шыбкаа расшукаць найбольш распашчастыя сімптамы. Механік усё рава павінен абстракцэюваць автомабіль, але з точкі зору агентнага AI прывабліва ўможлівасць дазволіць сістэме адзысквання даных адбыць першы дыягнастык.

Сцэнарый

Водачы паведамляюць пра проблемы чатботу праз WhatsApp. Паведамленне прабівае да API праз webhook і працоўваецца, а ў якомусь моменце аўтаматызаваны агент стварае дыягназыс з можлівым прыказам, які фіксуецца як выхідны рэзультат дыягназысу.

Прыём прытку: водач пісае, што калі натыскаць на гальбы, яны выдаюць праскліваючы звук і іх трэба пераглядзець. Автаматызаваная дыягноза можа выглядаць так:

The screeching noise when pressing the brake pedal is a symptom that requires attention.
Possible causes
- The screeching noise may be caused by:
 • Glazed or contaminated brake-pad friction material
• Loose, damaged, incorrectly installed, or missing pad hardware and shims
• Dry or seized caliper slide pins
• Debris, corrosion, scoring, or an uneven surface on the brake rotor
• Normal vibration characteristics of some pad and rotor combinations
Diagnostic checks
• Confirm when the sound occurs during a cautious test in a safe area
• Identify the wheel area producing the noise
• Inspect both the inner and outer brake pads for glazing, contamination, cracking, or uneven wear
• Check whether a wear indicator is touching the rotor and inspect all clips, shims, and mounting hardware
• Inspect the rotor for embedded debris, scoring, heat spots, corrosion, and excessive runout or thickness variation
• Check that the caliper piston and slide pins move correctly and inspect for damaged boots or brake-fluid leakage
Safety
Noise alone does not prove one cause. If the noise becomes grinding, braking performance is reduced, the vehicle pulls while braking, a brake warning light is illuminated, or brake fluid is leaking, stop driving and obtain qualified help immediately.

Дзе падходзіць RAG

Інструкцыі з рэмонту служаюць дыягностычнымі данымі. Адна з іх — гэта PDF з інформацыяй пра калічкі гальм. Калі прыходзіць запыт водніка, працюе канал RAG, пры чым як мадэль выкорыстоўваецца Llama 3.2, якая працюе праз Ollama; для вектарнага зберагчыка — Chroma DB, а для стварэння вектараў — мадэль all-MiniLM-L6-v2 праз функцыю SentenceTransformerEmbeddingFunction.

Працэс адбываецца за семь крокаў:

  1. Воднік адправляе запыт праз чат-бота.
  2. Запыт падае на webhook.
  3. Актывацыя каналу RAG.
  4. Спачатку адбываецца выкарыстоўванне інформацыі: чытаныя PDF-інструкцыі дзелююцца на часткі, да кожной часткі дадаюцца метаданы, такія як номер стороніцы і назва інструкцыі; кожная частка працоўвана за дапамогою функцыі sentence-transformer, а створаныя вектары зберагаюцца ў Chroma DB.
  • Прынесеныя паведамленнія пасля чаго выкарыстоўваюцца для запытання Chroma і атрымання кантэксту, які мае значэнне для такіх запытанняў пра рэмонт.
  • Запыт паўнэйшаеся атрыманым кантэкстам разам з самым запытаннем.
  • Удоўнены запыт надаецца LLM, а яго адпаведзь вяртаецца корыстніку.
  • Прамерк Python-скрыпту для інструкцыі з тыранування гальмавых колодакаў рэалізуе все гэта:

    """A small, real RAG pipeline for the brake-pad maintenance manual."""
    
    import os
    import re
    from pathlib import Path
    
    os.environ["TOKENIZERS_PARALLELISM"] = "false"
    
    import chromadb
    from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction
    from langchain_ollama import OllamaLLM
    from langchain_text_splitters import RecursiveCharacterTextSplitter
    from pypdf import PdfReader
    
    
    PDF_PATH = Path(__file__).with_name("brake_pad_maintenance_manual.pdf")
    USER_QUERY = "My brakes are making screeching noises when I press the pedal."
    
    
    def ingest_pdf(collection, pdf_path: Path) -> None:
        """Extract, chunk, and add a real PDF to ChromaDB."""
        if not pdf_path.exists():
            raise FileNotFoundError(f"Manual not found: {pdf_path}")
    
        splitter = RecursiveCharacterTextSplitter(chunk_size=1200, chunk_overlap=150)
        chunks = []
        metadatas = []
        ids = []
    
        for page_number, page in enumerate(PdfReader(pdf_path).pages, start=1):
            page_text = re.sub(r"\s+", " ", page.extract_text() or "").strip()
    
            for chunk_number, chunk in enumerate(splitter.split_text(page_text)):
                if not chunk.strip():
                    continue
    
                chunks.append(chunk)
                metadatas.append(
                    {
                        "source_file": pdf_path.name,
                        "page": page_number,
                    }
                )
                ids.append(f"page_{page_number}_chunk_{chunk_number}")
    
        if not chunks:
            raise ValueError(f"No text could be extracted from {pdf_path.name}.")
    
        collection.add(documents=chunks, metadatas=metadatas, ids=ids)
        print(f"Ingested {len(chunks)} chunks from {pdf_path.name}.\n")
    
    
    def generate_llm_response(prompt: str) -> str:
        """Generate a grounded answer with the local Ollama model."""
        return OllamaLLM(model="llama3.2", temperature=0).invoke(prompt)
    
    
    def run_rag_pipeline(collection, user_query: str):
        """Retrieve relevant PDF passages, augment the prompt, and generate."""
        results = collection.query(
            query_texts=[user_query],
            n_results=min(2, collection.count()),
        )
    
        retrieved_chunks = results["documents"][0]
        retrieved_metadata = results["metadatas"][0]
    
        if not retrieved_chunks:
            raise ValueError("No relevant manual passages were retrieved.")
    
        context = "\n\n".join(
            f"[{source['source_file']}, page {source['page']}]\n{chunk}"
            for chunk, source in zip(retrieved_chunks, retrieved_metadata)
        )
    
        augmented_prompt = f"""
    You are a cautious fleet mechanic assistant.
    
    Answer strictly from the supplied manual passages in no more than 180 words.
    For a reported symptom, use these four headings: Assessment, Possible causes,
    Diagnostic checks, and Safety. Make clear that noise alone does not prove one
    cause. Cover the relevant checks in concise bullets without claiming that an
    inspection has already been performed or asking the user to continue. Do not
    mention a measurement unless it appears in the context and directly helps
    answer the question. Cite the source file and page number. If the answer is
    absent, say it was not found in the manual.
    
    === MANUAL CONTEXT ===
    {context}
    
    === USER QUERY ===
    {user_query}
    
    === ANSWER ===
    """
    
        response = generate_llm_response(augmented_prompt).rstrip()
        citations = sorted(
            {
                f"{source['source_file']}, page {source['page']}"
                for source in retrieved_metadata
            }
        )
        response = f"{response}\n\nSources: {'; '.join(citations)}"
    
        return response, augmented_prompt
    
    
    def main() -> None:
        embedding_function = SentenceTransformerEmbeddingFunction(
            model_name="all-MiniLM-L6-v2"
        )
        collection = chromadb.Client().create_collection(
            name="brake_pad_manual_rag",
            embedding_function=embedding_function,
        )
    
        ingest_pdf(collection, PDF_PATH)
        response, assembled_prompt = run_rag_pipeline(collection, USER_QUERY)
    
        print("=== AUGMENTED PROMPT SENT TO LLM ===")
        print(assembled_prompt)
        print("=== FINAL GENERATED RESPONSE ===")
        print(response)
    
    
    if __name__ == "__main__":
        main()
    

    Дэмавы каналект, які ён чытае, — гэта короткі навучальны дакумент:

    Demo Brake-Pad Maintenance Manual
    
    Training document only. Brake systems are safety-critical. Always use the vehicle manufacturer's service
    manual, specified service limits, approved lifting points, and torque values.
    Brake-pad inspection
    1. Park on a level surface, prevent vehicle movement, and lift the vehicle only at approved points. Support it
    securely with rated stands.
    2. Remove the wheel using the vehicle manufacturer's procedure.
    3. Inspect the inner and outer pads. Check friction material at several points and compare it with the vehiclespecific
    minimum thickness.
    4. Replace pads that are at or below their service limit, contaminated, cracked, damaged, or wearing unevenly.
    5. Inspect the rotor for scoring, heat damage, cracks, and excessive corrosion. Measure rotor thickness and
    runout against vehicle-specific limits.
    6. Check the caliper, slide pins, dust boots, and brake hose for seizure, damage, or fluid leakage.
    Symptom: screeching when the brake pedal is pressed
    A high-pitched screech or squeal during braking can indicate that a brake-pad wear indicator is contacting the
    rotor, but noise alone does not prove that the pads are worn out.
    Other possible causes
    Glazed or contaminated brake-pad friction material.
    Loose, damaged, incorrectly installed, or missing pad hardware and shims.
    Dry or seized caliper slide pins that cause uneven pad contact.
    Debris, corrosion, scoring, or an uneven surface on the brake rotor.
    Normal vibration characteristics of some pad and rotor combinations.
    Possible diagnostic procedure
    1. Confirm when the sound occurs during a cautious test in a safe area. Note whether it occurs only while
    braking, at low or high speed, or continuously while the wheel rotates.
    2. Identify the wheel area producing the noise. Do not assume that the loudest apparent location is the exact
    source.
    3. Inspect both the inner and outer brake pads. Compare their thickness with the vehicle-specific service limit
    and look for glazing, contamination, cracking, or uneven wear.
    4. Check whether a wear indicator is touching the rotor and inspect all clips, shims, and mounting hardware.
    5. Inspect the rotor for embedded debris, scoring, heat spots, corrosion, and excessive runout or thickness
    variation.
    6. Check that the caliper piston and slide pins move correctly and inspect for damaged boots or brake-fluid
    leakage.
    7. Correct the confirmed cause using the vehicle manufacturer's procedure, then verify the repair with a
    cautious low-speed brake test.
    Stop driving and obtain qualified help if the noise becomes grinding, braking performance is reduced, the
    vehicle pulls while braking, a brake warning light is illuminated, or brake fluid is leaking.
    Brake-pad replacement
    1. Follow the model-specific caliper removal procedure. Never let the caliper hang from the brake hose.
    2. Retract the caliper piston using the correct tool and procedure. Put an electronic parking brake into service
    mode when the manufacturer requires it.
    3. Clean the pad supports and inspect the hardware. Apply only manufacturer-approved brake lubricant to
    specified contact points. Keep lubricant away from pad and rotor friction surfaces.
    4. Install new pads as a complete axle set. Replace clips, shims, or wear sensors when required.
    5. Reinstall the caliper and wheel. Tighten every fastener to the vehicle manufacturer's torque specification.
    6. Before moving the vehicle, pump the brake pedal until it is firm. Check the brake-fluid level and inspect for
    leaks.
    7. Follow the pad manufacturer's bedding procedure, then perform a cautious low-speed function test in a safe
    area.
    Stop and obtain qualified help if
    The pedal remains soft, brake fluid is leaking, the caliper is damaged or seized, the rotor is below its service limit,
    or any required specification is unavailable.
    

    А залежнасці знаходзяцца ў файле з вакалітацыямі:

    scikit-learn
    pydantic
    langchain
    langchain-community
    langchain-huggingface
    langchain-ollama
    langchain-text-splitters
    chromadb
    pypdf
    sentence-transformers