Skip to content
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

[Release] Support for Ollama inside Docker #101

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/src/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"fmt"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -75,3 +76,8 @@ func JoinWithQuotes(arr []string) string {

return strings.Join(quotedStrings, ", ")
}

func IsRunningInsideDocker() bool {
_, err := os.Stat("/.dockerenv")
return !os.IsNotExist(err)
}
2 changes: 2 additions & 0 deletions core/src/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

var IsDevelopment = os.Getenv("ENVIRONMENT") == "dev"
var OllamaHost = os.Getenv("WHODB_OLLAMA_HOST")
var OllamaPort = os.Getenv("WHODB_OLLAMA_PORT")

type DatabaseCredentials struct {
Hostname string `json:"host"`
Expand Down
27 changes: 23 additions & 4 deletions core/src/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"io"
"net/http"
"strings"

"github.com/clidey/whodb/core/src/common"
"github.com/clidey/whodb/core/src/env"
)

type completionRequest struct {
Expand All @@ -23,8 +26,6 @@ type completionResponse struct {
Done bool `json:"done"`
}

const ollamaLocalEndpoint = "http://localhost:11434/api"

type LLMType string

const (
Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *LLMClient) Complete(prompt string, model LLMModel, receiverChan *chan s
var url string
switch c.Type {
case Ollama_LLMType:
url = fmt.Sprintf("%v/generate", ollamaLocalEndpoint)
url = fmt.Sprintf("%v/generate", getOllamaEndpoint())
}

resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
Expand Down Expand Up @@ -96,7 +97,7 @@ func (c *LLMClient) GetSupportedModels() ([]string, error) {
var url string
switch c.Type {
case Ollama_LLMType:
url = fmt.Sprintf("%v/tags", ollamaLocalEndpoint)
url = fmt.Sprintf("%v/tags", getOllamaEndpoint())
}

resp, err := http.Get(url)
Expand Down Expand Up @@ -143,3 +144,21 @@ func Instance(llmType LLMType) *LLMClient {
llmInstance[llmType] = instance
return instance
}

func getOllamaEndpoint() string {
host := "localhost"
port := "11434"

if common.IsRunningInsideDocker() {
host = "host.docker.internal"
}

if env.OllamaHost != "" {
host = env.OllamaHost
}
if env.OllamaPort != "" {
port = env.OllamaPort
}

return fmt.Sprintf("http://%v:%v/api", host, port)
}
3 changes: 2 additions & 1 deletion core/src/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/clidey/whodb/core/graph"
"github.com/clidey/whodb/core/src/auth"
"github.com/clidey/whodb/core/src/common"
"github.com/clidey/whodb/core/src/log"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
Expand Down Expand Up @@ -69,7 +70,7 @@ func InitializeRouter(staticFiles embed.FS) {
log.Logger.Infof("http://0.0.0.0:%s", port)
log.Logger.Info("Explore and enjoy working with your databases!")

if !isDocker() {
if !common.IsRunningInsideDocker() {
openBrowser(fmt.Sprintf("http://localhost:%v", port))
}

Expand Down
6 changes: 0 additions & 6 deletions core/src/router/task.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package router

import (
"os"
"os/exec"
"runtime"

Expand All @@ -24,8 +23,3 @@ func openBrowser(url string) {
log.Logger.Warnf("Failed to open browser: %v\n", err)
}
}

func isDocker() bool {
_, err := os.Stat("/.dockerenv")
return !os.IsNotExist(err)
}