types.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math"
  6. "os"
  7. "runtime"
  8. "time"
  9. )
  10. type StatusError struct {
  11. StatusCode int
  12. Status string
  13. ErrorMessage string `json:"error"`
  14. }
  15. func (e StatusError) Error() string {
  16. switch {
  17. case e.Status != "" && e.ErrorMessage != "":
  18. return fmt.Sprintf("%s: %s", e.Status, e.ErrorMessage)
  19. case e.Status != "":
  20. return e.Status
  21. case e.ErrorMessage != "":
  22. return e.ErrorMessage
  23. default:
  24. // this should not happen
  25. return "something went wrong, please see the ollama server logs for details"
  26. }
  27. }
  28. type GenerateRequest struct {
  29. SessionID int64 `json:"session_id"`
  30. SessionDuration Duration `json:"session_duration,omitempty"`
  31. Model string `json:"model"`
  32. Prompt string `json:"prompt"`
  33. Context []int `json:"context,omitempty"`
  34. Options `json:"options"`
  35. }
  36. type CreateRequest struct {
  37. Name string `json:"name"`
  38. Path string `json:"path"`
  39. }
  40. type DeleteRequest struct {
  41. Name string `json:"name"`
  42. }
  43. type CopyRequest struct {
  44. Source string `json:"source"`
  45. Destination string `json:"destination"`
  46. }
  47. type PullRequest struct {
  48. Name string `json:"name"`
  49. Insecure bool `json:"insecure,omitempty"`
  50. Username string `json:"username"`
  51. Password string `json:"password"`
  52. }
  53. type ProgressResponse struct {
  54. Status string `json:"status"`
  55. Digest string `json:"digest,omitempty"`
  56. Total int `json:"total,omitempty"`
  57. Completed int `json:"completed,omitempty"`
  58. }
  59. type PushRequest struct {
  60. Name string `json:"name"`
  61. Insecure bool `json:"insecure,omitempty"`
  62. Username string `json:"username"`
  63. Password string `json:"password"`
  64. }
  65. type ListResponse struct {
  66. Models []ListResponseModel `json:"models"`
  67. }
  68. type ListResponseModel struct {
  69. Name string `json:"name"`
  70. ModifiedAt time.Time `json:"modified_at"`
  71. Size int `json:"size"`
  72. }
  73. type GenerateResponse struct {
  74. SessionID int64 `json:"session_id"`
  75. SessionExpiresAt time.Time `json:"session_expires_at"`
  76. Model string `json:"model"`
  77. CreatedAt time.Time `json:"created_at"`
  78. Response string `json:"response,omitempty"`
  79. Done bool `json:"done"`
  80. Context []int `json:"context,omitempty"`
  81. TotalDuration time.Duration `json:"total_duration,omitempty"`
  82. LoadDuration time.Duration `json:"load_duration,omitempty"`
  83. PromptEvalCount int `json:"prompt_eval_count,omitempty"`
  84. PromptEvalDuration time.Duration `json:"prompt_eval_duration,omitempty"`
  85. EvalCount int `json:"eval_count,omitempty"`
  86. EvalDuration time.Duration `json:"eval_duration,omitempty"`
  87. }
  88. func (r *GenerateResponse) Summary() {
  89. if r.TotalDuration > 0 {
  90. fmt.Fprintf(os.Stderr, "total duration: %v\n", r.TotalDuration)
  91. }
  92. if r.LoadDuration > 0 {
  93. fmt.Fprintf(os.Stderr, "load duration: %v\n", r.LoadDuration)
  94. }
  95. if r.PromptEvalCount > 0 {
  96. fmt.Fprintf(os.Stderr, "prompt eval count: %d token(s)\n", r.PromptEvalCount)
  97. }
  98. if r.PromptEvalDuration > 0 {
  99. fmt.Fprintf(os.Stderr, "prompt eval duration: %s\n", r.PromptEvalDuration)
  100. fmt.Fprintf(os.Stderr, "prompt eval rate: %.2f tokens/s\n", float64(r.PromptEvalCount)/r.PromptEvalDuration.Seconds())
  101. }
  102. if r.EvalCount > 0 {
  103. fmt.Fprintf(os.Stderr, "eval count: %d token(s)\n", r.EvalCount)
  104. }
  105. if r.EvalDuration > 0 {
  106. fmt.Fprintf(os.Stderr, "eval duration: %s\n", r.EvalDuration)
  107. fmt.Fprintf(os.Stderr, "eval rate: %.2f tokens/s\n", float64(r.EvalCount)/r.EvalDuration.Seconds())
  108. }
  109. }
  110. type Options struct {
  111. Seed int `json:"seed,omitempty"`
  112. // Backend options
  113. UseNUMA bool `json:"numa,omitempty"`
  114. // Model options
  115. NumCtx int `json:"num_ctx,omitempty"`
  116. NumKeep int `json:"num_keep,omitempty"`
  117. NumBatch int `json:"num_batch,omitempty"`
  118. NumGPU int `json:"num_gpu,omitempty"`
  119. MainGPU int `json:"main_gpu,omitempty"`
  120. LowVRAM bool `json:"low_vram,omitempty"`
  121. F16KV bool `json:"f16_kv,omitempty"`
  122. LogitsAll bool `json:"logits_all,omitempty"`
  123. VocabOnly bool `json:"vocab_only,omitempty"`
  124. UseMMap bool `json:"use_mmap,omitempty"`
  125. UseMLock bool `json:"use_mlock,omitempty"`
  126. EmbeddingOnly bool `json:"embedding_only,omitempty"`
  127. // Predict options
  128. RepeatLastN int `json:"repeat_last_n,omitempty"`
  129. RepeatPenalty float32 `json:"repeat_penalty,omitempty"`
  130. FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
  131. PresencePenalty float32 `json:"presence_penalty,omitempty"`
  132. Temperature float32 `json:"temperature,omitempty"`
  133. TopK int `json:"top_k,omitempty"`
  134. TopP float32 `json:"top_p,omitempty"`
  135. TFSZ float32 `json:"tfs_z,omitempty"`
  136. TypicalP float32 `json:"typical_p,omitempty"`
  137. Mirostat int `json:"mirostat,omitempty"`
  138. MirostatTau float32 `json:"mirostat_tau,omitempty"`
  139. MirostatEta float32 `json:"mirostat_eta,omitempty"`
  140. PenalizeNewline bool `json:"penalize_newline,omitempty"`
  141. NumThread int `json:"num_thread,omitempty"`
  142. }
  143. func DefaultOptions() Options {
  144. return Options{
  145. Seed: -1,
  146. UseNUMA: false,
  147. NumCtx: 2048,
  148. NumBatch: 512,
  149. NumGPU: 1,
  150. LowVRAM: false,
  151. F16KV: true,
  152. UseMMap: true,
  153. UseMLock: false,
  154. RepeatLastN: 64,
  155. RepeatPenalty: 1.1,
  156. FrequencyPenalty: 0.0,
  157. PresencePenalty: 0.0,
  158. Temperature: 0.8,
  159. TopK: 40,
  160. TopP: 0.9,
  161. TFSZ: 1.0,
  162. TypicalP: 1.0,
  163. Mirostat: 0,
  164. MirostatTau: 5.0,
  165. MirostatEta: 0.1,
  166. PenalizeNewline: true,
  167. NumThread: runtime.NumCPU(),
  168. }
  169. }
  170. type Duration struct {
  171. time.Duration
  172. }
  173. func (d *Duration) UnmarshalJSON(b []byte) (err error) {
  174. var v any
  175. if err := json.Unmarshal(b, &v); err != nil {
  176. return err
  177. }
  178. d.Duration = 5 * time.Minute
  179. switch t := v.(type) {
  180. case float64:
  181. if t < 0 {
  182. t = math.MaxFloat64
  183. }
  184. d.Duration = time.Duration(t)
  185. case string:
  186. d.Duration, err = time.ParseDuration(t)
  187. if err != nil {
  188. return err
  189. }
  190. }
  191. return nil
  192. }