File size: 3,955 Bytes
5c01b9f
 
 
97379bb
 
 
 
 
 
 
 
 
 
 
5c01b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6570d62
74601ce
 
6570d62
5c01b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97379bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c01b9f
 
 
 
 
 
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
# unsloth_doc_app.py
import streamlit as st
import os
from smolagents import (
    CodeAgent,
    ToolCallingAgent,
    HfApiModel,
    DuckDuckGoSearchTool,
    LiteLLMModel,
    tool,
    VisitWebpageTool,
    GradioUI,
    PythonInterpreterTool
)
import requests
import json
from typing import List, Dict, Any

# Configuration
PAGE_CONFIG = {
    "page_title": "Unsloth Documentation Search",
    "page_icon": "πŸ“˜",
    "layout": "wide"
}

AUTHORIZED_IMPORTS: List[str] = [
    'random', 'collections', 'datetime', 'time', 'queue', 'unicodedata', 
    'os', 'stat', 'json', 'math', 're', 'itertools', 'statistics', 
    'pandas', 'numpy', 'requests'
]

# Agent Initialization
def initialize_unsloth_agent(model=None):
    """Initialize the Unsloth Documentation Agent"""
    tools = [
        DuckDuckGoSearchTool(),
        VisitWebpageTool()
    ]
    if model is None:
        from smolagents import HfApiModel
        model = HfApiModel(
            model_id="meta-llama/Llama-4-Scout-17B-16E-Instruct"
            # provider="together",
        )
    
    return CodeAgent(
        name="unsloth_agent",
        description="Unsloth Documentation AI Agent",
        tools=tools,
        model=model,
        max_steps=12,
        additional_authorized_imports=AUTHORIZED_IMPORTS
    )

# UI Components
def setup_ui():
    """Setup Streamlit UI components"""
    st.set_page_config(**PAGE_CONFIG)
    
    st.title("πŸ“˜ Unsloth Documentation Search")
    st.markdown("""
    This tool uses an AI agent to help you explore Unsloth documentation.
    Ask questions about Unsloth features, usage, or troubleshooting.
    """)

def display_results(results: str):
    """Display search results in a formatted way"""
    st.markdown("### πŸ“ Results")
    st.markdown(results, unsafe_allow_html=True)

def setup_sidebar():
    """Setup sidebar content"""
    with st.sidebar:
        st.markdown("### About This Tool")
        st.markdown("""
        This agent specializes in Unsloth documentation, providing:
        - πŸ“š Quick answers from web searches
        - 🌐 Webpage content retrieval
        - πŸ€– AI-powered insights
        """)
        st.markdown("### Tips")
        st.markdown("""
        - Use specific queries like "How to install Unsloth" or "Unsloth fine-tuning guide".
        - Results may include raw data from searches or webpages.
        """)

# Main App
@st.cache_resource
def initialize_app():
    """Initialize the application"""
    return initialize_unsloth_agent()

def main():
    setup_ui()
    setup_sidebar()
    
    if 'agent' not in st.session_state:
        with st.spinner("Initializing Unsloth Documentation Agent..."):
            st.session_state.agent = initialize_app()

    search_query = st.text_input(
        "πŸ” Search Unsloth Documentation",
        placeholder="E.g., How to fine-tune with Unsloth"
    )

    # Single button to handle both cases
    if st.button("Search", type="primary", key="search_button"):
        if search_query:
            with st.spinner("Searching Unsloth documentation..."):
                try:
                    results = st.session_state.agent.run(search_query, additional_args={"sources" : ["https://docs.unsloth.ai/"],"instructions": "Response to the user answer as a structured documentation format."})
                    display_results(results)
                    
                    st.markdown("---")
                    st.info("""
                    πŸ’‘ **How to read the results:**
                    - Results may include JSON from searches or webpage snippets.
                    - Check the sidebar for tips on refining your query.
                    """)
                except Exception as e:
                    st.error(f"An error occurred: {e}")
        else:
            st.warning("Please enter a search query.")

    st.markdown("---")
    st.caption("Powered by SmolAgents and Unsloth")

if __name__ == "__main__":
    main()