2023-08-30 20:35:03 +00:00
|
|
|
package llm
|
|
|
|
|
|
|
|
import (
|
2023-11-29 19:00:37 +00:00
|
|
|
_ "embed"
|
2023-08-30 20:35:03 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jmorganca/ollama/api"
|
|
|
|
)
|
|
|
|
|
2023-11-10 00:44:02 +00:00
|
|
|
const jsonGrammar = `
|
|
|
|
root ::= object
|
|
|
|
value ::= object | array | string | number | ("true" | "false" | "null") ws
|
|
|
|
|
|
|
|
object ::=
|
|
|
|
"{" ws (
|
|
|
|
string ":" ws value
|
|
|
|
("," ws string ":" ws value)*
|
|
|
|
)? "}" ws
|
|
|
|
|
|
|
|
array ::=
|
|
|
|
"[" ws (
|
|
|
|
value
|
|
|
|
("," ws value)*
|
|
|
|
)? "]" ws
|
|
|
|
|
|
|
|
string ::=
|
|
|
|
"\"" (
|
|
|
|
[^"\\] |
|
|
|
|
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
|
|
|
|
)* "\"" ws
|
|
|
|
|
|
|
|
number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
|
|
|
|
|
|
|
|
# Optional space: by convention, applied in this grammar after literal chars when allowed
|
|
|
|
ws ::= ([ \t\n] ws)?
|
|
|
|
`
|
|
|
|
|
2023-12-11 21:56:22 +00:00
|
|
|
type ImageData struct {
|
|
|
|
Data []byte `json:"data"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
}
|
|
|
|
|
2023-12-22 17:55:18 +00:00
|
|
|
var payloadMissing = fmt.Errorf("expected dynamic library payloads not included in this build of ollama")
|
2023-10-12 15:16:37 +00:00
|
|
|
|
2023-10-16 23:31:29 +00:00
|
|
|
type prediction struct {
|
2023-09-03 21:46:35 +00:00
|
|
|
Content string `json:"content"`
|
|
|
|
Model string `json:"model"`
|
|
|
|
Prompt string `json:"prompt"`
|
|
|
|
Stop bool `json:"stop"`
|
|
|
|
|
2023-10-16 23:31:29 +00:00
|
|
|
Timings struct {
|
|
|
|
PredictedN int `json:"predicted_n"`
|
|
|
|
PredictedMS float64 `json:"predicted_ms"`
|
|
|
|
PromptN int `json:"prompt_n"`
|
|
|
|
PromptMS float64 `json:"prompt_ms"`
|
|
|
|
}
|
2023-08-30 20:35:03 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 19:00:37 +00:00
|
|
|
const maxRetries = 3
|
2023-10-04 18:09:00 +00:00
|
|
|
|
2023-12-05 19:57:33 +00:00
|
|
|
type PredictOpts struct {
|
2024-01-03 17:01:42 +00:00
|
|
|
Prompt string
|
|
|
|
Format string
|
2024-02-01 02:56:12 +00:00
|
|
|
Images []ImageData
|
2024-01-03 17:01:42 +00:00
|
|
|
Options api.Options
|
2023-12-05 19:57:33 +00:00
|
|
|
}
|
2023-12-05 05:16:27 +00:00
|
|
|
|
2023-12-05 19:57:33 +00:00
|
|
|
type PredictResult struct {
|
|
|
|
Content string
|
|
|
|
Done bool
|
|
|
|
PromptEvalCount int
|
|
|
|
PromptEvalDuration time.Duration
|
|
|
|
EvalCount int
|
|
|
|
EvalDuration time.Duration
|
|
|
|
}
|
2023-09-03 18:10:03 +00:00
|
|
|
|
2023-08-30 20:35:03 +00:00
|
|
|
type TokenizeRequest struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenizeResponse struct {
|
|
|
|
Tokens []int `json:"tokens"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DetokenizeRequest struct {
|
|
|
|
Tokens []int `json:"tokens"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DetokenizeResponse struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmbeddingRequest struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmbeddingResponse struct {
|
|
|
|
Embedding []float64 `json:"embedding"`
|
|
|
|
}
|