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
e1351674
Unverified
Commit
e1351674
authored
Feb 01, 2024
by
Jeffrey Morgan
Committed by
GitHub
Feb 01, 2024
Browse files
Add multimodel support to `ollama run` in noninteractive mopde (#2317)
parent
38296ab3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
49 deletions
+56
-49
cmd/cmd.go
cmd/cmd.go
+54
-47
cmd/interactive.go
cmd/interactive.go
+2
-2
No files found.
cmd/cmd.go
View file @
e1351674
...
...
@@ -25,6 +25,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
"golang.org/x/exp/slices"
"golang.org/x/term"
"github.com/jmorganca/ollama/api"
...
...
@@ -147,7 +148,7 @@ func RunHandler(cmd *cobra.Command, args []string) error {
name
:=
args
[
0
]
// check if the model exists on the server
_
,
err
=
client
.
Show
(
cmd
.
Context
(),
&
api
.
ShowRequest
{
Name
:
name
})
show
,
err
:
=
client
.
Show
(
cmd
.
Context
(),
&
api
.
ShowRequest
{
Name
:
name
})
var
statusError
api
.
StatusError
switch
{
case
errors
.
As
(
err
,
&
statusError
)
&&
statusError
.
StatusCode
==
http
.
StatusNotFound
:
...
...
@@ -158,7 +159,50 @@ func RunHandler(cmd *cobra.Command, args []string) error {
return
err
}
return
RunGenerate
(
cmd
,
args
)
interactive
:=
true
opts
:=
runOptions
{
Model
:
args
[
0
],
WordWrap
:
os
.
Getenv
(
"TERM"
)
==
"xterm-256color"
,
Options
:
map
[
string
]
interface
{}{},
MultiModal
:
slices
.
Contains
(
show
.
Details
.
Families
,
"clip"
),
ParentModel
:
show
.
Details
.
ParentModel
,
}
format
,
err
:=
cmd
.
Flags
()
.
GetString
(
"format"
)
if
err
!=
nil
{
return
err
}
opts
.
Format
=
format
prompts
:=
args
[
1
:
]
// prepend stdin to the prompt if provided
if
!
term
.
IsTerminal
(
int
(
os
.
Stdin
.
Fd
()))
{
in
,
err
:=
io
.
ReadAll
(
os
.
Stdin
)
if
err
!=
nil
{
return
err
}
prompts
=
append
([]
string
{
string
(
in
)},
prompts
...
)
opts
.
WordWrap
=
false
interactive
=
false
}
opts
.
Prompt
=
strings
.
Join
(
prompts
,
" "
)
if
len
(
prompts
)
>
0
{
interactive
=
false
}
nowrap
,
err
:=
cmd
.
Flags
()
.
GetBool
(
"nowordwrap"
)
if
err
!=
nil
{
return
err
}
opts
.
WordWrap
=
!
nowrap
if
!
interactive
{
return
generate
(
cmd
,
opts
)
}
return
generateInteractive
(
cmd
,
opts
)
}
func
PushHandler
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
...
...
@@ -410,51 +454,6 @@ func PullHandler(cmd *cobra.Command, args []string) error {
return
nil
}
func
RunGenerate
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
interactive
:=
true
opts
:=
runOptions
{
Model
:
args
[
0
],
WordWrap
:
os
.
Getenv
(
"TERM"
)
==
"xterm-256color"
,
Options
:
map
[
string
]
interface
{}{},
}
format
,
err
:=
cmd
.
Flags
()
.
GetString
(
"format"
)
if
err
!=
nil
{
return
err
}
opts
.
Format
=
format
prompts
:=
args
[
1
:
]
// prepend stdin to the prompt if provided
if
!
term
.
IsTerminal
(
int
(
os
.
Stdin
.
Fd
()))
{
in
,
err
:=
io
.
ReadAll
(
os
.
Stdin
)
if
err
!=
nil
{
return
err
}
prompts
=
append
([]
string
{
string
(
in
)},
prompts
...
)
opts
.
WordWrap
=
false
interactive
=
false
}
opts
.
Prompt
=
strings
.
Join
(
prompts
,
" "
)
if
len
(
prompts
)
>
0
{
interactive
=
false
}
nowrap
,
err
:=
cmd
.
Flags
()
.
GetBool
(
"nowordwrap"
)
if
err
!=
nil
{
return
err
}
opts
.
WordWrap
=
!
nowrap
if
!
interactive
{
return
generate
(
cmd
,
opts
)
}
return
generateInteractive
(
cmd
,
opts
)
}
type
generateContextKey
string
type
runOptions
struct
{
...
...
@@ -630,10 +629,18 @@ func generate(cmd *cobra.Command, opts runOptions) error {
return
nil
}
if
opts
.
MultiModal
{
opts
.
Prompt
,
opts
.
Images
,
err
=
extractFileData
(
opts
.
Prompt
)
if
err
!=
nil
{
return
err
}
}
request
:=
api
.
GenerateRequest
{
Model
:
opts
.
Model
,
Prompt
:
opts
.
Prompt
,
Context
:
generateContext
,
Images
:
opts
.
Images
,
Format
:
opts
.
Format
,
System
:
opts
.
System
,
Template
:
opts
.
Template
,
...
...
cmd/interactive.go
View file @
e1351674
...
...
@@ -601,10 +601,10 @@ func extractFileData(input string) (string, []api.ImageData, error) {
if
os
.
IsNotExist
(
err
)
{
continue
}
fmt
.
P
rintf
(
"Couldn't process image: %q
\n
"
,
err
)
fmt
.
Fp
rintf
(
os
.
Stderr
,
"Couldn't process image: %q
\n
"
,
err
)
return
""
,
imgs
,
err
}
fmt
.
P
rintf
(
"Added image '%s'
\n
"
,
nfp
)
fmt
.
Fp
rintf
(
os
.
Stderr
,
"Added image '%s'
\n
"
,
nfp
)
input
=
strings
.
ReplaceAll
(
input
,
fp
,
""
)
imgs
=
append
(
imgs
,
data
)
}
...
...
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