Create Smolagent.md
Browse files- Smolagent.md +127 -0
Smolagent.md
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Advanced RAG Tool for smolagents
|
2 |
+
|
3 |
+
This repository contains an improved Retrieval-Augmented Generation (RAG) tool built for the `smolagents` library from Hugging Face. This tool allows you to:
|
4 |
+
|
5 |
+
- Create vector stores from various document types (PDF, TXT, HTML, etc.)
|
6 |
+
- Choose different embedding models for better semantic understanding
|
7 |
+
- Configure chunk sizes and overlaps for optimal text splitting
|
8 |
+
- Select between different vector stores (FAISS or Chroma)
|
9 |
+
- Share your tool on the Hugging Face Hub
|
10 |
+
|
11 |
+
## Installation
|
12 |
+
|
13 |
+
```bash
|
14 |
+
pip install smolagents langchain-community langchain-text-splitters faiss-cpu chromadb sentence-transformers pypdf2 gradio
|
15 |
+
```
|
16 |
+
|
17 |
+
## Basic Usage
|
18 |
+
|
19 |
+
```python
|
20 |
+
from rag_tool import RAGTool
|
21 |
+
|
22 |
+
# Initialize the RAG tool
|
23 |
+
rag_tool = RAGTool()
|
24 |
+
|
25 |
+
# Configure with custom settings
|
26 |
+
rag_tool.configure(
|
27 |
+
documents_path="./my_document.pdf",
|
28 |
+
embedding_model="BAAI/bge-small-en-v1.5",
|
29 |
+
vector_store_type="faiss",
|
30 |
+
chunk_size=1000,
|
31 |
+
chunk_overlap=200,
|
32 |
+
persist_directory="./vector_store",
|
33 |
+
device="cpu" # Use "cuda" for GPU acceleration
|
34 |
+
)
|
35 |
+
|
36 |
+
# Query the documents
|
37 |
+
result = rag_tool("What is attention in transformer architecture?", top_k=3)
|
38 |
+
print(result)
|
39 |
+
```
|
40 |
+
|
41 |
+
## Using with an Agent
|
42 |
+
|
43 |
+
```python
|
44 |
+
import warnings
|
45 |
+
# Suppress LangChain deprecation warnings
|
46 |
+
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
47 |
+
|
48 |
+
from smolagents import CodeAgent, InferenceClientModel
|
49 |
+
from rag_tool import RAGTool
|
50 |
+
|
51 |
+
# Initialize and configure the RAG tool
|
52 |
+
rag_tool = RAGTool()
|
53 |
+
rag_tool.configure(documents_path="./my_document.pdf")
|
54 |
+
|
55 |
+
# Create an agent model
|
56 |
+
model = InferenceClientModel(
|
57 |
+
model_id="mistralai/Mistral-7B-Instruct-v0.2",
|
58 |
+
token="your_huggingface_token"
|
59 |
+
)
|
60 |
+
|
61 |
+
# Create the agent with our RAG tool
|
62 |
+
agent = CodeAgent(tools=[rag_tool], model=model, add_base_tools=True)
|
63 |
+
|
64 |
+
# Run the agent
|
65 |
+
result = agent.run("Explain the key components of the transformer architecture")
|
66 |
+
print(result)
|
67 |
+
```
|
68 |
+
|
69 |
+
## Gradio Interface
|
70 |
+
|
71 |
+
For an interactive experience, run the Gradio app:
|
72 |
+
|
73 |
+
```bash
|
74 |
+
python gradio_app.py
|
75 |
+
```
|
76 |
+
|
77 |
+
This provides a web interface where you can:
|
78 |
+
- Upload documents
|
79 |
+
- Configure embedding models and chunk settings
|
80 |
+
- Query your documents with semantic search
|
81 |
+
|
82 |
+
## Customization Options
|
83 |
+
|
84 |
+
### Embedding Models
|
85 |
+
|
86 |
+
You can choose from various embedding models:
|
87 |
+
- `sentence-transformers/all-MiniLM-L6-v2` (fast, smaller model)
|
88 |
+
- `BAAI/bge-small-en-v1.5` (good balance of performance and speed)
|
89 |
+
- `BAAI/bge-base-en-v1.5` (better performance, slower)
|
90 |
+
- `thenlper/gte-small` (good for general text embeddings)
|
91 |
+
- `thenlper/gte-base` (larger GTE model)
|
92 |
+
|
93 |
+
### Vector Store Types
|
94 |
+
|
95 |
+
- `faiss`: Fast, in-memory vector database (better for smaller collections)
|
96 |
+
- `chroma`: Persistent vector database with metadata filtering capabilities
|
97 |
+
|
98 |
+
### Document Types
|
99 |
+
|
100 |
+
The tool supports multiple document types:
|
101 |
+
- PDF documents
|
102 |
+
- Text files (.txt)
|
103 |
+
- Markdown files (.md)
|
104 |
+
- HTML files (.html)
|
105 |
+
- Entire directories of mixed document types
|
106 |
+
|
107 |
+
## Sharing Your Tool
|
108 |
+
|
109 |
+
You can share your tool on the Hugging Face Hub:
|
110 |
+
|
111 |
+
```python
|
112 |
+
rag_tool.push_to_hub("your-username/rag-retrieval-tool", token="your_huggingface_token")
|
113 |
+
```
|
114 |
+
|
115 |
+
## Limitations
|
116 |
+
|
117 |
+
- The tool currently doesn't support image content from PDFs
|
118 |
+
- Very large documents may require additional memory
|
119 |
+
- Some embedding models may be slow on CPU-only environments
|
120 |
+
|
121 |
+
## Contributing
|
122 |
+
|
123 |
+
Contributions are welcome! Feel free to open an issue or submit a pull request.
|
124 |
+
|
125 |
+
## License
|
126 |
+
|
127 |
+
MIT
|