Skip to content

Commit

Permalink
feat: add server package with version and models endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
presbrey committed Sep 3, 2024
1 parent 92203d2 commit 2b3a28c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/ollamafarmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"log"
"net/http"

"github.com/presbrey/ollamafarm"
"github.com/presbrey/ollamafarm/server"
)

func main() {
farm := ollamafarm.New()

// Register your Ollama clients here
// For example:
// farm.RegisterURL("http://localhost:11434", nil)

s := server.NewServer(farm)

http.HandleFunc("/version", s.VersionHandler)
http.HandleFunc("/models", s.ModelsHandler)

log.Println("Server starting on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
22 changes: 22 additions & 0 deletions farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,25 @@ func (f *Farm) ModelCounts(where *Where) map[string]uint {

return modelCounts
}

// AllModels returns a list of all unique models available across all registered Ollamas.
func (f *Farm) AllModels() []string {
f.mu.RLock()
defer f.mu.RUnlock()

modelSet := make(map[string]struct{})
for _, ollama := range f.ollamas {
if !ollama.properties.Offline {
for model := range ollama.models {
modelSet[model] = struct{}{}
}
}
}

models := make([]string, 0, len(modelSet))
for model := range modelSet {
models = append(models, model)
}

return models
}
41 changes: 41 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package server

import (
"encoding/json"
"net/http"

"github.com/presbrey/ollamafarm"
)

type Server struct {
Farm *ollamafarm.Farm
}

func NewServer(farm *ollamafarm.Farm) *Server {
return &Server{Farm: farm}
}

func (s *Server) VersionHandler(w http.ResponseWriter, r *http.Request) {
ollama := s.Farm.First(nil)
if ollama == nil {
http.Error(w, "No available Ollama instances", http.StatusServiceUnavailable)
return
}

ctx := r.Context()
version, err := ollama.Client().Version(ctx)
if err != nil {
http.Error(w, "Failed to get version", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"version": version})
}

func (s *Server) ModelsHandler(w http.ResponseWriter, r *http.Request) {
models := s.Farm.AllModels()

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(models)
}

0 comments on commit 2b3a28c

Please sign in to comment.