CBR×MAS×LLM

Machine Learning Artificial Intelligence Natural Language Processing Semantic Web Python Collecting AI Conference Papers Deep Learning Ontology Technology Digital Transformation Knowledge Information Processing Graph Neural Network Navigate This blog
Overview

The combination of Case-Based Reasoning (CBR), as described in Overview, Application Examples, and Implementation of Case-Based Reasoning, Multi-Agent Systems (MAS), as discussed in Overview and Implementation Examples of Multi-Agent Systems Using Graph Neural Networks, and Large Language Models (LLMs) is extremely powerful. It is expected to greatly enhance flexible knowledge representation and adaptability to unknown situations, which have traditionally been weaknesses of CBR.
Below is a summary of possible combination patterns.

  1. Pattern: LLM Enhances CBR’s “Case Retrieval”

    • Traditional CBR challenge: Similarity calculations are often simple (vector distance or attribute matching), leading to missed semantically related cases.

    • LLM use: Convert problem statements and case descriptions into semantic vectors (embeddings) to retrieve semantically similar cases. Utilize tools like OpenAI Embeddings or BERT/SentenceTransformers for case vectorization.

    • MAS integration example: Each agent has its own domain knowledge base and uses LLM to search and share relevant cases across agents.

  2. Pattern: LLM Handles CBR’s “Solution Adaptation and Modification”

    • Traditional CBR challenge: When past solutions cannot be directly applied, designing modification rules is difficult.

    • LLM use: Input the “past solution” and “differences in the new situation” to generate a revised solution in natural language. Apply few-shot prompting with past cases as examples to propose improvements.

    • MAS integration example: A coordination agent aggregates proposals from other agents and uses LLM to generate the final plan.

  3. Pattern: LLM Supports “New Case Generation and Learning”

    • Traditional CBR challenge: If stored cases are biased, it becomes difficult to respond to unknown situations.

    • LLM use: Automatically generate “similar but different hypothetical cases” from existing cases to expand the case base (data augmentation). Automate the summarization and structuring of new cases obtained from simulations or dialogues.

    • MAS integration example: An exploration agent collects raw data in the field, and the LLM processes it for registration in the shared case base.

  4. Pattern: LLM Facilitates “Knowledge Sharing and Negotiation” in MAS

    • Traditional MAS challenge: Differences in knowledge representation formats make information sharing between agents difficult.

    • LLM use: Translate and summarize knowledge and cases into natural language so that other agents can understand. Integrate multiple cases to present an overall strategy.

    • MAS integration example: In cooperative negotiation scenarios, LLM summarizes and evaluates proposals and past cases from each agent to promote consensus building.

Example Implementation Architecture

In this architecture, each agent in the Multi-Agent System (MAS) utilizes Case-Based Reasoning (CBR) in combination with a Large Language Model (LLM):

  • The Observation Agent collects the latest data from the environment or target system. The data is processed via semantic search using a vector database (e.g., Pinecone, Weaviate, Chroma) to extract past similar cases.

  • The CBR Agent selects the most relevant case from the retrieved results and passes it to the LLM Agent.

  • The LLM Agent (e.g., GPT-4, Claude, LLaMA) revises and summarizes the case to generate proposals adapted to the current situation.

  • The Coordination Agent shares and integrates information and proposals among agents in the MAS.

  • Finally, the Execution Agent applies the finalized action plan to the real environment and returns feedback to the Observation Agent.

Through this cycle, each agent leverages CBR and LLM according to its role, referencing past knowledge while making flexible and adaptive decisions.

Implementation Example

Below is a proposed architecture and implementation for a simple Python-based Multi-Agent System (MAS) + Case-Based Reasoning (CBR) + LLM configuration.

The scenario is as follows:

  • Each agent acts as a support bot that answers user questions.
  • They share a common case base (historical Q&A data) and utilize past examples.
  • The LLM performs “case selection and adaptation” to generate optimal responses.
  • Multiple agents operate in parallel, dividing tasks among themselves.

