File size: 3,650 Bytes
77d6bf8
7ea840e
dfe9436
2d69ce4
77d6bf8
2d69ce4
dfe9436
7ea840e
2d69ce4
 
 
dfe9436
2d69ce4
 
 
 
 
7ea840e
f2f4514
 
7ea840e
fb8b6c8
 
7116e42
77d6bf8
f2f4514
94f34de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea840e
 
2d69ce4
 
7ea840e
f2f4514
fb8b6c8
 
f2f4514
2d69ce4
94f34de
f2f4514
fb8b6c8
94f34de
 
2d69ce4
7ea840e
 
 
 
94f34de
 
 
 
fb8b6c8
94f34de
 
 
 
 
 
 
 
fb8b6c8
2d69ce4
 
 
dfe9436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb8b6c8
f2f4514
7ea840e
 
fb8b6c8
7ea840e
fb8b6c8
7ea840e
fb8b6c8
94f34de
f2f4514
 
94f34de
 
2d69ce4
94f34de
2d69ce4
03a7b12
94f34de
2d69ce4
 
94f34de
2d69ce4
 
 
 
 
f2f4514
dfe9436
fb8b6c8
2d69ce4
fb8b6c8
 
 
77d6bf8
7ea840e
f2f4514
fb8b6c8
f2f4514
2d69ce4
77d6bf8
 
 
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
import gradio as gr
import numpy as np
import requests
import os

from sentence_transformers import SentenceTransformer


# ==============================
# 1. Setup Gemini API
# ==============================
#client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))

# ==============================
# 2. Load Embedding Model
# ==============================
embed_model = SentenceTransformer("all-MiniLM-L6-v2")


# Load data
def load_data():
    with open("data.txt", "r") as f:
        return f.read()


# Chunking
def chunk_text(text, chunk_size=300):
    sentences = text.split(". ")
    
    chunks = []
    current_chunk = ""

    for sentence in sentences:
        if len(current_chunk) + len(sentence) < chunk_size:
            current_chunk += sentence + ". "
        else:
            chunks.append(current_chunk.strip())
            current_chunk = sentence + ". "

    if current_chunk:
        chunks.append(current_chunk.strip())

    return chunks


# Embed chunks 
# Convert all documents into vectors
def embed_chunks(chunks):
    return embed_model.encode(chunks)


# Retrieve relevant chunks
# Retrieve Top-K Chunks (Dynamic chunk)
def retrieve(query, chunks, embeddings, top_k=3, threshold=0.3):
    query_embedding = embed_model.encode([query])[0]

    scored_chunks = []

    # Similarity Function
    for i, emb in enumerate(embeddings):
        similarity = np.dot(query_embedding, emb) / (
            np.linalg.norm(query_embedding) * np.linalg.norm(emb)
        )
        scored_chunks.append((similarity, chunks[i]))

    # Sort by similarity
    scored_chunks.sort(reverse=True)

    # Filter by threshold
    filtered = [chunk for score, chunk in scored_chunks if score > threshold]

    # If nothing passes, fallback to top_k
    if not filtered:
        filtered = [chunk for _, chunk in scored_chunks[:top_k]]

    return filtered[:top_k]

# ==============================
# Gemini Generation
# ==============================
#def generate_with_gemini(prompt):
#    response = client.models.generate_content(
#       model="gemini-2.0-flash",   # ✅ USE THIS (not 1.5)
#       contents=prompt
#   )
#   return response.text

def generate_with_grok(prompt):
    url = "https://api.x.ai/v1/chat/completions"

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {os.environ.get('XAI_API_KEY')}"
    }

    payload = {
        "model": "grok-4-1-fast",
        "messages": [
            {"role": "system", "content": "You are an AI systems expert."},
            {"role": "user", "content": prompt}
        ],
        "temperature": 0.3
    }

    response = requests.post(url, headers=headers, json=payload)

    return response.json()["choices"][0]["message"]["content"]

# Final RAG pipeline
def ask_ai(query):
    text = load_data()
    chunks = chunk_text(text)
    embeddings = embed_chunks(chunks)

    relevant_chunks = retrieve(query, chunks, embeddings)

    context = "\n\n".join([f"Context {i+1}: {chunk}" for i, chunk in enumerate(relevant_chunks)])

    prompt = f"""
    You are an AI systems expert.
    
    Use ONLY the context below to answer the question.
    
    Context:
    {context}
    
    Question:
    {query}
    
    Answer clearly with:
    1. Definition
    2. How it works
    3. Example
    """ 

    answer = generate_with_grok(prompt)

    return answer


# UI
demo = gr.Interface(
    fn=ask_ai,
    inputs=gr.Textbox(lines=3, placeholder="Ask anything about AI systems..."),
    outputs=gr.Textbox(lines=15),
    title="AIWisdom — Full RAG System",
    description="Retrieval + LLM reasoning (real AI system)",
)

demo.launch()