ollama.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package llm
  2. import (
  3. "fmt"
  4. "yunion.io/x/pkg/errors"
  5. "yunion.io/x/onecloud/pkg/apis"
  6. )
  7. type OllamaModelFileSpec struct {
  8. Parameter *OllamaModelFileParameter `json:"parameter"`
  9. Template *string `json:"template,omitempty"`
  10. System *string `json:"system,omitempty"`
  11. // Adapter *string `json:"adapter,omitempty"`
  12. License *string `json:"license,omitempty"`
  13. Message []*OllamaModelFileMessage `json:"message,omitempty"`
  14. }
  15. type OllamaGgufSpec struct {
  16. GgufFile string `json:"gguf_file"`
  17. Source string `json:"source"`
  18. ModelFile *OllamaModelFileSpec `json:"modelfile,omitempty"`
  19. }
  20. // type OllamaPullModelInput struct {
  21. // Model string `json:"model"`
  22. // Gguf *OllamaGgufSpec `json:"gguf,omitempty"`
  23. // }
  24. // type OllamaCreateInput struct {
  25. // compute.ServerCreateInput
  26. // OllamaPullModelInput
  27. // }
  28. type OllamaListInput struct {
  29. apis.VirtualResourceListInput
  30. GuestId string `json:"guest_id"`
  31. }
  32. type OllamaAccessCacheInput struct {
  33. ModelName string `json:"model_name"`
  34. Blobs []string `json:"blobs"`
  35. }
  36. type OllamaAccessGgufFileInput struct {
  37. HostPath string `json:"host_path"`
  38. TargetDir string `json:"target_dir"`
  39. }
  40. type OllamaModelFileMessage struct {
  41. Role string `json:"role"`
  42. Content string `json:"content"`
  43. }
  44. func (m *OllamaModelFileMessage) ValidateRole() error {
  45. switch m.Role {
  46. case LLM_OLLAMA_GGUF_MESSAGE_ROLE_SYSTEM,
  47. LLM_OLLAMA_GGUF_MESSAGE_ROLE_USER,
  48. LLM_OLLAMA_GGUF_MESSAGE_ROLE_ASSISTANT:
  49. return nil
  50. default:
  51. return errors.Errorf("invalid role: %s, must be one of: system, user, assistant", m.Role)
  52. }
  53. }
  54. type OllamaModelFileParameter struct {
  55. NumCtx *int `json:"num_ctx,omitempty"`
  56. RepeatLastN *int `json:"repeat_last_n,omitempty"`
  57. RepeatPenalty *float64 `json:"repeat_penalty,omitempty"`
  58. Temperature *float64 `json:"temperature,omitempty"`
  59. Seed *int `json:"seed,omitempty"`
  60. Stop *string `json:"stop,omitempty"`
  61. NumPredict *int `json:"num_predict,omitempty"`
  62. TopK *int `json:"top_k,omitempty"`
  63. TopP *float64 `json:"top_p,omitempty"`
  64. MinP *float64 `json:"min_p,omitempty"`
  65. }
  66. func (p *OllamaModelFileParameter) GetParameters() map[string]string {
  67. pairs := make(map[string]string)
  68. addInt := func(key string, val *int) {
  69. if val != nil {
  70. pairs[key] = fmt.Sprintf("%d", *val)
  71. }
  72. }
  73. addFloat := func(key string, val *float64) {
  74. if val != nil {
  75. pairs[key] = fmt.Sprintf("%f", *val)
  76. }
  77. }
  78. addString := func(key string, val *string) {
  79. if val != nil {
  80. pairs[key] = fmt.Sprintf("\"%s\"", *val)
  81. }
  82. }
  83. addInt(LLM_OLLAMA_MODELFILE_PARAMETER_NUM_CTX, p.NumCtx)
  84. addInt(LLM_OLLAMA_MODELFILE_PARAMETER_REPEAT_LAST_N, p.RepeatLastN)
  85. addFloat(LLM_OLLAMA_MODELFILE_PARAMETER_REPEAT_PENALTY, p.RepeatPenalty)
  86. addFloat(LLM_OLLAMA_MODELFILE_PARAMETER_TEMPERATURE, p.Temperature)
  87. addInt(LLM_OLLAMA_MODELFILE_PARAMETER_SEED, p.Seed)
  88. addString(LLM_OLLAMA_MODELFILE_PARAMETER_STOP, p.Stop)
  89. addInt(LLM_OLLAMA_MODELFILE_PARAMETER_NUM_PREDICT, p.NumPredict)
  90. addInt(LLM_OLLAMA_MODELFILE_PARAMETER_TOP_K, p.TopK)
  91. addFloat(LLM_OLLAMA_MODELFILE_PARAMETER_TOP_P, p.TopP)
  92. addFloat(LLM_OLLAMA_MODELFILE_PARAMETER_MIN_P, p.MinP)
  93. return pairs
  94. }