Skip to content

An AI-powered search engine with a generative UI

refactor: improve model configuration management

Yoshiki Miurababdb0844e10
diff --git a/README.md b/README.md
index e1f55c02..56a1e80d 100644
--- a/README.md
+++ b/README.md
@@ -150,6 +150,29 @@ Host your own live version of Morphic with Vercel, Cloudflare Pages, or Docker.
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fmiurla%2Fmorphic&env=OPENAI_API_KEY,TAVILY_API_KEY,UPSTASH_REDIS_REST_URL,UPSTASH_REDIS_REST_TOKEN)
+### Docker Prebuilt Image
+
+Prebuilt Docker images are available on GitHub Container Registry:
+
+```bash
+docker pull ghcr.io/miurla/morphic:latest
+```
+
+You can use it with docker-compose:
+
+```yaml
+services:
+ morphic:
+ image: ghcr.io/miurla/morphic:latest
+ env_file: .env.local
+ ports:
+ - '3000:3000'
+ volumes:
+ - ./models.json:/app/public/config/models.json # Optional: Override default model configuration
+```
+
+The default model configuration is located at `public/config/models.json`. For Docker deployment, you can create `models.json` alongside `.env.local` to override the default configuration.
+
## 🔎 Search Engine
### Setting up the Search Engine in Your Browser
diff --git a/app/page.tsx b/app/page.tsx
index b2a50c95..fd8d62cb 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -1,9 +1,9 @@
import { Chat } from '@/components/chat'
-import modelsList from '@/lib/config/models.json'
-import { Model } from '@/lib/types/models'
+import { getModels } from '@/lib/config/models'
import { generateId } from 'ai'
export default function Page() {
const id = generateId()
- return <Chat id={id} models={modelsList.models as Model[]} />
+ const models = getModels()
+ return <Chat id={id} models={models} />
}
diff --git a/app/search/page.tsx b/app/search/page.tsx
index 9cef30d9..47d1405a 100644
--- a/app/search/page.tsx
+++ b/app/search/page.tsx
@@ -1,4 +1,5 @@
import { Chat } from '@/components/chat'
+import { getModels } from '@/lib/config/models'
import { generateId } from 'ai'
import { redirect } from 'next/navigation'
@@ -13,5 +14,6 @@ export default async function SearchPage(props: {
}
const id = generateId()
- return <Chat id={id} query={q} />
+ const models = getModels()
+ return <Chat id={id} query={q} models={models} />
}
diff --git a/app/share/[id]/page.tsx b/app/share/[id]/page.tsx
index bd25a476..a940f3a4 100644
--- a/app/share/[id]/page.tsx
+++ b/app/share/[id]/page.tsx
@@ -1,7 +1,6 @@
import { Chat } from '@/components/chat'
import { getSharedChat } from '@/lib/actions/chat'
-import modelsList from '@/lib/config/models.json'
-import { Model } from '@/lib/types/models'
+import { getModels } from '@/lib/config/models'
import { convertToUIMessages } from '@/lib/utils'
import { notFound } from 'next/navigation'
@@ -25,18 +24,17 @@ export default async function SharePage(props: {
}) {
const { id } = await props.params
const chat = await getSharedChat(id)
- // convertToUIMessages for useChat hook
- const messages = convertToUIMessages(chat?.messages || [])
if (!chat || !chat.sharePath) {
- notFound()
+ return notFound()
}
+ const models = getModels()
return (
<Chat
- id={id}
- savedMessages={messages}
- models={modelsList.models as Model[]}
+ id={chat.id}
+ savedMessages={convertToUIMessages(chat.messages)}
+ models={models}
/>
)
}
diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md
index 97d2c467..97636a9e 100644
--- a/docs/CONFIGURATION.md
+++ b/docs/CONFIGURATION.md
@@ -121,7 +121,7 @@ docker-compose logs searxng
## Additional AI Providers
-Models are configured in `lib/config/models.json`. Each model requires its corresponding API key to be set in the environment variables.
+Models are configured in `public/config/models.json`. Each model requires its corresponding API key to be set in the environment variables.
### Model Configuration
diff --git a/lib/config/models.ts b/lib/config/models.ts
new file mode 100644
index 00000000..ab826c2f
--- /dev/null
+++ b/lib/config/models.ts
@@ -0,0 +1,31 @@
+import { Model } from '@/lib/types/models'
+import { existsSync, readFileSync } from 'fs'
+import { join } from 'path'
+
+export function validateModel(model: any): model is Model {
+ return (
+ typeof model.id === 'string' &&
+ typeof model.name === 'string' &&
+ typeof model.provider === 'string' &&
+ typeof model.providerId === 'string' &&
+ typeof model.enabled === 'boolean' &&
+ (model.toolCallType === 'native' || model.toolCallType === 'manual') &&
+ (model.toolCallModel === undefined || typeof model.toolCallModel === 'string')
+ )
+}
+
+export function getModels(): Model[] {
+ const configPath = join(process.cwd(), 'public', 'config', 'models.json')
+
+ try {
+ const config = JSON.parse(readFileSync(configPath, 'utf-8'))
+ if (Array.isArray(config.models) && config.models.every(validateModel)) {
+ return config.models
+ }
+ console.warn('Invalid model configuration')
+ } catch (error) {
+ console.warn('Failed to load models:', error)
+ }
+
+ return []
+}
diff --git a/lib/config/models.json b/public/config/models.json
similarity index 100%
rename from lib/config/models.json
rename to public/config/models.json