/query
Ask questions about images and get natural language answers.
Quick Start
First, install the package:
pip install moondream pillow
npm install moondream
pip install openai pillow
npm install openai
Place an image file in your working directory, then:
import moondream as md from PIL import Image # Initialize with API key model = md.vl(api_key="your-api-key") # Load an image image = Image.open("path/to/image.jpg") # Ask a question answer = model.query(image, "What's in this image?")["answer"] print("Answer:", answer) # Stream the response for chunk in model.query(image, "What's in this image?", stream=True)["answer"]: print(chunk, end="", flush=True)
import { vl } from "moondream"; import fs from "fs"; const model = new vl({ apiKey: "your-api-key" }); const image = fs.readFileSync("path/to/image.jpg"); async function main() { // Regular response const answer = await model.query({ image, question: "What's in this image?" }); console.log(answer); // Streamed response const stream = await model.query({ image, question: "What's in this image?", stream: true }); for await (const chunk of stream.answer) process.stdout.write(chunk); } main();
from openai import OpenAI import base64 # Load and encode image image_path = "path/to/image.jpg" with open(image_path, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode("utf-8") client = OpenAI( base_url="https://api.moondream.ai/v1", api_key="your-api-key" ) # For non-streaming response: response = client.chat.completions.create( model="moondream-2B", messages=[ { "role": "user", "content": [ { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{image_base64}" }, }, { "type": "text", "text": "What's in this image?", }, ], } ], # stream=True # Uncomment for streaming response ) # For non-streaming response: print(f"ID: {response.id}") print(f"Created: {response.created}") print(f"Model: {response.model}") print(f"Answer: {response.choices[0].message.content}") print(f"Usage - Prompt tokens: {response.usage.prompt_tokens}") print(f"Usage - Completion tokens: {response.usage.completion_tokens}") print(f"Usage - Total tokens: {response.usage.total_tokens}") # For streaming response: """ # Get first chunk to access metadata first_chunk = next(response) print(f"ID: {first_chunk.id}") print(f"Created: {first_chunk.created}") print(f"Model: {first_chunk.model}") print("Answer: ", end="", flush=True) print(first_chunk.choices[0].delta.content or "", end="", flush=True) for chunk in response: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="", flush=True) print() # Note: Usage statistics are not available in streaming mode """
const OpenAI = require('openai'); const fs = require('fs'); // Load and encode image const image_path = "path/to/image.jpg"; const image_base64 = fs.readFileSync(image_path).toString('base64'); const client = new OpenAI({ baseURL: "https://api.moondream.ai/v1", apiKey: "your-api-key" }); async function main() { try { // For non-streaming response: const response = await client.chat.completions.create({ model: "moondream-2B", messages: [ { role: "user", content: [ { type: "image_url", image_url: { url: `data:image/jpeg;base64,${image_base64}` } }, { type: "text", text: "What's in this image?" } ] } ], // stream: true // Uncomment for streaming response }); // For non-streaming response: console.log(`ID: ${response.id}`); console.log(`Created: ${response.created}`); console.log(`Model: ${response.model}`); console.log(`Answer: ${response.choices[0].message.content}`); console.log(`Usage - Prompt tokens: ${response.usage.prompt_tokens}`); console.log(`Usage - Completion tokens: ${response.usage.completion_tokens}`); console.log(`Usage - Total tokens: ${response.usage.total_tokens}`); // For streaming response: /* let isFirst = true; process.stdout.write("Answer: "); for await (const chunk of response) { if (isFirst) { console.log(`ID: ${chunk.id}`); console.log(`Created: ${chunk.created}`); console.log(`Model: ${chunk.model}`); process.stdout.write("Answer: "); isFirst = false; } if (chunk.choices[0].delta.content) { process.stdout.write(chunk.choices[0].delta.content); } } console.log(); // Note: Usage statistics are not available in streaming mode */ } catch (error) { console.error("Error:", error); } } main();
curl --location 'https://api.moondream.ai/v1/query' \ --header 'X-Moondream-Auth: <API KEY FROM console.moondream.ai>' \ --header 'Content-Type: application/json' \ --data '{ "image_url": "data:image/jpeg;base64,<BASE64-STRING>", "question": "What is this?", "stream": false }'
The client libraries provide the simplest integration for working with the Moondream API.