Commit ·
20050d9
1
Parent(s): 6d11ec6
add project header
Browse files- README.md +23 -20
- assets/project_header.png +3 -0
README.md
CHANGED
|
@@ -16,9 +16,10 @@ tags:
|
|
| 16 |
- educational
|
| 17 |
- visualization
|
| 18 |
---
|
| 19 |
-
|
| 20 |
# Context Engineering Visualizer
|
| 21 |
|
|
|
|
|
|
|
| 22 |
A professional educational tool that demonstrates how information flows into an AI agent's context window before inference. Built with LangChain and Gradio.
|
| 23 |
|
| 24 |
Application deployed on Hugging Face: [Context Engineering Visualizer](https://huggingface.co/spaces/mcikalmerdeka/context-engineering-visualizer)
|
|
@@ -190,6 +191,7 @@ python app/process_knowledge.py
|
|
| 190 |
```
|
| 191 |
|
| 192 |
This will:
|
|
|
|
| 193 |
- Load the Product Strategy PDF document
|
| 194 |
- Split it into optimized chunks (800 chars with 150 char overlap)
|
| 195 |
- Create embeddings using OpenAI `text-embedding-3-small`
|
|
@@ -364,13 +366,13 @@ class ContextEngineeringAgent:
|
|
| 364 |
# Layer 1: System instructions (stable)
|
| 365 |
# Layer 2: Conversation history (recent only)
|
| 366 |
history_text = self.memory.get_history_text()
|
| 367 |
-
|
| 368 |
# Layer 3: Retrieved knowledge (top-2 relevant)
|
| 369 |
retrieved_docs = self.knowledge_base.retrieve_relevant(user_query)
|
| 370 |
-
|
| 371 |
# Layer 4: User query
|
| 372 |
# Layer 5: Tools (automatically handled by agent)
|
| 373 |
-
|
| 374 |
# Assemble with clear structure
|
| 375 |
context_message = f"""Context from Knowledge Base:
|
| 376 |
{retrieved_docs}
|
|
@@ -380,7 +382,7 @@ Previous Conversation:
|
|
| 380 |
|
| 381 |
Current Question:
|
| 382 |
{user_query}"""
|
| 383 |
-
|
| 384 |
return self.agent.invoke({"messages": [{"role": "user", "content": context_message}]})
|
| 385 |
```
|
| 386 |
|
|
@@ -392,37 +394,37 @@ class KnowledgeBase:
|
|
| 392 |
self.embeddings = OpenAIEmbeddings(model=embedding_model)
|
| 393 |
self.vectorstore = self._load_or_create_index(recreate=False)
|
| 394 |
self.top_k = top_k
|
| 395 |
-
|
| 396 |
def _load_or_create_index(self, recreate: bool = False) -> FAISS:
|
| 397 |
# Load existing index or create from PDF
|
| 398 |
if not recreate and os.path.exists(self.index_path):
|
| 399 |
return FAISS.load_local(self.index_path, self.embeddings)
|
| 400 |
-
|
| 401 |
# Load PDF and split into chunks
|
| 402 |
loader = PyPDFLoader(self.pdf_path)
|
| 403 |
documents = loader.load()
|
| 404 |
-
|
| 405 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 406 |
chunk_size=800, # Optimized for structured content
|
| 407 |
chunk_overlap=150, # Balance between context and redundancy
|
| 408 |
separators=["\n\n", "\n", ". ", ", ", " ", ""]
|
| 409 |
)
|
| 410 |
chunks = text_splitter.split_documents(documents)
|
| 411 |
-
|
| 412 |
# Create and save FAISS index
|
| 413 |
vectorstore = FAISS.from_documents(chunks, self.embeddings)
|
| 414 |
vectorstore.save_local(self.index_path)
|
| 415 |
return vectorstore
|
| 416 |
-
|
| 417 |
def retrieve_relevant(self, query: str, k: int = None) -> str:
|
| 418 |
"""Retrieve top-k relevant chunks with metadata"""
|
| 419 |
docs = self.vectorstore.similarity_search(query, k=k or self.top_k)
|
| 420 |
-
|
| 421 |
formatted_chunks = []
|
| 422 |
for i, doc in enumerate(docs, 1):
|
| 423 |
chunk_text = f"--- Chunk {i} ---\nMetadata: {doc.metadata}\n\n{doc.page_content}"
|
| 424 |
formatted_chunks.append(chunk_text)
|
| 425 |
-
|
| 426 |
return "\n\n".join(formatted_chunks)
|
| 427 |
```
|
| 428 |
|
|
@@ -465,33 +467,33 @@ We limit history to 4 messages. For longer conversations, this prevents context
|
|
| 465 |
def calculate_metric(metric_name: str, values: str) -> str:
|
| 466 |
"""
|
| 467 |
Compute an official business metric using centralized metrics logic.
|
| 468 |
-
|
| 469 |
This tool represents the company's authoritative metrics service.
|
| 470 |
Product strategy documents intentionally omit calculation formulas
|
| 471 |
and defer all computations to this tool to ensure consistency.
|
| 472 |
-
|
| 473 |
Supported metrics:
|
| 474 |
- "stam": Successful Transactions per Active Merchant
|
| 475 |
- "nrr": Net Revenue Retention
|
| 476 |
- "payment_success_rate": Adjusted Payment Success Rate
|
| 477 |
-
|
| 478 |
Args:
|
| 479 |
metric_name: Name of the metric to compute
|
| 480 |
values: Comma-separated numeric inputs (e.g., "125000, 500")
|
| 481 |
-
|
| 482 |
Returns:
|
| 483 |
Human-readable string with computed metric
|
| 484 |
"""
|
| 485 |
nums = [float(x.strip()) for x in values.split(",")]
|
| 486 |
-
|
| 487 |
if metric_name.lower() == "stam":
|
| 488 |
result = nums[0] / nums[1] # transactions / merchants
|
| 489 |
return f"STAM: {result:.2f} successful transactions per merchant"
|
| 490 |
-
|
| 491 |
elif metric_name.lower() == "nrr":
|
| 492 |
result = (nums[0] / nums[1]) * 100 # retained / starting
|
| 493 |
return f"Net Revenue Retention (NRR): {result:.2f}%"
|
| 494 |
-
|
| 495 |
# ... other metrics
|
| 496 |
```
|
| 497 |
|
|
@@ -503,6 +505,7 @@ def calculate_metric(metric_name: str, values: str) -> str:
|
|
| 503 |
- **Real-world modeling**: Mimics actual enterprise metric systems
|
| 504 |
|
| 505 |
This ensures:
|
|
|
|
| 506 |
1. **Consistency**: All metric calculations use same logic
|
| 507 |
2. **Maintainability**: Formula changes update in one place
|
| 508 |
3. **Auditability**: Tool calls are traceable
|
|
@@ -529,7 +532,7 @@ class Settings:
|
|
| 529 |
|
| 530 |
# UI settings
|
| 531 |
GRADIO_SERVER_PORT = 7860
|
| 532 |
-
|
| 533 |
# System prompt
|
| 534 |
SYSTEM_PROMPT = """You are an internal company knowledge assistant for AtlasPay..."""
|
| 535 |
```
|
|
|
|
| 16 |
- educational
|
| 17 |
- visualization
|
| 18 |
---
|
|
|
|
| 19 |
# Context Engineering Visualizer
|
| 20 |
|
| 21 |
+
[](https://huggingface.co/spaces/mcikalmerdeka/context-engineering-visualizer)
|
| 22 |
+
|
| 23 |
A professional educational tool that demonstrates how information flows into an AI agent's context window before inference. Built with LangChain and Gradio.
|
| 24 |
|
| 25 |
Application deployed on Hugging Face: [Context Engineering Visualizer](https://huggingface.co/spaces/mcikalmerdeka/context-engineering-visualizer)
|
|
|
|
| 191 |
```
|
| 192 |
|
| 193 |
This will:
|
| 194 |
+
|
| 195 |
- Load the Product Strategy PDF document
|
| 196 |
- Split it into optimized chunks (800 chars with 150 char overlap)
|
| 197 |
- Create embeddings using OpenAI `text-embedding-3-small`
|
|
|
|
| 366 |
# Layer 1: System instructions (stable)
|
| 367 |
# Layer 2: Conversation history (recent only)
|
| 368 |
history_text = self.memory.get_history_text()
|
| 369 |
+
|
| 370 |
# Layer 3: Retrieved knowledge (top-2 relevant)
|
| 371 |
retrieved_docs = self.knowledge_base.retrieve_relevant(user_query)
|
| 372 |
+
|
| 373 |
# Layer 4: User query
|
| 374 |
# Layer 5: Tools (automatically handled by agent)
|
| 375 |
+
|
| 376 |
# Assemble with clear structure
|
| 377 |
context_message = f"""Context from Knowledge Base:
|
| 378 |
{retrieved_docs}
|
|
|
|
| 382 |
|
| 383 |
Current Question:
|
| 384 |
{user_query}"""
|
| 385 |
+
|
| 386 |
return self.agent.invoke({"messages": [{"role": "user", "content": context_message}]})
|
| 387 |
```
|
| 388 |
|
|
|
|
| 394 |
self.embeddings = OpenAIEmbeddings(model=embedding_model)
|
| 395 |
self.vectorstore = self._load_or_create_index(recreate=False)
|
| 396 |
self.top_k = top_k
|
| 397 |
+
|
| 398 |
def _load_or_create_index(self, recreate: bool = False) -> FAISS:
|
| 399 |
# Load existing index or create from PDF
|
| 400 |
if not recreate and os.path.exists(self.index_path):
|
| 401 |
return FAISS.load_local(self.index_path, self.embeddings)
|
| 402 |
+
|
| 403 |
# Load PDF and split into chunks
|
| 404 |
loader = PyPDFLoader(self.pdf_path)
|
| 405 |
documents = loader.load()
|
| 406 |
+
|
| 407 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 408 |
chunk_size=800, # Optimized for structured content
|
| 409 |
chunk_overlap=150, # Balance between context and redundancy
|
| 410 |
separators=["\n\n", "\n", ". ", ", ", " ", ""]
|
| 411 |
)
|
| 412 |
chunks = text_splitter.split_documents(documents)
|
| 413 |
+
|
| 414 |
# Create and save FAISS index
|
| 415 |
vectorstore = FAISS.from_documents(chunks, self.embeddings)
|
| 416 |
vectorstore.save_local(self.index_path)
|
| 417 |
return vectorstore
|
| 418 |
+
|
| 419 |
def retrieve_relevant(self, query: str, k: int = None) -> str:
|
| 420 |
"""Retrieve top-k relevant chunks with metadata"""
|
| 421 |
docs = self.vectorstore.similarity_search(query, k=k or self.top_k)
|
| 422 |
+
|
| 423 |
formatted_chunks = []
|
| 424 |
for i, doc in enumerate(docs, 1):
|
| 425 |
chunk_text = f"--- Chunk {i} ---\nMetadata: {doc.metadata}\n\n{doc.page_content}"
|
| 426 |
formatted_chunks.append(chunk_text)
|
| 427 |
+
|
| 428 |
return "\n\n".join(formatted_chunks)
|
| 429 |
```
|
| 430 |
|
|
|
|
| 467 |
def calculate_metric(metric_name: str, values: str) -> str:
|
| 468 |
"""
|
| 469 |
Compute an official business metric using centralized metrics logic.
|
| 470 |
+
|
| 471 |
This tool represents the company's authoritative metrics service.
|
| 472 |
Product strategy documents intentionally omit calculation formulas
|
| 473 |
and defer all computations to this tool to ensure consistency.
|
| 474 |
+
|
| 475 |
Supported metrics:
|
| 476 |
- "stam": Successful Transactions per Active Merchant
|
| 477 |
- "nrr": Net Revenue Retention
|
| 478 |
- "payment_success_rate": Adjusted Payment Success Rate
|
| 479 |
+
|
| 480 |
Args:
|
| 481 |
metric_name: Name of the metric to compute
|
| 482 |
values: Comma-separated numeric inputs (e.g., "125000, 500")
|
| 483 |
+
|
| 484 |
Returns:
|
| 485 |
Human-readable string with computed metric
|
| 486 |
"""
|
| 487 |
nums = [float(x.strip()) for x in values.split(",")]
|
| 488 |
+
|
| 489 |
if metric_name.lower() == "stam":
|
| 490 |
result = nums[0] / nums[1] # transactions / merchants
|
| 491 |
return f"STAM: {result:.2f} successful transactions per merchant"
|
| 492 |
+
|
| 493 |
elif metric_name.lower() == "nrr":
|
| 494 |
result = (nums[0] / nums[1]) * 100 # retained / starting
|
| 495 |
return f"Net Revenue Retention (NRR): {result:.2f}%"
|
| 496 |
+
|
| 497 |
# ... other metrics
|
| 498 |
```
|
| 499 |
|
|
|
|
| 505 |
- **Real-world modeling**: Mimics actual enterprise metric systems
|
| 506 |
|
| 507 |
This ensures:
|
| 508 |
+
|
| 509 |
1. **Consistency**: All metric calculations use same logic
|
| 510 |
2. **Maintainability**: Formula changes update in one place
|
| 511 |
3. **Auditability**: Tool calls are traceable
|
|
|
|
| 532 |
|
| 533 |
# UI settings
|
| 534 |
GRADIO_SERVER_PORT = 7860
|
| 535 |
+
|
| 536 |
# System prompt
|
| 537 |
SYSTEM_PROMPT = """You are an internal company knowledge assistant for AtlasPay..."""
|
| 538 |
```
|
assets/project_header.png
ADDED
|
Git LFS Details
|