Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
38d9036b
Unverified
Commit
38d9036b
authored
Jul 29, 2024
by
Michael Yang
Committed by
GitHub
Jul 29, 2024
Browse files
Merge pull request #5992 from ollama/mxyng/save
fix: model save
parents
6f26e932
3d9de805
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
61 deletions
+60
-61
cmd/interactive.go
cmd/interactive.go
+26
-20
cmd/interactive_test.go
cmd/interactive_test.go
+34
-41
No files found.
cmd/interactive.go
View file @
38d9036b
package
cmd
import
(
"cmp"
"errors"
"fmt"
"io"
...
...
@@ -9,13 +10,14 @@ import (
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"github.com/ollama/ollama/api"
"github.com/ollama/ollama/envconfig"
"github.com/ollama/ollama/parser"
"github.com/ollama/ollama/progress"
"github.com/ollama/ollama/readline"
"github.com/ollama/ollama/types/errtypes"
...
...
@@ -376,9 +378,9 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
return
err
}
req
:=
&
api
.
ShowRequest
{
Name
:
opts
.
Model
,
System
:
opts
.
System
,
Options
:
opts
.
Options
,
Name
:
opts
.
Model
,
System
:
opts
.
System
,
Options
:
opts
.
Options
,
}
resp
,
err
:=
client
.
Show
(
cmd
.
Context
(),
req
)
if
err
!=
nil
{
...
...
@@ -507,31 +509,35 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
}
func
buildModelfile
(
opts
runOptions
)
string
{
var
mf
strings
.
Builder
model
:=
opts
.
ParentModel
if
model
==
""
{
model
=
opts
.
Model
}
fmt
.
Fprintf
(
&
mf
,
"FROM %s
\n
"
,
model
)
var
f
parser
.
File
f
.
Commands
=
append
(
f
.
Commands
,
parser
.
Command
{
Name
:
"model"
,
Args
:
cmp
.
Or
(
opts
.
ParentModel
,
opts
.
Model
)})
if
opts
.
System
!=
""
{
f
mt
.
Fprintf
(
&
mf
,
"SYSTEM
\"\"\"
%s
\"\"\"\n
"
,
opts
.
System
)
f
.
Commands
=
append
(
f
.
Commands
,
parser
.
Command
{
Name
:
"system"
,
Args
:
opts
.
System
}
)
}
keys
:=
make
([]
string
,
0
)
for
k
:=
range
opts
.
Options
{
keys
=
append
(
keys
,
k
)
}
sort
.
Strings
(
keys
)
keys
:=
maps
.
Keys
(
opts
.
Options
)
slices
.
Sort
(
keys
)
for
_
,
k
:=
range
keys
{
fmt
.
Fprintf
(
&
mf
,
"PARAMETER %s %v
\n
"
,
k
,
opts
.
Options
[
k
])
v
:=
opts
.
Options
[
k
]
var
cmds
[]
parser
.
Command
switch
t
:=
v
.
(
type
)
{
case
[]
string
:
for
_
,
s
:=
range
t
{
cmds
=
append
(
cmds
,
parser
.
Command
{
Name
:
k
,
Args
:
s
})
}
default
:
cmds
=
append
(
cmds
,
parser
.
Command
{
Name
:
k
,
Args
:
fmt
.
Sprintf
(
"%v"
,
t
)})
}
f
.
Commands
=
append
(
f
.
Commands
,
cmds
...
)
}
fmt
.
Fprintln
(
&
mf
)
for
_
,
msg
:=
range
opts
.
Messages
{
fmt
.
F
printf
(
&
mf
,
"MESSAGE %s
\"\"\"
%s
\"\"\"\n
"
,
msg
.
Role
,
msg
.
Content
)
f
.
Commands
=
append
(
f
.
Commands
,
parser
.
Command
{
Name
:
"message"
,
Args
:
fmt
.
S
printf
(
"%s: %s
"
,
msg
.
Role
,
msg
.
Content
)
})
}
return
m
f
.
String
()
return
f
.
String
()
}
func
normalizeFilePath
(
fp
string
)
string
{
...
...
cmd/interactive_test.go
View file @
38d9036b
package
cmd
import
(
"bytes"
"testing"
"text/template"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ollama/ollama/api"
)
...
...
@@ -57,58 +55,53 @@ d:\path with\spaces\seven.svg inbetween7 c:\users\jdoe\eight.png inbetween8
func
TestModelfileBuilder
(
t
*
testing
.
T
)
{
opts
:=
runOptions
{
Model
:
"hork"
,
System
:
"You are part horse and part shark, but all hork. Do horklike things"
,
Model
:
"hork"
,
System
:
"You are part horse and part shark, but all hork. Do horklike things"
,
Messages
:
[]
api
.
Message
{
{
Role
:
"user"
,
Content
:
"Hey there hork!"
},
{
Role
:
"assistant"
,
Content
:
"Yes it is true, I am half horse, half shark."
},
},
Options
:
map
[
string
]
interface
{}{},
Options
:
map
[
string
]
any
{
"temperature"
:
0.9
,
"seed"
:
42
,
"penalize_newline"
:
false
,
"stop"
:
[]
string
{
"hi"
,
"there"
},
},
}
opts
.
Options
[
"temperature"
]
=
0.9
opts
.
Options
[
"seed"
]
=
42
opts
.
Options
[
"penalize_newline"
]
=
false
opts
.
Options
[
"stop"
]
=
[]
string
{
"hi"
,
"there"
}
mf
:=
buildModelfile
(
opts
)
expectedModelfile
:=
`FROM {{.Model}}
SYSTEM """{{.System}}"""
t
.
Run
(
"model"
,
func
(
t
*
testing
.
T
)
{
expect
:=
`FROM hork
SYSTEM You are part horse and part shark, but all hork. Do horklike things
PARAMETER penalize_newline false
PARAMETER seed 42
PARAMETER stop [hi there]
PARAMETER stop hi
PARAMETER stop there
PARAMETER temperature 0.9
MESSAGE user """Hey there hork!"""
MESSAGE assistant """Yes it is true, I am half horse, half shark."""
MESSAGE user Hey there hork!
MESSAGE assistant Yes it is true, I am half horse, half shark.
`
tmpl
,
err
:=
template
.
New
(
""
)
.
Parse
(
expectedModelfile
)
require
.
NoError
(
t
,
err
)
var
buf
bytes
.
Buffer
err
=
tmpl
.
Execute
(
&
buf
,
opts
)
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
buf
.
String
(),
mf
)
actual
:=
buildModelfile
(
opts
)
if
diff
:=
cmp
.
Diff
(
expect
,
actual
);
diff
!=
""
{
t
.
Errorf
(
"mismatch (-want +got):
\n
%s"
,
diff
)
}
})
opts
.
P
arent
M
odel
=
"horseshark"
mf
=
buildModelfile
(
opts
)
expect
edModelfile
=
`FROM {{.ParentModel}}
SYSTEM
"""{{.System}}"""
t
.
Run
(
"p
arent
m
odel
"
,
func
(
t
*
testing
.
T
)
{
opts
.
ParentModel
=
"horseshark"
expect
:=
`FROM horseshark
SYSTEM
You are part horse and part shark, but all hork. Do horklike things
PARAMETER penalize_newline false
PARAMETER seed 42
PARAMETER stop [hi there]
PARAMETER stop hi
PARAMETER stop there
PARAMETER temperature 0.9
MESSAGE user """Hey there hork!"""
MESSAGE assistant """Yes it is true, I am half horse, half shark."""
MESSAGE user Hey there hork!
MESSAGE assistant Yes it is true, I am half horse, half shark.
`
tmpl
,
err
=
template
.
New
(
""
)
.
Parse
(
expectedModelfile
)
require
.
NoError
(
t
,
err
)
var
parentBuf
bytes
.
Buffer
err
=
tmpl
.
Execute
(
&
parentBuf
,
opts
)
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
parentBuf
.
String
(),
mf
)
actual
:=
buildModelfile
(
opts
)
if
diff
:=
cmp
.
Diff
(
expect
,
actual
);
diff
!=
""
{
t
.
Errorf
(
"mismatch (-want +got):
\n
%s"
,
diff
)
}
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment