Examples
These examples show complete programs you can copy, compile, and adapt. They
all live in the repository’s examples/
directory.
Most examples use OpenAI. Set OPENAI_API_KEY before running them, or follow
the provider-specific instructions on the smoke-test pages.
Run an example from the repository root:
OPENAI_API_KEY=... nim c -r examples/generate_text.nimThe examples cover:
- Generate text: make a basic model request.
- Stream text: print text as it arrives.
- Async generation: run requests concurrently.
- Tool call: let the model call a typed local function.
- Agent: reuse agent configuration across runs.
- Agent events: observe events and approve tools.
- Lifecycle callbacks: monitor retries, tools, and steps.
- Structured output: decode a response into a Nim object.
- Stream object: receive partial structured output.
- Conversation: continue and serialize a conversation.
- Embeddings: create vectors and compare similarity.
- Provider options: configure provider-specific settings.
- Wrap provider: add request and response hooks.
- Chat with PDF: send a PDF as file content.
- MCP client: discover and call MCP tools.
- Anthropic smoke test: try Anthropic streaming and JSON output.
- Google smoke test: try Gemini hosted tools and citations.
The PDF example uses the repository’s bundled examples/example.pdf file. The
smoke tests make live provider requests and need their provider credentials.