-
Notifications
You must be signed in to change notification settings - Fork 57
fix: add deterministic mock AI provider for local/demo use #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4541224
454bf7d
0f58ba1
77f42b5
f03518a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ PORT=3000 | |
| NODE_ENV=development | ||
|
|
||
| # AI Service Configuration | ||
| # AI provider selection: "openrouter" (default) or "ollama" | ||
| # AI provider selection: "openrouter" (default), "ollama", or "mock" (local/demo only) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a developer follows this new Useful? React with 👍 / 👎. |
||
| AI_PROVIDER=openrouter | ||
|
|
||
| # OpenRouter Configuration (when AI_PROVIDER=openrouter) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,13 @@ services: | |
| - METRICS_ENABLED=${METRICS_ENABLED:-true} | ||
| - METRICS_PATH=${METRICS_PATH:-/metrics} | ||
| # Pass through from .env file | ||
| - AI_PROVIDER | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a user follows the Docker Compose quick start and sets Useful? React with 👍 / 👎. |
||
| - OLLAMA_URL=${OLLAMA_URL:-http://host.docker.internal:11434} | ||
| - OLLAMA_MODEL | ||
| - OPENROUTER_API_KEY | ||
| - OPENROUTER_MODEL | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This Compose env list now exposes the provider knobs, but it still omits Useful? React with 👍 / 👎. |
||
| - OPENROUTER_URL | ||
| - RUNNING_IN_DOCKER=true | ||
| - SERVER_WALLET_PRIVATE_KEY | ||
| - RECIPIENT_ADDRESS | ||
| - PAYMENT_AMOUNT | ||
|
|
@@ -37,6 +42,8 @@ services: | |
| start_period: 10s | ||
| networks: | ||
| - microai-net | ||
| extra_hosts: | ||
| - "host.docker.internal:host-gateway" | ||
|
|
||
| verifier: | ||
| build: ./verifier | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,7 @@ The verifier route `POST /verify` is not a gateway route. It belongs to the inte | |
| | `ratelimit.go` | Token bucket implementation. | | ||
| | `metrics.go` | Prometheus metric definitions and metrics endpoint config helpers. | | ||
| | `middleware.go` | Request timeout, correlation ID, and request metrics middleware. | | ||
| | `internal/ai/` | OpenRouter and Ollama provider implementations. | | ||
| | `internal/ai/` | OpenRouter, Ollama, and Mock provider implementations. | | ||
| | `openapi.yaml` | Public gateway API contract. | | ||
|
|
||
| ## Configuration | ||
|
|
@@ -88,7 +88,7 @@ Common optional variables: | |
| | Variable | Default | Notes | | ||
| | --- | --- | --- | | ||
| | `PORT` | `3000` | Gateway listen port. | | ||
| | `AI_PROVIDER` | `openrouter` | Supported values: `openrouter`, `ollama`. | | ||
| | `AI_PROVIDER` | `openrouter` | Supported values: `openrouter`, `ollama`, `mock` (local/demo only). | | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When readers follow the root README instead of Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Adding Useful? React with 👍 / 👎. |
||
| | `OPENROUTER_MODEL` | `z-ai/glm-4.5-air:free` in code/docs unless overridden | OpenRouter model. | | ||
| | `OPENROUTER_URL` | `https://openrouter.ai/api/v1/chat/completions` provider default | Used by tests and custom OpenRouter-compatible endpoints. | | ||
| | `OLLAMA_URL` | `http://localhost:11434` | Used when `AI_PROVIDER=ollama`. | | ||
|
|
@@ -129,7 +129,7 @@ cd gateway | |
| RECEIPT_STORE=memory CACHE_ENABLED=false go run . | ||
| ``` | ||
|
|
||
| The verifier must be reachable at `VERIFIER_URL` for signed requests. OpenRouter startup requires `OPENROUTER_API_KEY` unless `AI_PROVIDER=ollama`. | ||
| The verifier must be reachable at `VERIFIER_URL` for signed requests. OpenRouter startup requires `OPENROUTER_API_KEY` unless `AI_PROVIDER` is set to `ollama` or `mock`. | ||
|
|
||
| ## Testing | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package ai | ||
|
|
||
| import "context" | ||
|
|
||
| type MockProvider struct { | ||
| Name string | ||
| } | ||
|
|
||
| func NewMockProvider() *MockProvider { | ||
| return &MockProvider{Name: "Mock"} | ||
| } | ||
|
|
||
| func (m *MockProvider) Generate(ctx context.Context, text string) (string, error) { | ||
| if err := ctx.Err(); err != nil { | ||
| return "", err | ||
| } | ||
| return "This is a deterministic mock summary of the input text for local/demo testing.", nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Provider defines the interface for AI service providers | ||
|
|
@@ -25,7 +26,37 @@ func NewProvider() (Provider, error) { | |
| return NewOpenRouterProvider(), nil | ||
| case "ollama": | ||
| return NewOllamaProvider(), nil | ||
| case "mock": | ||
| if err := IsMockAllowed(os.Getenv("NODE_ENV"), os.Getenv("APP_ENV"), os.Getenv("ALLOW_MOCK_PROVIDER")); err != nil { | ||
| return nil, err | ||
| } | ||
| return NewMockProvider(), nil | ||
|
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because Useful? React with 👍 / 👎.
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because the mock provider is documented as local/demo-only but this branch accepts Useful? React with 👍 / 👎. |
||
| default: | ||
| return nil, fmt.Errorf("unsupported AI provider: %s", providerType) | ||
| } | ||
| } | ||
|
|
||
| // IsMockAllowed evaluates NODE_ENV, APP_ENV, and ALLOW_MOCK_PROVIDER to see if the mock provider can be run. | ||
| // It normalizes checks by lowercasing and trimming spaces, evaluating production prefixes, | ||
| // and treating truthy values for ALLOW_MOCK_PROVIDER. | ||
| func IsMockAllowed(nodeEnv, appEnv, allowMock string) error { | ||
| nodeEnv = strings.ToLower(strings.TrimSpace(nodeEnv)) | ||
| appEnv = strings.ToLower(strings.TrimSpace(appEnv)) | ||
| allowMock = strings.ToLower(strings.TrimSpace(allowMock)) | ||
|
|
||
| isProd := strings.HasPrefix(nodeEnv, "prod") || strings.HasPrefix(appEnv, "prod") | ||
| if isProd { | ||
| return fmt.Errorf("mock AI provider is disabled in production environments") | ||
| } | ||
|
|
||
| isAllowedEnv := nodeEnv == "development" || nodeEnv == "local" || nodeEnv == "demo" || nodeEnv == "test" || nodeEnv == "dev" || | ||
| appEnv == "development" || appEnv == "local" || appEnv == "demo" || appEnv == "test" || appEnv == "dev" | ||
|
|
||
| isExplicitlyAllowed := allowMock == "true" || allowMock == "1" || allowMock == "yes" | ||
|
|
||
| if !isAllowedEnv && !isExplicitlyAllowed { | ||
| return fmt.Errorf("mock AI provider is disabled. Enable it by setting ALLOW_MOCK_PROVIDER=true, or set NODE_ENV/APP_ENV to development/local/demo/test") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a local/demo user copies this env template, sets
AI_PROVIDER=mock, and starts the stack with Docker Compose, the gateway container still will not see the setting becausedocker-compose.ymlonly lists the variables that get placed in the container environment and omitsAI_PROVIDER; Docker’s Compose docs describe theenvironmentattribute as what sets container environment variables. In that scenario the gateway falls back toopenrouterand fails startup withoutOPENROUTER_API_KEY, so please addAI_PROVIDER(and any provider-specific mock/ollama variables as needed) to the gateway service environment.Useful? React with 👍 / 👎.