llm.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package llm
  2. import (
  3. "yunion.io/x/jsonutils"
  4. "yunion.io/x/pkg/errors"
  5. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  6. api "yunion.io/x/onecloud/pkg/apis/llm"
  7. "yunion.io/x/onecloud/pkg/cloudcommon/cmdline"
  8. "yunion.io/x/onecloud/pkg/mcclient/options"
  9. )
  10. type LLMBaseListOptions struct {
  11. options.BaseListOptions
  12. Host string `help:"filter by host"`
  13. LLMStatus []string `help:"filter by server status"`
  14. NetworkType string `help:"filter by network type"`
  15. NetworkId string `help:"filter by network id"`
  16. ListenPort int `help:"filter by listen port"`
  17. PublicIp string `help:"filter by public ip"`
  18. VolumeId string `help:"filter by volume id"`
  19. Unused *bool `help:"filter by unused"`
  20. Used *bool `help:"filter by used"`
  21. }
  22. type LLMListOptions struct {
  23. LLMBaseListOptions
  24. LlmSku string `help:"filter by llm sku"`
  25. LlmImage string `help:"filter by llm image"`
  26. LLMTypes []string `help:"filter by llm types"`
  27. }
  28. func (o *LLMListOptions) Params() (jsonutils.JSONObject, error) {
  29. params, err := options.ListStructToParams(o)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if o.Used != nil {
  34. params.Set("unused", jsonutils.JSONFalse)
  35. }
  36. return params, nil
  37. }
  38. type LLMShowOptions struct {
  39. options.BaseShowOptions
  40. }
  41. func (o *LLMShowOptions) Params() (jsonutils.JSONObject, error) {
  42. return options.StructToParams(o)
  43. }
  44. type LLMBaseCreateOptions struct {
  45. options.BaseCreateOptions
  46. AutoStart bool
  47. ProjectId string
  48. PreferHost string
  49. Net []string `help:"Network descriptions"`
  50. BandwidthMb int
  51. Count int `default:"1" help:"batch create count" json:"-"`
  52. }
  53. type LLMCreateOptions struct {
  54. LLMBaseCreateOptions
  55. LLM_SKU_ID string `help:"llm sku id or name" json:"llm_sku_id"`
  56. PreferredModel string `help:"vLLM preferred model dir name under models path (e.g. Qwen/Qwen2-7B)" json:"-"`
  57. VllmArg []string `help:"vLLM args in format key=value; use key= for flags without values" json:"-"`
  58. }
  59. func (o *LLMCreateOptions) Params() (jsonutils.JSONObject, error) {
  60. params := jsonutils.Marshal(o).(*jsonutils.JSONDict)
  61. if len(o.Net) > 0 {
  62. nets := make([]*computeapi.NetworkConfig, 0)
  63. for i, n := range o.Net {
  64. net, err := cmdline.ParseNetworkConfig(n, i)
  65. if err != nil {
  66. return nil, errors.Wrapf(err, "parse network config %s", n)
  67. }
  68. nets = append(nets, net)
  69. }
  70. params.Add(jsonutils.Marshal(nets), "nets")
  71. }
  72. vllmSpec, err := newVLLMSpecFromArgs(o.PreferredModel, o.VllmArg)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if vllmSpec != nil {
  77. spec := &api.LLMSpec{Ollama: nil, Vllm: vllmSpec, Dify: nil}
  78. params.Set("llm_spec", jsonutils.Marshal(spec))
  79. }
  80. return params, nil
  81. }
  82. func (o *LLMCreateOptions) GetCountParam() int {
  83. return o.Count
  84. }
  85. type LLMUpdateOptions struct {
  86. options.BaseIdOptions
  87. PreferredModel string `help:"vLLM preferred model dir name under models path (e.g. Qwen/Qwen2-7B); takes effect after pod recreate" json:"-"`
  88. VllmArg []string `help:"vLLM args in format key=value; use key= for flags without values" json:"-"`
  89. }
  90. func (o *LLMUpdateOptions) GetId() string {
  91. return o.ID
  92. }
  93. func (o *LLMUpdateOptions) Params() (jsonutils.JSONObject, error) {
  94. dict, err := options.StructToParams(o)
  95. if err != nil {
  96. return nil, err
  97. }
  98. vllmSpec, err := newVLLMSpecFromArgs(o.PreferredModel, o.VllmArg)
  99. if err != nil {
  100. return nil, err
  101. }
  102. if vllmSpec != nil {
  103. spec := &api.LLMSpec{Ollama: nil, Vllm: vllmSpec, Dify: nil}
  104. dict.Set("llm_spec", jsonutils.Marshal(spec))
  105. }
  106. return dict, nil
  107. }
  108. type LLMDeleteOptions struct {
  109. options.BaseIdOptions
  110. }
  111. func (o *LLMDeleteOptions) GetId() string {
  112. return o.ID
  113. }
  114. func (o *LLMDeleteOptions) Params() (jsonutils.JSONObject, error) {
  115. return options.StructToParams(o)
  116. }
  117. type LLMStartOptions struct {
  118. options.BaseIdsOptions
  119. }
  120. func (o *LLMStartOptions) Params() (jsonutils.JSONObject, error) {
  121. return jsonutils.Marshal(o), nil
  122. }
  123. type LLMRestartOptions struct {
  124. options.BaseIdsOptions
  125. }
  126. func (o *LLMRestartOptions) Params() (jsonutils.JSONObject, error) {
  127. return jsonutils.Marshal(o), nil
  128. }
  129. type LLMStopOptions struct {
  130. options.BaseIdsOptions
  131. }
  132. func (o *LLMStopOptions) Params() (jsonutils.JSONObject, error) {
  133. return jsonutils.Marshal(o), nil
  134. }
  135. type LLMIdOptions struct {
  136. ID string `help:"llm id" json:"-"`
  137. }
  138. func (opts *LLMIdOptions) GetId() string {
  139. return opts.ID
  140. }
  141. func (opts *LLMIdOptions) Params() (jsonutils.JSONObject, error) {
  142. return jsonutils.Marshal(opts), nil
  143. }
  144. type LLMAvailableNetworkOptions struct {
  145. NetworkType string `help:"network server_type filter, e.g. guest|hostlocal"`
  146. VpcId string `help:"vpc id filter"`
  147. }
  148. func (opts *LLMAvailableNetworkOptions) GetId() string {
  149. return ""
  150. }
  151. func (opts *LLMAvailableNetworkOptions) Params() (jsonutils.JSONObject, error) {
  152. return options.StructToParams(opts)
  153. }
  154. type LLMProviderModelsOptions struct {
  155. URL string `help:"provider url, e.g. http://127.0.0.1:11434 or http://127.0.0.1:8000" json:"url"`
  156. ProviderType string `help:"provider type, use openai for OpenAI-compatible endpoints such as vllm" json:"provider_type" choices:"ollama|openai"`
  157. }
  158. func (opts *LLMProviderModelsOptions) GetId() string {
  159. return ""
  160. }
  161. func (opts *LLMProviderModelsOptions) Params() (jsonutils.JSONObject, error) {
  162. return options.StructToParams(opts)
  163. }
  164. type LLMSaveInstantModelOptions struct {
  165. LLMIdOptions
  166. MODEL_ID string `help:"llm model id, e.g. 500a1f067a9f"`
  167. Name string `help:"instant app name, e.g. qwen3:8b"`
  168. AutoRestart bool
  169. }
  170. func (opts *LLMSaveInstantModelOptions) Params() (jsonutils.JSONObject, error) {
  171. input := api.LLMSaveInstantModelInput{
  172. ModelId: opts.MODEL_ID,
  173. ModelFullName: opts.Name,
  174. // AutoRestart: opts.AutoRestart,
  175. }
  176. return jsonutils.Marshal(input), nil
  177. }
  178. type LLMQuickModelsOptions struct {
  179. LLMIdOptions
  180. MODEL []string `help:"model id of instant model, e.g. qwen3:0.6b-251202 or 7f72b5a1-4049-43db-8e91-8dee736ae1ac"`
  181. Method string `help:"install or uninstall" choices:"install|uninstall"`
  182. }
  183. func (opts *LLMQuickModelsOptions) Params() (jsonutils.JSONObject, error) {
  184. params := api.LLMPerformQuickModelsInput{}
  185. for _, mdl := range opts.MODEL {
  186. params.Models = append(params.Models, api.ModelInfo{
  187. Id: mdl,
  188. })
  189. }
  190. if len(opts.Method) > 0 {
  191. params.Method = api.TQuickModelMethod(opts.Method)
  192. }
  193. return jsonutils.Marshal(params), nil
  194. }