GitHub Copilot commited on
Commit
41a5ff3
·
1 Parent(s): 8cb4c46

Feature: Add NVIDIA NeMo Agent Toolkit connector (Protocol 4 enhancement)

Browse files
Files changed (3) hide show
  1. .env.template +4 -0
  2. logos/connectors.py +89 -2
  3. requirements.txt +1 -0
.env.template CHANGED
@@ -5,6 +5,10 @@
5
  # Get from: https://huggingface.co/settings/tokens
6
  HF_TOKEN=
7
 
 
 
 
 
8
  # Hugging Face Space ID (for deployment)
9
  HF_SPACE_ID=ANXLOG/LOGOS-SPCW-Matroska
10
 
 
5
  # Get from: https://huggingface.co/settings/tokens
6
  HF_TOKEN=
7
 
8
+ # NVIDIA API Key (for NeMo Agent Toolkit / Nemotron models)
9
+ # Get from: https://build.nvidia.com/
10
+ NVIDIA_API_KEY=
11
+
12
  # Hugging Face Space ID (for deployment)
13
  HF_SPACE_ID=ANXLOG/LOGOS-SPCW-Matroska
14
 
logos/connectors.py CHANGED
@@ -218,6 +218,86 @@ class VisionConnector:
218
  except ImportError as e:
219
  raise ImportError(f"Required library not installed: {e}")
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
  # ==========================================
223
  # FACTORY
@@ -228,7 +308,7 @@ def get_connector(connector_type: str, **kwargs) -> Any:
228
  Factory function for connectors.
229
 
230
  Args:
231
- connector_type: One of 'hf', 'ocr', 'vision'
232
  **kwargs: Connector-specific arguments
233
 
234
  Returns:
@@ -237,7 +317,8 @@ def get_connector(connector_type: str, **kwargs) -> Any:
237
  connectors = {
238
  'hf': HuggingFaceConnector,
239
  'ocr': OCRConnector,
240
- 'vision': VisionConnector
 
241
  }
242
 
243
  if connector_type not in connectors:
@@ -268,5 +349,11 @@ AVAILABLE_CONNECTORS = {
268
  'capabilities': ['calculate_ssim', 'analyze_entropy'],
269
  'requires': ['opencv-python-headless', 'scikit-image'],
270
  'env_vars': []
 
 
 
 
 
 
271
  }
272
  }
 
218
  except ImportError as e:
219
  raise ImportError(f"Required library not installed: {e}")
220
 
221
+ # ==========================================
222
+ # NEMO AGENT CONNECTOR (NVIDIA)
223
+ # ==========================================
224
+
225
+ class NeMoAgentConnector:
226
+ """
227
+ Adapter for NVIDIA NeMo Agent Toolkit.
228
+ Provides ReAct agents, tool calling, and vision-language models.
229
+ """
230
+
231
+ def __init__(self, api_key: Optional[str] = None, base_url: str = "https://integrate.api.nvidia.com/v1"):
232
+ self.api_key = api_key or os.environ.get('NVIDIA_API_KEY')
233
+ self.base_url = base_url
234
+ self._agent = None
235
+
236
+ def _ensure_agent(self):
237
+ """Lazy initialization of NeMo Agent."""
238
+ if self._agent is None:
239
+ try:
240
+ from nat import Agent
241
+ self._agent = Agent(api_key=self.api_key, base_url=self.base_url)
242
+ except ImportError:
243
+ raise ImportError("nvidia-nat not installed. Run: pip install nvidia-nat")
244
+ return self._agent
245
+
246
+ def chat(self, message: str, model: str = "nvidia/nemotron-3-nano-30b-a3b") -> str:
247
+ """
248
+ Chat with NeMo agent.
249
+
250
+ Args:
251
+ message: User message
252
+ model: NVIDIA model ID
253
+
254
+ Returns:
255
+ Agent response
256
+ """
257
+ try:
258
+ from openai import OpenAI
259
+ client = OpenAI(api_key=self.api_key, base_url=self.base_url)
260
+ response = client.chat.completions.create(
261
+ model=model,
262
+ messages=[{"role": "user", "content": message}]
263
+ )
264
+ return response.choices[0].message.content
265
+ except Exception as e:
266
+ return f"[NeMo Error] {e}"
267
+
268
+ def analyze_diagram(self, image_path: str, prompt: str = "Describe this architectural diagram in detail.") -> str:
269
+ """
270
+ Analyze diagram/image using vision-language model.
271
+
272
+ Args:
273
+ image_path: Path to image
274
+ prompt: Analysis prompt
275
+
276
+ Returns:
277
+ Analysis text
278
+ """
279
+ try:
280
+ import base64
281
+ from openai import OpenAI
282
+
283
+ with open(image_path, 'rb') as f:
284
+ image_data = base64.b64encode(f.read()).decode('utf-8')
285
+
286
+ client = OpenAI(api_key=self.api_key, base_url=self.base_url)
287
+ response = client.chat.completions.create(
288
+ model="nvidia/nemotron-nano-12b-v2-vl",
289
+ messages=[{
290
+ "role": "user",
291
+ "content": [
292
+ {"type": "text", "text": prompt},
293
+ {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_data}"}}
294
+ ]
295
+ }]
296
+ )
297
+ return response.choices[0].message.content
298
+ except Exception as e:
299
+ return f"[Vision Error] {e}"
300
+
301
 
302
  # ==========================================
303
  # FACTORY
 
308
  Factory function for connectors.
309
 
310
  Args:
311
+ connector_type: One of 'hf', 'ocr', 'vision', 'nemo'
312
  **kwargs: Connector-specific arguments
313
 
314
  Returns:
 
317
  connectors = {
318
  'hf': HuggingFaceConnector,
319
  'ocr': OCRConnector,
320
+ 'vision': VisionConnector,
321
+ 'nemo': NeMoAgentConnector
322
  }
323
 
324
  if connector_type not in connectors:
 
349
  'capabilities': ['calculate_ssim', 'analyze_entropy'],
350
  'requires': ['opencv-python-headless', 'scikit-image'],
351
  'env_vars': []
352
+ },
353
+ 'nemo': {
354
+ 'name': 'NVIDIA NeMo Agent Toolkit',
355
+ 'capabilities': ['chat', 'analyze_diagram', 'react_agent', 'tool_calling'],
356
+ 'requires': ['nvidia-nat', 'openai'],
357
+ 'env_vars': ['NVIDIA_API_KEY']
358
  }
359
  }
requirements.txt CHANGED
@@ -7,3 +7,4 @@ huggingface_hub<0.21.0
7
  plotly
8
  sympy
9
  easyocr
 
 
7
  plotly
8
  sympy
9
  easyocr
10
+ nvidia-nat