model.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/sirupsen/logrus"
  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. }
  67. func (s *composeService) newModelAPI(project *types.Project) (*modelAPI, error) {
  68. dockerModel, err := manager.GetPlugin("model", s.dockerCli, &cobra.Command{})
  69. if err != nil {
  70. if errdefs.IsNotFound(err) {
  71. return nil, fmt.Errorf("'models' support requires Docker Model plugin")
  72. }
  73. return nil, err
  74. }
  75. endpoint, cleanup, err := s.propagateDockerEndpoint()
  76. if err != nil {
  77. return nil, err
  78. }
  79. return &modelAPI{
  80. path: dockerModel.Path,
  81. prepare: func(ctx context.Context, cmd *exec.Cmd) error {
  82. return s.prepareShellOut(ctx, project.Environment, cmd)
  83. },
  84. cleanup: cleanup,
  85. env: append(project.Environment.Values(), endpoint...),
  86. }, nil
  87. }
  88. func (m *modelAPI) Close() {
  89. m.cleanup()
  90. }
  91. func (m *modelAPI) PullModel(ctx context.Context, model types.ModelConfig, quietPull bool, events api.EventProcessor) error {
  92. events.On(api.Resource{
  93. ID: model.Name,
  94. Status: api.Working,
  95. Text: "Pulling",
  96. })
  97. cmd := exec.CommandContext(ctx, m.path, "pull", model.Model)
  98. err := m.prepare(ctx, cmd)
  99. if err != nil {
  100. return err
  101. }
  102. stream, err := cmd.StdoutPipe()
  103. if err != nil {
  104. return err
  105. }
  106. err = cmd.Start()
  107. if err != nil {
  108. return err
  109. }
  110. scanner := bufio.NewScanner(stream)
  111. for scanner.Scan() {
  112. msg := scanner.Text()
  113. if msg == "" {
  114. continue
  115. }
  116. if !quietPull {
  117. events.On(api.Resource{
  118. ID: model.Name,
  119. Status: api.Working,
  120. Text: api.StatusPulling,
  121. })
  122. }
  123. }
  124. err = cmd.Wait()
  125. if err != nil {
  126. events.On(errorEvent(model.Name, err.Error()))
  127. }
  128. events.On(api.Resource{
  129. ID: model.Name,
  130. Status: api.Working,
  131. Text: api.StatusPulled,
  132. })
  133. return err
  134. }
  135. func (m *modelAPI) ConfigureModel(ctx context.Context, config types.ModelConfig, events api.EventProcessor) error {
  136. if len(config.RuntimeFlags) != 0 {
  137. logrus.Warnf("Runtime flags are not supported and will be ignored for model %s", config.Model)
  138. config.RuntimeFlags = nil
  139. }
  140. events.On(api.Resource{
  141. ID: config.Name,
  142. Status: api.Working,
  143. Text: "Configuring",
  144. })
  145. // configure [--context-size=<n>] MODEL
  146. args := []string{"configure"}
  147. if config.ContextSize > 0 {
  148. args = append(args, "--context-size", strconv.Itoa(config.ContextSize))
  149. }
  150. args = append(args, config.Model)
  151. cmd := exec.CommandContext(ctx, m.path, args...)
  152. err := m.prepare(ctx, cmd)
  153. if err != nil {
  154. return err
  155. }
  156. return cmd.Run()
  157. }
  158. func (m *modelAPI) SetModelVariables(ctx context.Context, project *types.Project) error {
  159. cmd := exec.CommandContext(ctx, m.path, "status", "--json")
  160. err := m.prepare(ctx, cmd)
  161. if err != nil {
  162. return err
  163. }
  164. statusOut, err := cmd.CombinedOutput()
  165. if err != nil {
  166. return fmt.Errorf("error checking docker-model status: %w", err)
  167. }
  168. type Status struct {
  169. Endpoint string `json:"endpoint"`
  170. }
  171. var status Status
  172. err = json.Unmarshal(statusOut, &status)
  173. if err != nil {
  174. return err
  175. }
  176. for _, service := range project.Services {
  177. for ref, modelConfig := range service.Models {
  178. model := project.Models[ref]
  179. varPrefix := strings.ReplaceAll(strings.ToUpper(ref), "-", "_")
  180. var variable string
  181. if modelConfig != nil && modelConfig.ModelVariable != "" {
  182. variable = modelConfig.ModelVariable
  183. } else {
  184. variable = varPrefix + "_MODEL"
  185. }
  186. service.Environment[variable] = &model.Model
  187. if modelConfig != nil && modelConfig.EndpointVariable != "" {
  188. variable = modelConfig.EndpointVariable
  189. } else {
  190. variable = varPrefix + "_URL"
  191. }
  192. service.Environment[variable] = &status.Endpoint
  193. }
  194. }
  195. return nil
  196. }
  197. type Model struct {
  198. Id string `json:"id"`
  199. Tags []string `json:"tags"`
  200. Created int `json:"created"`
  201. Config struct {
  202. Format string `json:"format"`
  203. Quantization string `json:"quantization"`
  204. Parameters string `json:"parameters"`
  205. Architecture string `json:"architecture"`
  206. Size string `json:"size"`
  207. } `json:"config"`
  208. }
  209. func (m *modelAPI) ListModels(ctx context.Context) ([]string, error) {
  210. cmd := exec.CommandContext(ctx, m.path, "ls", "--json")
  211. err := m.prepare(ctx, cmd)
  212. if err != nil {
  213. return nil, err
  214. }
  215. output, err := cmd.CombinedOutput()
  216. if err != nil {
  217. return nil, fmt.Errorf("error checking available models: %w", err)
  218. }
  219. type AvailableModel struct {
  220. Id string `json:"id"`
  221. Tags []string `json:"tags"`
  222. Created int `json:"created"`
  223. }
  224. models := []AvailableModel{}
  225. err = json.Unmarshal(output, &models)
  226. if err != nil {
  227. return nil, fmt.Errorf("error unmarshalling available models: %w", err)
  228. }
  229. var availableModels []string
  230. for _, model := range models {
  231. availableModels = append(availableModels, model.Tags...)
  232. }
  233. return availableModels, nil
  234. }