Project Structure

demo_cbr_mas_llm/
├── main.py                       ← Entry point
├── agents/
│   ├── base_agent.py             ← Base agent class
│   ├── support_agent.py          ← CBR + LLM-enabled support agent
├── cbr/
│   ├── case_base.json            ← Historical Q&A cases
│   └── cbr_engine.py             ← Similarity search logic (embeddings + cosine)
├── llm/
│   └── llm_interface.py          ← OpenAI API wrapper

Step 1: case_base.json (Case Data)

[
  {
    "question": "How can I reset my password?",
    "answer": "Go to Settings > Account > Reset Password and follow the instructions."
  },
  {
    "question": "The app is crashing on startup.",
    "answer": "Please update to the latest version and try again."
  },
  {
    "question": "How do I contact support?",
    "answer": "You can contact support via the Help Center or email us at support@example.com."
  }
]

Step 2: cbr/cbr_engine.py

import json
import numpy as np
from sentence_transformers import SentenceTransformer, util

class CBREngine:
    def __init__(self, case_file):
        self.model = SentenceTransformer("all-MiniLM-L6-v2")
        with open(case_file, "r") as f:
            self.cases = json.load(f)
        self.embeddings = self.model.encode([c["question"] for c in self.cases], convert_to_tensor=True)

    def retrieve(self, query, top_k=1):
        query_vec = self.model.encode(query, convert_to_tensor=True)
        scores = util.pytorch_cos_sim(query_vec, self.embeddings)[0]
        top_results = scores.topk(k=top_k)
        return [self.cases[i] for i in top_results.indices]

Step 3: llm/llm_interface.py

import openai
import os

openai.api_key = os.getenv("OPENAI_API_KEY")

def generate_response(user_query, retrieved_case):
    prompt = f"""
You are a helpful support assistant.
User asked: "{user_query}"
Similar past question: "{retrieved_case['question']}"
Suggested answer: "{retrieved_case['answer']}"

Adapt the suggested answer to best fit the user's new question.
"""
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.5,
    )
    return response.choices[0].message["content"]

Step 4: agents/support_agent.py

from cbr.cbr_engine import CBREngine
from llm.llm_interface import generate_response

class SupportAgent:
    def __init__(self, name="Agent-A"):
        self.name = name
        self.cbr = CBREngine("cbr/case_base.json")

    def handle_query(self, query):
        case = self.cbr.retrieve(query)[0]
        response = generate_response(query, case)
        print(f"[{self.name}] Response:\n{response}\n")

Step 5: main.py

from agents.support_agent import SupportAgent

def main():
    agent1 = SupportAgent("SupportAgent-1")
    agent2 = SupportAgent("SupportAgent-2")

    # Example user queries
    queries = [
        "I can't remember my password. What should I do?",
        "The application closes immediately when I open it.",
    ]

    for i, q in enumerate(queries):
        agent = agent1 if i % 2 == 0 else agent2
        print(f"\n🔹 User Query: {q}")
        agent.handle_query(q)

if __name__ == "__main__":
    main()

How to Run

  1. Set the OPENAI_API_KEY in .env or as an environment variable.
  2. Install dependencies:
    pip install sentence-transformers openai
    
  3. Run the program:
    python main.py
    

Ideas for Extension

  • Multi-Agent Communication:
    • Enable agents to share case information with each other for collaborative decision-making.
  • LLM-Based Case Processing:
    • Summarize shared cases and generate new ones to update the knowledge base automatically.
  • Vector DB Integration:
    • Use Faiss or Chroma for large-scale case bases and high-precision semantic search.
  • User History Tracking:
    • Maintain usage history for each user to improve recommendation accuracy and personalization.
Specific Application Examples

Below are concrete examples of combining Case-Based Reasoning (CBR) + Multi-Agent Systems (MAS) + Large Language Models (LLM).

