llama.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package llm
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "time"
  6. "github.com/ollama/ollama/api"
  7. )
  8. const jsonGrammar = `
  9. root ::= object
  10. value ::= object | array | string | number | ("true" | "false" | "null") ws
  11. object ::=
  12. "{" ws (
  13. string ":" ws value
  14. ("," ws string ":" ws value)*
  15. )? "}" ws
  16. array ::=
  17. "[" ws (
  18. value
  19. ("," ws value)*
  20. )? "]" ws
  21. string ::=
  22. "\"" (
  23. [^"\\] |
  24. "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
  25. )* "\"" ws
  26. number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
  27. # Optional space: by convention, applied in this grammar after literal chars when allowed
  28. ws ::= ([ \t\n] ws)?
  29. `
  30. type ImageData struct {
  31. Data []byte `json:"data"`
  32. ID int `json:"id"`
  33. }
  34. var payloadMissing = fmt.Errorf("expected dynamic library payloads not included in this build of ollama")
  35. type prediction struct {
  36. Content string `json:"content"`
  37. Model string `json:"model"`
  38. Prompt string `json:"prompt"`
  39. Stop bool `json:"stop"`
  40. Timings struct {
  41. PredictedN int `json:"predicted_n"`
  42. PredictedMS float64 `json:"predicted_ms"`
  43. PromptN int `json:"prompt_n"`
  44. PromptMS float64 `json:"prompt_ms"`
  45. }
  46. }
  47. const maxRetries = 3
  48. type PredictOpts struct {
  49. Prompt string
  50. Format string
  51. Images []ImageData
  52. Options api.Options
  53. }
  54. type PredictResult struct {
  55. Content string
  56. Done bool
  57. PromptEvalCount int
  58. PromptEvalDuration time.Duration
  59. EvalCount int
  60. EvalDuration time.Duration
  61. }
  62. type TokenizeRequest struct {
  63. Content string `json:"content"`
  64. }
  65. type TokenizeResponse struct {
  66. Tokens []int `json:"tokens"`
  67. }
  68. type DetokenizeRequest struct {
  69. Tokens []int `json:"tokens"`
  70. }
  71. type DetokenizeResponse struct {
  72. Content string `json:"content"`
  73. }
  74. type EmbeddingRequest struct {
  75. Content string `json:"content"`
  76. }
  77. type EmbeddingResponse struct {
  78. Embedding []float64 `json:"embedding"`
  79. }