This example demonstrates how to create and use an Agent with NVIDIA models using the Upsonic framework.
This example includes:
- NvidiaModel: Uses NVIDIA NIM API to access various AI models
- Agent: Upsonic Agent configured with NVIDIA model
- Task: Simple task execution with user queries
- FastAPI Server: Run as a server using
upsonic runcommand - Direct Execution: Run directly with
python main.py
Install the required dependencies using the Upsonic CLI:
upsonic install all # Installs all dependencies in the upsonic_configs.json
# OR
upsonic install <dependency group>Alternatively, you can install specific sections:
upsonic install api- Install API dependencies onlyupsonic install all- Install all dependencies (api, streamlit, development)upsonic install- Install only "api" group as default
-
Copy
env.exampleto.env:cp env.example .env
-
Edit
.envand add your NVIDIA API key:NVIDIA_API_KEY=your_actual_api_key_here -
Get your NVIDIA API key:
- Visit https://build.nvidia.com/
- Sign up or log in to your NVIDIA account
- Navigate to API Keys section
- Create a new API key
- Copy the key to your
.envfile
Run the agent as a FastAPI server using the Upsonic CLI:
upsonic runThis will:
- Start a FastAPI server on
http://0.0.0.0:8000 - Create an API endpoint at
/callthat accepts POST requests - Generate OpenAPI documentation at
/docs - Use the
async main(inputs)function frommain.py
API Usage:
# Using curl
curl -X POST "http://localhost:8000/call" \
-H "Content-Type: application/json" \
-d '{"user_query": "What is artificial intelligence?"}'
# Or visit http://localhost:8000/docs for interactive API documentationRun the example directly:
uv run main.pyThe example uses the following environment variables:
- NVIDIA_API_KEY (required): Your NVIDIA API key. Alternatively, you can use
NGC_API_KEY. - NVIDIA_BASE_URL (optional): Custom base URL for NVIDIA NIM endpoint. Defaults to
https://integrate.api.nvidia.com/v1.
The upsonic_configs.json file controls:
- agent_name: Name of the agent ("NVIDIA Agent")
- description: Agent description
- entrypoints.api_file: Main Python file (
main.py) - input_schema: Defines the input parameters (currently
user_query) - output_schema: Defines the output structure (currently
bot_response) - dependencies: Lists all required packages for different environments
You can change the model in main.py by modifying the model_name parameter in the NvidiaModel constructor. Available models include:
meta/llama-3.1-nemotron-70b-instruct:1.0(default)openai/gpt-oss-20b:1.0mistral/mistral-large:2.0- And many more available through NVIDIA NIM
Check the NVIDIA NIM documentation for the full list of available models.
nvidia-agent/
├── main.py # Main agent file with async main(inputs) function for FastAPI server
├── upsonic_configs.json # Configuration file defining agent metadata, input/output schemas, and dependencies
├── env.example # Example environment variables file (copy to .env and add your API key)
└── README.md # This documentation file
upsonic init: Initialize a new agent projectupsonic install [section]: Install dependencies fromupsonic_configs.jsonupsonic run [--host HOST] [--port PORT]: Run the agent as a FastAPI serverupsonic add <library> <section>: Add a dependency to the configupsonic remove <library> <section>: Remove a dependency from the config
- Missing API Key: Make sure
NVIDIA_API_KEYis set in your.envfile - Dependencies Not Installed: Run
upsonic installto install required packages - Port Already in Use: Use
upsonic run --port 8001to use a different port - Model Not Found: Check that the model name is correct and available in NVIDIA NIM