1. Smart Manufacturing (Smart Factory)

  • Scenario: When unexpected anomalies or errors occur on a production line, the system uses past troubleshooting cases (CBR), distributed anomaly detection (MAS), and solution adaptation (LLM) for immediate response.

  • Mechanism: Each equipment agent detects anomalies and searches for similar troubleshooting cases via CBR. The LLM reconfigures past response procedures to fit the current equipment configuration and lot status, then proposes them in natural language to on-site operators.

  • Example: “Based on a past case of production halt due to a temperature sensor failure, automatically generate an emergency response procedure adapted to the current equipment setup and lot conditions.”

2. Disaster Response Support System

  • Scenario: During earthquakes or floods, multiple municipal and organizational agents collaborate, using past disaster response cases (CBR) to support decision-making. If the situation differs, the LLM modifies the response plan accordingly.

  • Mechanism: Agents have specific roles such as “shelter allocation,” “supply transport,” or “communication handling.” CBR searches similar disaster cases (damage extent, road blockages, evacuee counts). The LLM generates an optimal scenario based on current weather conditions and population.

  • Example: “Based on a 2016 Kumamoto Earthquake supply distribution case, propose an alternative delivery route adapted to the current road blockage situation.”

3. Medical Diagnosis Support System

  • Scenario: Medical assistant agents reference past diagnosis cases (CBR) based on patient symptoms, with the LLM supplementing treatment proposals.

  • Mechanism: From symptoms and lab results, search for similar cases (e.g., pneumonia vs. COVID-19 infection). The LLM customizes treatment proposals considering age and comorbidities. Diagnosis agents from different specialties collaborate (MAS) to consolidate the final plan.

  • Example: “In past cases, antibiotics were effective, but since the current patient has reduced kidney function, the LLM suggests an alternative medication.”

4. Logistics Optimization with Autonomous Mobile Robots (AMR)

  • Scenario: Multiple robots perform delivery and transport in a warehouse, flexibly adapting to obstacles or congestion using CBR + LLM.

  • Mechanism: Each robot remembers past cases of “traffic congestion” or “obstacle avoidance.” CBR selects an appropriate route based on the situation, and the LLM supplements instructions in natural language (e.g., “Aisle 3 is under maintenance, choose route B”). Roles are coordinated collaboratively (MAS).

  • Example: “From a similar congestion case, the LLM instructs the robot on an alternative route plus speed adjustment.”

5. Automated Customer Support (Collaborative Agents)

  • Scenario: In customer support chats, multiple specialized agents (account, billing, technical) collaborate by searching similar FAQs (CBR), with the LLM refining the response into natural language.

  • Mechanism: For a user query, multiple specialized agents search for similar cases. The LLM integrates the information and generates a coherent, context-appropriate answer. If necessary, the query is “handed off” to another agent.

  • Example: “For a combined issue of ‘app crashes + lost billing data,’ the technical and billing agents collaborate, and the LLM provides an integrated explanation.”

6. Educational and Personalized Learning Support Agents

  • Scenario: Learning support agents provide personalized feedback to students using past learning histories (CBR) plus LLM-based customization.

  • Mechanism: Search for similar patterns of difficulty (e.g., “Middle school student struggling with the Pythagorean theorem”). The LLM generates explanations tailored to the student’s progress and vocabulary level. Learning assistants divide responsibilities by subject (English, math, science) in an MAS configuration.

  • Example: “Based on a past case where a student struggling with factoring improved through a certain explanation, the LLM generates a simplified version for the current student.”

Domains Suited for This Approach

  • Rich availability of past cases.

  • Past solutions cannot be used as-is but can be adapted for similar problems.

  • Dynamic situations requiring flexible adaptation.

  • Tasks requiring multiple roles or perspectives where distributed processing is effective.

References

1. Case-Based Reasoning (CBR)

2. Multi-Agent Systems (MAS)

3. Integration of CBR and MAS

4. Linking CBR with LLMs and Modern AI (CBR + LLM / MAS)

      コメント

      タイトルとURLをコピーしました