Duibonduil commited on
Commit
5d344d1
·
verified ·
1 Parent(s): 575af01

Upload main.py

Browse files
Files changed (1) hide show
  1. examples/main.py +214 -0
examples/main.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from anyio import to_thread
2
+ from starlette.applications import Starlette
3
+ from starlette.responses import HTMLResponse, JSONResponse
4
+ from starlette.routing import Route
5
+
6
+ from smolagents import CodeAgent, InferenceClientModel, MCPClient
7
+
8
+
9
+ # Create an MCP client to connect to the MCP server
10
+ mcp_server_parameters = {
11
+ "url": "https://evalstate-hf-mcp-server.hf.space/mcp",
12
+ "transport": "streamable-http",
13
+ }
14
+ mcp_client = MCPClient(server_parameters=mcp_server_parameters)
15
+
16
+ # Create a CodeAgent with a specific model and the tools from the MCP client
17
+ agent = CodeAgent(
18
+ model=InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
19
+ tools=mcp_client.get_tools(),
20
+ )
21
+
22
+
23
+ # Define the shutdown handler to disconnect the MCP client
24
+ async def shutdown():
25
+ mcp_client.disconnect()
26
+
27
+
28
+ async def homepage(request):
29
+ return HTMLResponse(
30
+ r"""
31
+ <!DOCTYPE html>
32
+ <html lang="en">
33
+ <head>
34
+ <meta charset="UTF-8">
35
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
36
+ <title>Smolagents Demo</title>
37
+ <style>
38
+ body {
39
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
40
+ max-width: 800px;
41
+ margin: 0 auto;
42
+ padding: 20px;
43
+ background-color: #f5f5f5;
44
+ }
45
+ .container {
46
+ background: white;
47
+ border-radius: 12px;
48
+ padding: 30px;
49
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
50
+ }
51
+ h1 {
52
+ color: #333;
53
+ text-align: center;
54
+ margin-bottom: 30px;
55
+ }
56
+ .chat-container {
57
+ border: 1px solid #ddd;
58
+ border-radius: 8px;
59
+ height: 400px;
60
+ overflow-y: auto;
61
+ padding: 15px;
62
+ margin-bottom: 20px;
63
+ background-color: #fafafa;
64
+ }
65
+ .message {
66
+ margin-bottom: 15px;
67
+ padding: 10px;
68
+ border-radius: 6px;
69
+ }
70
+ .user-message {
71
+ background-color: #007bff;
72
+ color: white;
73
+ margin-left: 50px;
74
+ }
75
+ .agent-message {
76
+ background-color: #e9ecef;
77
+ color: #333;
78
+ margin-right: 50px;
79
+ }
80
+ .input-container {
81
+ display: flex;
82
+ gap: 10px;
83
+ }
84
+ input[type="text"] {
85
+ flex: 1;
86
+ padding: 12px;
87
+ border: 1px solid #ddd;
88
+ border-radius: 6px;
89
+ font-size: 16px;
90
+ }
91
+ button {
92
+ padding: 12px 24px;
93
+ background-color: #007bff;
94
+ color: white;
95
+ border: none;
96
+ border-radius: 6px;
97
+ cursor: pointer;
98
+ font-size: 16px;
99
+ }
100
+ button:hover {
101
+ background-color: #0056b3;
102
+ }
103
+ button:disabled {
104
+ background-color: #ccc;
105
+ cursor: not-allowed;
106
+ }
107
+ .loading {
108
+ color: #666;
109
+ font-style: italic;
110
+ }
111
+ </style>
112
+ </head>
113
+ <body>
114
+ <div class="container">
115
+ <h1>🤖 Smolagents Demo</h1>
116
+ <div class="chat-container" id="chat-container">
117
+ <div class="message agent-message">
118
+ Hello! I'm a code agent with access to MCP tools. Ask me anything!
119
+ </div>
120
+ </div>
121
+ <div class="input-container">
122
+ <input type="text" id="message-input" placeholder="Ask me anything..." autofocus>
123
+ <button onclick="sendMessage()" id="send-button">Send</button>
124
+ </div>
125
+ </div>
126
+
127
+ <script>
128
+ const chatContainer = document.getElementById('chat-container');
129
+ const messageInput = document.getElementById('message-input');
130
+ const sendButton = document.getElementById('send-button');
131
+
132
+ function addMessage(content, isUser = false) {
133
+ const messageDiv = document.createElement('div');
134
+ messageDiv.className = `message ${isUser ? 'user-message' : 'agent-message'}`;
135
+ messageDiv.textContent = content;
136
+ chatContainer.appendChild(messageDiv);
137
+ chatContainer.scrollTop = chatContainer.scrollHeight;
138
+ }
139
+
140
+ async function sendMessage() {
141
+ const message = messageInput.value.trim();
142
+ if (!message) return;
143
+
144
+ // Add user message
145
+ addMessage(message, true);
146
+ messageInput.value = '';
147
+ sendButton.disabled = true;
148
+ sendButton.textContent = 'Sending...';
149
+
150
+ // Add loading indicator
151
+ const loadingDiv = document.createElement('div');
152
+ loadingDiv.className = 'message agent-message loading';
153
+ loadingDiv.textContent = 'Thinking...';
154
+ chatContainer.appendChild(loadingDiv);
155
+ chatContainer.scrollTop = chatContainer.scrollHeight;
156
+
157
+ try {
158
+ const response = await fetch('/chat', {
159
+ method: 'POST',
160
+ headers: {
161
+ 'Content-Type': 'application/json',
162
+ },
163
+ body: JSON.stringify({ message }),
164
+ });
165
+
166
+ const data = await response.json();
167
+
168
+ // Remove loading indicator
169
+ chatContainer.removeChild(loadingDiv);
170
+
171
+ // Add agent response
172
+ addMessage(data.reply);
173
+ } catch (error) {
174
+ // Remove loading indicator
175
+ chatContainer.removeChild(loadingDiv);
176
+ addMessage(`Error: ${error.message}`);
177
+ } finally {
178
+ sendButton.disabled = false;
179
+ sendButton.textContent = 'Send';
180
+ messageInput.focus();
181
+ }
182
+ }
183
+
184
+ // Send message on Enter key
185
+ messageInput.addEventListener('keypress', function(e) {
186
+ if (e.key === 'Enter') {
187
+ sendMessage();
188
+ }
189
+ });
190
+ </script>
191
+ </body>
192
+ </html>
193
+ """
194
+ )
195
+
196
+
197
+ async def chat(request):
198
+ data = await request.json()
199
+ message = data.get("message", "").strip()
200
+ # Run in a thread to avoid blocking the event loop
201
+ result = await to_thread.run_sync(agent.run, message)
202
+ # Format the result if it's a complex data structure
203
+ reply = str(result)
204
+ return JSONResponse({"reply": reply})
205
+
206
+
207
+ app = Starlette(
208
+ debug=True,
209
+ routes=[
210
+ Route("/", homepage),
211
+ Route("/chat", chat, methods=["POST"]),
212
+ ],
213
+ on_shutdown=[shutdown], # Register the shutdown handler: disconnect the MCP client
214
+ )