".github/vscode:/vscode.git/clone" did not exist on "562f279a2d802e14cb3cf7bd55cae559f467dd9a"
llama.go 1.92 KB
Newer Older
1
2
3
package llm

import (
4
	_ "embed"
5
6
7
8
9
10
	"fmt"
	"time"

	"github.com/jmorganca/ollama/api"
)

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)?
`

Patrick Devine's avatar
Patrick Devine committed
39
40
41
42
43
type ImageData struct {
	Data []byte `json:"data"`
	ID   int    `json:"id"`
}

44
var payloadMissing = fmt.Errorf("expected dynamic library payloads not included in this build of ollama")
45

Michael Yang's avatar
Michael Yang committed
46
type prediction struct {
Michael Yang's avatar
Michael Yang committed
47
48
49
50
51
	Content string `json:"content"`
	Model   string `json:"model"`
	Prompt  string `json:"prompt"`
	Stop    bool   `json:"stop"`

Michael Yang's avatar
Michael Yang committed
52
53
54
55
56
57
	Timings struct {
		PredictedN  int     `json:"predicted_n"`
		PredictedMS float64 `json:"predicted_ms"`
		PromptN     int     `json:"prompt_n"`
		PromptMS    float64 `json:"prompt_ms"`
	}
58
59
}

60
const maxRetries = 3
61

Bruce MacDonald's avatar
Bruce MacDonald committed
62
type PredictOpts struct {
63
64
	Prompt  string
	Format  string
Jeffrey Morgan's avatar
Jeffrey Morgan committed
65
	Images  []ImageData
66
	Options api.Options
Bruce MacDonald's avatar
Bruce MacDonald committed
67
}
68

Bruce MacDonald's avatar
Bruce MacDonald committed
69
70
71
72
73
74
75
76
type PredictResult struct {
	Content            string
	Done               bool
	PromptEvalCount    int
	PromptEvalDuration time.Duration
	EvalCount          int
	EvalDuration       time.Duration
}
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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"`
}