model.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "bufio"
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "os/exec"
  20. "slices"
  21. "strconv"
  22. "strings"
  23. "github.com/compose-spec/compose-go/v2/types"
  24. "github.com/containerd/errdefs"
  25. "github.com/docker/cli/cli-plugins/manager"
  26. "github.com/docker/docker/api/types/versions"
  27. "github.com/spf13/cobra"
  28. "golang.org/x/sync/errgroup"
  29. "github.com/docker/compose/v5/pkg/api"
  30. )
  31. func (s *composeService) ensureModels(ctx context.Context, project *types.Project, quietPull bool) error {
  32. if len(project.Models) == 0 {
  33. return nil
  34. }
  35. mdlAPI, err := s.newModelAPI(project)
  36. if err != nil {
  37. return err
  38. }
  39. defer mdlAPI.Close()
  40. availableModels, err := mdlAPI.ListModels(ctx)
  41. eg, ctx := errgroup.WithContext(ctx)
  42. eg.Go(func() error {
  43. return mdlAPI.SetModelVariables(ctx, project)
  44. })
  45. for name, config := range project.Models {
  46. if config.Name == "" {
  47. config.Name = name
  48. }
  49. eg.Go(func() error {
  50. if !slices.Contains(availableModels, config.Model) {
  51. err = mdlAPI.PullModel(ctx, config, quietPull, s.events)
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. return mdlAPI.ConfigureModel(ctx, config, s.events)
  57. })
  58. }
  59. return eg.Wait()
  60. }
  61. type modelAPI struct {
  62. path string
  63. env []string
  64. prepare func(ctx context.Context, cmd *exec.Cmd) error
  65. cleanup func()
  66. version string
  67. }
  68. func (s *composeService) newModelAPI(project *types.Project) (*modelAPI, error) {
  69. dockerModel, err := manager.GetPlugin("model", s.dockerCli, &cobra.Command{})
  70. if err != nil {
  71. if errdefs.IsNotFound(err) {
  72. return nil, fmt.Errorf("'models' support requires Docker Model plugin")
  73. }
  74. return nil, err
  75. }
  76. if dockerModel.Err != nil {
  77. return nil, fmt.Errorf("failed to load Docker Model plugin: %w", dockerModel.Err)
  78. }
  79. endpoint, cleanup, err := s.propagateDockerEndpoint()
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &modelAPI{
  84. path: dockerModel.Path,
  85. version: dockerModel.Version,
  86. prepare: func(ctx context.Context, cmd *exec.Cmd) error {
  87. return s.prepareShellOut(ctx, project.Environment, cmd)
  88. },
  89. cleanup: cleanup,
  90. env: append(project.Environment.Values(), endpoint...),
  91. }, nil
  92. }
  93. func (m *modelAPI) Close() {
  94. m.cleanup()
  95. }
  96. func (m *modelAPI) PullModel(ctx context.Context, model types.ModelConfig, quietPull bool, events api.EventProcessor) error {
  97. events.On(api.Resource{
  98. ID: model.Name,
  99. Status: api.Working,
  100. Text: api.StatusPulling,
  101. })
  102. cmd := exec.CommandContext(ctx, m.path, "pull", model.Model)
  103. err := m.prepare(ctx, cmd)
  104. if err != nil {
  105. return err
  106. }
  107. stream, err := cmd.StdoutPipe()
  108. if err != nil {
  109. return err
  110. }
  111. err = cmd.Start()
  112. if err != nil {
  113. return err
  114. }
  115. scanner := bufio.NewScanner(stream)
  116. for scanner.Scan() {
  117. msg := scanner.Text()
  118. if msg == "" {
  119. continue
  120. }
  121. if !quietPull {
  122. events.On(api.Resource{
  123. ID: model.Name,
  124. Status: api.Working,
  125. Text: api.StatusPulling,
  126. })
  127. }
  128. }
  129. err = cmd.Wait()
  130. if err != nil {
  131. events.On(errorEvent(model.Name, err.Error()))
  132. }
  133. events.On(api.Resource{
  134. ID: model.Name,
  135. Status: api.Working,
  136. Text: api.StatusPulled,
  137. })
  138. return err
  139. }
  140. func (m *modelAPI) ConfigureModel(ctx context.Context, config types.ModelConfig, events api.EventProcessor) error {
  141. events.On(api.Resource{
  142. ID: config.Name,
  143. Status: api.Working,
  144. Text: api.StatusConfiguring,
  145. })
  146. // configure [--context-size=<n>] MODEL [-- <runtime-flags...>]
  147. args := []string{"configure"}
  148. if config.ContextSize > 0 {
  149. args = append(args, "--context-size", strconv.Itoa(config.ContextSize))
  150. }
  151. args = append(args, config.Model)
  152. // Only append RuntimeFlags if docker model CLI version is >= v1.0.6
  153. if len(config.RuntimeFlags) != 0 && m.supportsRuntimeFlags() {
  154. args = append(args, "--")
  155. args = append(args, config.RuntimeFlags...)
  156. }
  157. cmd := exec.CommandContext(ctx, m.path, args...)
  158. err := m.prepare(ctx, cmd)
  159. if err != nil {
  160. return err
  161. }
  162. err = cmd.Run()
  163. if err != nil {
  164. events.On(errorEvent(config.Name, err.Error()))
  165. return err
  166. }
  167. events.On(api.Resource{
  168. ID: config.Name,
  169. Status: api.Done,
  170. Text: api.StatusConfigured,
  171. })
  172. return nil
  173. }
  174. func (m *modelAPI) SetModelVariables(ctx context.Context, project *types.Project) error {
  175. cmd := exec.CommandContext(ctx, m.path, "status", "--json")
  176. err := m.prepare(ctx, cmd)
  177. if err != nil {
  178. return err
  179. }
  180. statusOut, err := cmd.CombinedOutput()
  181. if err != nil {
  182. return fmt.Errorf("error checking docker-model status: %w", err)
  183. }
  184. type Status struct {
  185. Endpoint string `json:"endpoint"`
  186. }
  187. var status Status
  188. err = json.Unmarshal(statusOut, &status)
  189. if err != nil {
  190. return err
  191. }
  192. for _, service := range project.Services {
  193. for ref, modelConfig := range service.Models {
  194. model := project.Models[ref]
  195. varPrefix := strings.ReplaceAll(strings.ToUpper(ref), "-", "_")
  196. var variable string
  197. if modelConfig != nil && modelConfig.ModelVariable != "" {
  198. variable = modelConfig.ModelVariable
  199. } else {
  200. variable = varPrefix + "_MODEL"
  201. }
  202. service.Environment[variable] = &model.Model
  203. if modelConfig != nil && modelConfig.EndpointVariable != "" {
  204. variable = modelConfig.EndpointVariable
  205. } else {
  206. variable = varPrefix + "_URL"
  207. }
  208. service.Environment[variable] = &status.Endpoint
  209. }
  210. }
  211. return nil
  212. }
  213. type Model struct {
  214. Id string `json:"id"`
  215. Tags []string `json:"tags"`
  216. Created int `json:"created"`
  217. Config struct {
  218. Format string `json:"format"`
  219. Quantization string `json:"quantization"`
  220. Parameters string `json:"parameters"`
  221. Architecture string `json:"architecture"`
  222. Size string `json:"size"`
  223. } `json:"config"`
  224. }
  225. func (m *modelAPI) ListModels(ctx context.Context) ([]string, error) {
  226. cmd := exec.CommandContext(ctx, m.path, "ls", "--json")
  227. err := m.prepare(ctx, cmd)
  228. if err != nil {
  229. return nil, err
  230. }
  231. output, err := cmd.CombinedOutput()
  232. if err != nil {
  233. return nil, fmt.Errorf("error checking available models: %w", err)
  234. }
  235. type AvailableModel struct {
  236. Id string `json:"id"`
  237. Tags []string `json:"tags"`
  238. Created int `json:"created"`
  239. }
  240. models := []AvailableModel{}
  241. err = json.Unmarshal(output, &models)
  242. if err != nil {
  243. return nil, fmt.Errorf("error unmarshalling available models: %w", err)
  244. }
  245. var availableModels []string
  246. for _, model := range models {
  247. availableModels = append(availableModels, model.Tags...)
  248. }
  249. return availableModels, nil
  250. }
  251. // supportsRuntimeFlags checks if the docker model version supports runtime flags
  252. // Runtime flags are supported in version >= v1.0.6
  253. func (m *modelAPI) supportsRuntimeFlags() bool {
  254. // If version is not cached, don't append runtime flags to be safe
  255. if m.version == "" {
  256. return false
  257. }
  258. // Strip 'v' prefix if present (e.g., "v1.0.6" -> "1.0.6")
  259. versionStr := strings.TrimPrefix(m.version, "v")
  260. return !versions.LessThan(versionStr, "1.0.6")
  261. }