Examples
Runnable examples demonstrating GoAI features. Each example is a standalone main.go that you can run directly.
All examples live in goai/examples/.
Core
Basic text generation, streaming, and structured output.
chat
Simple non-streaming text generation with GenerateText.
- Provider: Google Gemini
- Features:
GenerateText,WithSystem,WithPrompt, token usage - Source:
examples/chat/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/chat/main.gostreaming
Streaming text generation with real-time output via TextStream().
- Provider: Google Gemini
- Features:
StreamText,TextStream(),Result()for usage after streaming - Source:
examples/streaming/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/streaming/main.gostructured
Structured output with typed Go structs using GenerateObject[T] and StreamObject[T].
- Provider: Google Gemini
- Features:
GenerateObject[T],StreamObject[T],PartialObjectStream(),SchemaFrom[T], struct tags (jsonschema) - Source:
examples/structured/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run ./examples/structured/embedding
Text embeddings with Embed and EmbedMany, including cosine similarity comparison.
- Provider: Google Gemini (
text-embedding-004) - Features:
Embed,EmbedMany, auto-chunking, pairwise cosine similarity - Source:
examples/embedding/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run ./examples/embedding/citations
Accessing Sources from grounded AI responses. The Sources API is provider-agnostic.
- Provider: Google Gemini
- Features:
TextResult.Sources, per-step sources,Source.URL,Source.Title, text offsets - Source:
examples/citations/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/citations/main.goTools
Custom tools, agent loops, and provider-defined tools.
tools
Single-step tool call with a custom tool definition and Execute handler.
- Provider: Google Gemini
- Features:
goai.Tool,InputSchema(JSON Schema),Executefunction,WithMaxSteps(2) - Source:
examples/tools/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/tools/main.goagent-loop
Multi-step agent loop with multiple tools and step/tool-call callbacks.
- Provider: Google Gemini
- Features: Multiple tools,
WithMaxSteps(5),WithOnStepFinish,WithOnToolCall, step history - Source:
examples/agent-loop/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/agent-loop/main.goProvider-Defined Tools
These examples use built-in tools provided by specific AI providers. The provider handles tool execution server-side.
web-search
Web search across OpenAI, Anthropic, and Google using their respective provider-defined tools.
- Providers: OpenAI (direct + Azure), Anthropic (direct + Bedrock), Google
- Features:
openai.Tools.WebSearch(),anthropic.Tools.WebSearch(),google.Tools.GoogleSearch(), source citations - Source:
examples/web-search/
export OPENAI_API_KEY=...
go run examples/web-search/main.go openai
export ANTHROPIC_API_KEY=...
go run examples/web-search/main.go anthropic
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/web-search/main.go googlegoogle-search
Google Search grounding with Gemini, returning grounded responses with source URLs.
- Provider: Google Gemini
- Features:
google.Tools.GoogleSearch(), grounding sources - Source:
examples/google-search/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/google-search/main.goweb-fetch
Anthropic's web fetch tool for retrieving and processing content from specific URLs.
- Providers: Anthropic (direct + Bedrock)
- Features:
anthropic.Tools.WebFetch(), citation support, URL content processing - Source:
examples/web-fetch/
export ANTHROPIC_API_KEY=...
go run examples/web-fetch/main.go anthropiccomputer-use
Anthropic's computer use tools: Computer (mouse/keyboard), Bash (shell), TextEditor (file editing).
- Providers: Anthropic (direct + Bedrock)
- Features:
anthropic.Tools.Computer(),anthropic.Tools.Bash(),anthropic.Tools.TextEditor(), provider-defined tools with client-sideExecutehandlers - Source:
examples/computer-use/
export ANTHROPIC_API_KEY=...
go run examples/computer-use/main.go anthropiccode-execution
Anthropic's sandboxed Python code execution tool.
- Providers: Anthropic (direct + Bedrock)
- Features:
anthropic.Tools.CodeExecution(), server-side Python execution - Source:
examples/code-execution/
export ANTHROPIC_API_KEY=...
go run examples/code-execution/main.go anthropiccode-interpreter
OpenAI's code interpreter tool for sandboxed Python execution.
- Providers: OpenAI (direct + Azure)
- Features:
openai.Tools.CodeInterpreter(), server-side Python execution - Source:
examples/code-interpreter/
export OPENAI_API_KEY=...
go run examples/code-interpreter/main.go openaigoogle-code-execution
Google Gemini's sandboxed Python code execution tool.
- Provider: Google Gemini
- Features:
google.Tools.CodeExecution(), server-side Python execution - Source:
examples/google-code-execution/
export GOOGLE_GENERATIVE_AI_API_KEY=...
go run examples/google-code-execution/main.goimage-generation
OpenAI's image generation tool via the Responses API. The LLM decides when to generate images during conversation.
- Providers: OpenAI (direct + Azure)
- Features:
openai.Tools.ImageGeneration(), image quality/size options - Source:
examples/image-generation/
export OPENAI_API_KEY=...
go run examples/image-generation/main.go openaifile-search
OpenAI's file search tool for semantic/keyword search over vector stores.
- Providers: OpenAI (direct + Azure)
- Features:
openai.Tools.FileSearch(), vector store integration - Source:
examples/file-search/
Requires a pre-created vector store with uploaded files.
export OPENAI_API_KEY=...
go run examples/file-search/main.go openai <vector-store-id>