An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: Restore Ollama provider support for v1.0.0 (#721)
Yoshiki Miura331e10774282
diff --git a/.env.local.example b/.env.local.exampleindex 48ab5bcc..fe6d922c 100644--- a/.env.local.example+++ b/.env.local.example@@ -37,6 +37,12 @@ OPENAI_API_KEY=[YOUR_OPENAI_API_KEY]# Unified API gateway for multiple providers# AI_GATEWAY_API_KEY=[YOUR_AI_GATEWAY_API_KEY]+# Option 5: Ollama (Self-hosted)+# Run local AI models with Ollama+# Requires Ollama to be installed and running: https://ollama.ai/+# Models must support 'tools' capability to work with Morphic+# OLLAMA_BASE_URL=http://localhost:11434+# -----------------------------------------------------------------------------# Primary Search Provider Configuration (Required - Choose One)# -----------------------------------------------------------------------------diff --git a/README.md b/README.mdindex ef26e63b..a7501b0d 100644--- a/README.md+++ b/README.md@@ -93,6 +93,7 @@ Your feedback helps make Morphic better for everyone!- Anthropic Claude- Google Gemini- Vercel AI Gateway+- Ollama (Self-hosted)Models are configured in `config/models/*.json` with profile-based settings. When using non-OpenAI providers, update the model configuration files with compatible model IDs. See [Configuration Guide](docs/CONFIGURATION.md) for details.diff --git a/bun.lock b/bun.lockindex 8442f041..81c5996f 100644--- a/bun.lock+++ b/bun.lock@@ -56,6 +56,7 @@"next": "^15.2.3","next-themes": "^0.3.0","node-html-parser": "^6.1.13",+ "ollama-ai-provider-v2": "^1.5.0","postgres": "^3.4.5","react": "^19.0.0","react-dom": "^19.0.0",@@ -1838,6 +1839,8 @@"object.values": ["[email protected]", "", { "dependencies": { "call-bind": "^1.0.8", "call-bound": "^1.0.3", "define-properties": "^1.2.1", "es-object-atoms": "^1.0.0" } }, "sha512-gXah6aZrcUxjWg2zR2MwouP2eHlCBzdV4pygudehaKXSGW4v2AsRQUK+lwwXhii6KFZcunEnmSUoYp5CXibxtA=="],+ "ollama-ai-provider-v2": ["[email protected]", "", { "dependencies": { "@ai-sdk/provider": "^2.0.0", "@ai-sdk/provider-utils": "^3.0.7" }, "peerDependencies": { "zod": "^4.0.16" } }, "sha512-o8nR80AaENpetYdCtlnZGEwO47N6Z6eEsBetitrh4nTXrbfWdRF4OWE5p5oHyo0R8sxg4zdxUsjmGVzPd3zBUQ=="],+"once": ["[email protected]", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="],"oniguruma-parser": ["[email protected]", "", {}, "sha512-8Unqkvk1RYc6yq2WBYRj4hdnsAxVze8i7iPfQr8e4uSP3tRv0rpZcbGUDvxfQQcdwHt/e9PrMvGCsa8OqG9X3w=="],diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.mdindex 57ada319..35035335 100644--- a/docs/CONFIGURATION.md+++ b/docs/CONFIGURATION.md@@ -193,6 +193,61 @@ ANTHROPIC_API_KEY=[YOUR_API_KEY]AI_GATEWAY_API_KEY=[YOUR_AI_GATEWAY_API_KEY]```+#### Ollama (Self-hosted)++[Ollama](https://ollama.ai/) enables you to run large language models locally on your own hardware, providing privacy, cost savings, and offline capabilities.++**Setup:**++1. Install Ollama following the [official installation guide](https://ollama.ai/download)++2. Pull a model with tools support (required for Morphic):++```bash+# Example: Pull Llama 3.2 with tools support+ollama pull llama3.2++# Or other models with tools capability:+ollama pull qwen2.5+ollama pull mistral+```++3. Configure your environment:++```bash+OLLAMA_BASE_URL=http://localhost:11434+```++**Important Notes:**++- **Tools Capability Required**: Morphic requires models to support the `tools` capability for web search functionality. On server startup, Morphic will automatically check which of your Ollama models support tools and log the results.++- **Model Configuration**: Update your `config/models/*.json` files to use Ollama models. Set `providerId` to `ollama` and use the model name from Ollama:++```json+{+ "id": "llama3.2:latest",+ "name": "Llama 3.2",+ "provider": "Ollama",+ "providerId": "ollama"+}+```++- **Checking Model Capabilities**: To verify if a model supports tools, check the server logs on startup. Morphic will display which models passed validation:++```+✓ llama3.2:latest supports tools+✗ codellama:latest does not support tools (skipped)+```++- **Performance**: For best performance, ensure your system meets Ollama's hardware requirements. GPU acceleration is recommended for larger models.++**Troubleshooting:**++- If Morphic can't connect to Ollama, verify it's running: `ollama list`+- Check that `OLLAMA_BASE_URL` points to the correct host (use `http://host.docker.internal:11434` when running Morphic in Docker)+- Review startup logs for model validation results+## Other Features### LLM Observabilitydiff --git a/instrumentation.ts b/instrumentation.tsindex 27b161f2..b0d73538 100644--- a/instrumentation.ts+++ b/instrumentation.ts@@ -1,9 +1,19 @@import { registerOTel } from '@vercel/otel'import { LangfuseExporter } from 'langfuse-vercel'-export function register() {+export async function register() {registerOTel({serviceName: 'morphic-ai-search',traceExporter: new LangfuseExporter()})++ // Initialize Ollama validation on server startup (Node.js only)+ if (process.env.NEXT_RUNTIME === 'nodejs') {+ const { initializeOllamaValidation } = await import(+ '@/lib/config/ollama-validator'+ )+ await initializeOllamaValidation().catch(err => {+ console.error('Failed to initialize Ollama validation:', err)+ })+ }}diff --git a/lib/config/ollama-validator.ts b/lib/config/ollama-validator.tsnew file mode 100644index 00000000..a90d6a62--- /dev/null+++ b/lib/config/ollama-validator.ts@@ -0,0 +1,114 @@+import { OllamaClient } from '@/lib/ollama/client'++/**+ * Memory cache for validated Ollama models+ * Stored in server memory (only suitable for local deployments)+ */+let validatedModels: Set<string> | null = null+let validationError: Error | null = null++/**+ * Initialize Ollama model validation on server startup+ * Checks which models support 'tools' capability required for Morphic+ */+export async function initializeOllamaValidation(): Promise<void> {+ // Skip validation if OLLAMA_BASE_URL is not configured+ if (!process.env.OLLAMA_BASE_URL) {+ console.log('Ollama validation skipped (OLLAMA_BASE_URL not configured)')+ return+ }++ try {+ console.log(+ `Starting Ollama model validation at ${process.env.OLLAMA_BASE_URL}`+ )++ const client = new OllamaClient(process.env.OLLAMA_BASE_URL)++ // Check if Ollama is available+ const isAvailable = await client.isAvailable()+ if (!isAvailable) {+ console.warn(+ 'Ollama instance is not available. Models will not be validated.'+ )+ return+ }++ // Get all available models+ const models = await client.getModels()+ console.log(`Found ${models.length} Ollama models`)++ // Validate each model for tools capability+ const validated = new Set<string>()+ for (const model of models) {+ try {+ const capabilities = await client.getModelCapabilities(model.name)+ if (capabilities.capabilities.includes('tools')) {+ validated.add(model.name)+ console.log(`✓ ${model.name} supports tools`)+ } else {+ console.log(`✗ ${model.name} does not support tools (skipped)`)+ }+ } catch (err) {+ console.warn(`Failed to check capabilities for ${model.name}:`, err)+ continue+ }+ }++ validatedModels = validated+ console.log(+ `Ollama validation complete: ${validated.size} models with tools support`+ )+ } catch (error) {+ validationError = error as Error+ console.error('Ollama validation failed:', error)+ console.warn('Morphic will continue, but Ollama models may not work')+ }+}++/**+ * Validate if a specific Ollama model supports tools capability+ * Returns validation result with optional error message+ */+export function validateOllamaModel(modelId: string): {+ valid: boolean+ error?: string+} {+ // If OLLAMA_BASE_URL is not configured, consider model valid (not using Ollama)+ if (!process.env.OLLAMA_BASE_URL) {+ return { valid: true }+ }++ // If validation hasn't run yet (shouldn't happen after startup), consider valid+ if (validatedModels === null && validationError === null) {+ return { valid: true }+ }++ // If validation failed, warn but allow the model to be used+ if (validationError) {+ return {+ valid: true,+ error: `Ollama validation failed: ${validationError.message}. Model may not work correctly.`+ }+ }++ // Check actual validation results+ if (!validatedModels!.has(modelId)) {+ return {+ valid: false,+ error: `Model "${modelId}" does not support tools capability required for Morphic. Please use a model with tools support.`+ }+ }++ return { valid: true }+}++/**+ * Get list of all validated Ollama models with tools support+ */+export function getValidatedOllamaModels(): string[] {+ if (!validatedModels) {+ return []+ }+ return Array.from(validatedModels)+}diff --git a/lib/ollama/transformer.ts b/lib/ollama/transformer.tsdeleted file mode 100644index 3d3016be..00000000--- a/lib/ollama/transformer.ts+++ /dev/null@@ -1,43 +0,0 @@-import { Model } from '@/lib/types/models'--import { OllamaModel, OllamaModelCapabilities } from './types'--/**- * Transform Ollama model to Morphic Model format- * Only includes models with tools capability (required for Morphic)- */-export function transformOllamaModel(- ollamaModel: OllamaModel,- capabilities?: OllamaModelCapabilities-): Model | null {- const hasTools = capabilities?.capabilities.includes('tools') || false-- if (!hasTools) {- return null- }-- return {- id: ollamaModel.name,- name: formatModelName(ollamaModel.name),- provider: 'Ollama',- providerId: 'ollama',- enabled: true,- toolCallType: 'native',- toolCallModel: ollamaModel.name,- capabilities: capabilities?.capabilities || [],- contextWindow: capabilities?.contextWindow || 128_000- }-}--/**- * Format model name for display- * Convert "llama3.2:latest" to "Llama 3.2 Latest"- */-function formatModelName(name: string): string {- return name- .replace(/[:]/g, ' ')- .replace(/([a-z])([A-Z])/g, '$1 $2')- .split(' ')- .map(word => word.charAt(0).toUpperCase() + word.slice(1))- .join(' ')-}diff --git a/lib/utils/registry.ts b/lib/utils/registry.tsindex 5aa31fe4..5eb1fa03 100644--- a/lib/utils/registry.ts+++ b/lib/utils/registry.ts@@ -3,6 +3,7 @@ import { createGateway } from '@ai-sdk/gateway'import { google } from '@ai-sdk/google'import { createOpenAI, openai } from '@ai-sdk/openai'import { createProviderRegistry, LanguageModel } from 'ai'+import { createOllama } from 'ollama-ai-provider-v2'export const registry = createProviderRegistry({openai,@@ -14,6 +15,11 @@ export const registry = createProviderRegistry({}),gateway: createGateway({apiKey: process.env.AI_GATEWAY_API_KEY+ }),+ ollama: createOllama({+ baseURL: process.env.OLLAMA_BASE_URL+ ? `${process.env.OLLAMA_BASE_URL}/api`+ : 'http://localhost:11434/api'})})@@ -38,6 +44,8 @@ export function isProviderEnabled(providerId: string): boolean {)case 'gateway':return !!process.env.AI_GATEWAY_API_KEY+ case 'ollama':+ return !!process.env.OLLAMA_BASE_URLdefault:return false}diff --git a/package.json b/package.jsonindex f267af0b..e00a1bba 100644--- a/package.json+++ b/package.json@@ -69,6 +69,7 @@"next": "^15.2.3","next-themes": "^0.3.0","node-html-parser": "^6.1.13",+ "ollama-ai-provider-v2": "^1.5.0","postgres": "^3.4.5","react": "^19.0.0","react-dom": "^19.0.0",