File size: 9,859 Bytes
a864e35
 
2ce4d6f
a864e35
 
 
2ce4d6f
a864e35
 
 
 
 
 
2ce4d6f
a864e35
2ce4d6f
a864e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ce4d6f
 
 
 
 
 
 
 
 
a864e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ce4d6f
a864e35
 
 
2ce4d6f
a864e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ce4d6f
 
 
 
 
 
 
 
 
 
 
 
a864e35
 
 
 
 
 
 
 
fdae7a6
a864e35
 
 
 
 
fdae7a6
a864e35
 
fdae7a6
a864e35
 
fdae7a6
a864e35
 
 
 
 
fdae7a6
a864e35
 
 
 
 
 
 
 
2ce4d6f
a864e35
 
 
 
 
 
fdae7a6
a864e35
 
 
 
 
 
 
fdae7a6
a864e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdae7a6
 
 
a864e35
fdae7a6
 
a864e35
 
fdae7a6
 
 
a864e35
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
Ask My Research - RAG chatbot over Anthony Maio's AI safety papers.
Runs on HuggingFace Spaces using the Inference API.
"""

import json
import os
import time
from pathlib import Path
from collections import defaultdict

import gradio as gr
import numpy as np
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import faiss

# =============================================================================
# Configuration
# =============================================================================

INDEX_DIR = Path("index")
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
LLM_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
TOP_K = 5  # Number of chunks to retrieve
MAX_NEW_TOKENS = 512

# Rate limiting
RATE_LIMIT = 20  # requests per window
RATE_WINDOW = 3600  # 1 hour in seconds
request_log = defaultdict(list)

# Paper metadata for nice display
PAPER_INFO = {
    "manifold_model_organisms_arxiv": {
        "title": "Model Organisms of Supply-Chain Co-option",
        "url": "https://zenodo.org/records/18203353"
    },
    "slipstream-paper": {
        "title": "Slipstream: Semantic Quantization for Multi-Agent Coordination",
        "url": "https://zenodo.org/records/18115418"
    },
    "cmed_paper": {
        "title": "Cross-Model Epistemic Divergence (CMED)",
        "url": "https://making-minds.ai/research/cmed"
    },
    "hdcs_paper": {
        "title": "Heterogeneous Divergence-Convergence Swarm (HDCS)",
        "url": "https://making-minds.ai/research/hdcs"
    },
    "synthesis_paper": {
        "title": "Synthesis: Test-Driven AI Self-Extension",
        "url": "https://making-minds.ai/research/synthesis"
    },
    "Coherence-Seeking-Architectures": {
        "title": "Coherence-Seeking Architectures for Agentic AI",
        "url": "https://zenodo.org/records/18137928"
    },
}

# =============================================================================
# Rate Limiting
# =============================================================================

def check_rate_limit(request: gr.Request) -> tuple[bool, str]:
    """Check if request is within rate limits."""
    if request is None:
        return True, ""

    ip = request.client.host if request.client else "unknown"
    now = time.time()

    # Clean old entries
    request_log[ip] = [t for t in request_log[ip] if now - t < RATE_WINDOW]

    if len(request_log[ip]) >= RATE_LIMIT:
        remaining = int(RATE_WINDOW - (now - request_log[ip][0]))
        return False, f"Rate limit exceeded. Please try again in {remaining // 60} minutes."

    request_log[ip].append(now)
    return True, ""

# =============================================================================
# Load Index and Models
# =============================================================================

print("Loading embedding model...")
embed_model = SentenceTransformer(EMBEDDING_MODEL)

print("Loading FAISS index...")
index_path = INDEX_DIR / "faiss.index"
chunks_path = INDEX_DIR / "chunks.json"

if index_path.exists() and chunks_path.exists():
    faiss_index = faiss.read_index(str(index_path))
    with open(chunks_path, "r", encoding="utf-8") as f:
        chunks = json.load(f)
    print(f"Loaded {faiss_index.ntotal} vectors and {len(chunks)} chunks")
else:
    print("WARNING: Index not found. Run embed_papers.py first!")
    faiss_index = None
    chunks = []

# Initialize the Inference Client
print("Initializing HF Inference Client...")
hf_token = os.environ.get("HF_TOKEN")
if hf_token:
    client = InferenceClient(token=hf_token)
    print("Inference client ready with authentication")
else:
    client = InferenceClient()
    print("WARNING: No HF_TOKEN found - using unauthenticated requests")

# =============================================================================
# RAG Functions
# =============================================================================

def retrieve(query: str, top_k: int = TOP_K) -> list[dict]:
    """Retrieve relevant chunks for a query."""
    if faiss_index is None or not chunks:
        return []

    # Embed query
    query_embedding = embed_model.encode([query], convert_to_numpy=True)
    faiss.normalize_L2(query_embedding)

    # Search
    distances, indices = faiss_index.search(query_embedding, top_k)

    # Get chunks with scores
    results = []
    for dist, idx in zip(distances[0], indices[0]):
        if idx < len(chunks):
            chunk = chunks[idx].copy()
            chunk["score"] = float(dist)
            results.append(chunk)

    return results


def format_context(retrieved_chunks: list[dict]) -> str:
    """Format retrieved chunks as context for the LLM."""
    if not retrieved_chunks:
        return "No relevant context found."

    context_parts = []
    for i, chunk in enumerate(retrieved_chunks, 1):
        source = chunk.get("source", "Unknown")
        page = chunk.get("page", "?")
        text = chunk.get("text", "")
        context_parts.append(f"[Source {i}: {source}, Page {page}]\n{text}")

    return "\n\n---\n\n".join(context_parts)


def format_citations(retrieved_chunks: list[dict]) -> str:
    """Format citations for display."""
    if not retrieved_chunks:
        return ""

    seen_sources = set()
    citations = []

    for chunk in retrieved_chunks:
        source = chunk.get("source", "Unknown")
        if source in seen_sources:
            continue
        seen_sources.add(source)

        # Look up paper info
        paper = None
        for key, info in PAPER_INFO.items():
            if key.lower() in source.lower() or source.lower() in key.lower():
                paper = info
                break

        if paper:
            citations.append(f"- [{paper['title']}]({paper['url']}) (p. {chunk.get('page', '?')})")
        else:
            citations.append(f"- {source} (p. {chunk.get('page', '?')})")

    return "\n".join(citations)


# =============================================================================
# Generation with Inference API
# =============================================================================

def generate_response(query: str, context: str) -> str:
    """Generate response using the HF Inference API."""

    # Build prompt
    system_prompt = """You are a helpful research assistant that answers questions about Anthony Maio's AI safety research papers.

IMPORTANT RULES:
1. ONLY answer based on the provided context from the papers
2. If the context doesn't contain relevant information, say "I don't have information about that in the indexed papers"
3. Be precise and cite which paper/concept you're referencing
4. Keep responses concise but informative
5. Use technical terms accurately as defined in the papers"""

    user_prompt = f"""Context from research papers:
{context}

Question: {query}

Provide a helpful answer based ONLY on the context above. If the context doesn't contain relevant information, say so."""

    # Format for Mistral instruction format
    prompt = f"<s>[INST] {system_prompt}\n\n{user_prompt} [/INST]"

    # Call the Inference API
    response = client.text_generation(
        prompt,
        model=LLM_MODEL,
        max_new_tokens=MAX_NEW_TOKENS,
        temperature=0.7,
        top_p=0.9,
        repetition_penalty=1.1,
    )

    return response.strip()


# =============================================================================
# Chat Function
# =============================================================================

def chat(message: str, history: list, request: gr.Request) -> str:
    """Main chat function."""

    # Rate limit check
    allowed, error_msg = check_rate_limit(request)
    if not allowed:
        return error_msg

    if not message.strip():
        return "Please enter a question."

    if faiss_index is None:
        return "The paper index is not loaded. Please check the Space configuration."

    # Retrieve relevant chunks
    retrieved = retrieve(message)

    if not retrieved:
        return "I couldn't find relevant information in the indexed papers."

    # Format context
    context = format_context(retrieved)

    # Generate response
    try:
        response = generate_response(message, context)
    except Exception as e:
        return f"Error generating response: {type(e).__name__}: {str(e)}"

    # Add citations
    citations = format_citations(retrieved)
    if citations:
        response = f"{response}\n\n**Sources:**\n{citations}"

    return response


# =============================================================================
# Gradio Interface
# =============================================================================

DESCRIPTION = """
# 🔬 Ask My Research

Chat with Anthony Maio's AI safety research papers. Ask questions about:

- **CMED** - Cross-Model Epistemic Divergence (weak verifier failures)
- **HDCS** - Heterogeneous Divergence-Convergence Swarm (ensemble oversight)
- **Slipstream** - Semantic quantization for multi-agent coordination
- **Model Organisms** - Living-off-the-land failure modes in RAG agents
- **Coherence** - Architectures for agentic AI continuity

*Responses are grounded in the actual papers with citations.*
"""

EXAMPLES = [
    "What is CMED and why does it matter for AI safety?",
    "How does Slipstream achieve 82% token reduction?",
    "What are living-off-the-land (LotL) failure modes?",
    "Explain the HDCS architecture for scalable oversight",
    "What is the Manifold Resonance Architecture (MRA)?",
    "How do weak verifiers fail to detect deceptive reasoning?",
]

demo = gr.ChatInterface(
    fn=chat,
    type="messages",
    title="Ask My Research",
    description=DESCRIPTION,
    examples=EXAMPLES,
    theme=gr.themes.Soft(
        primary_hue="orange",
        secondary_hue="yellow",
    ),
)

if __name__ == "__main__":
    demo.launch()