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
e72fe794
"vscode:/vscode.git/clone" did not exist on "4452f68d8fcede76817cb04fbee171d0d102443a"
Commit
e72fe794
authored
Jul 31, 2023
by
Bruce MacDonald
Browse files
check server is running before running command
parent
90ba0b80
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
25 deletions
+72
-25
api/client.go
api/client.go
+7
-0
cmd/cmd.go
cmd/cmd.go
+65
-25
No files found.
api/client.go
View file @
e72fe794
...
...
@@ -223,3 +223,10 @@ func (c *Client) Delete(ctx context.Context, req *DeleteRequest) error {
}
return
nil
}
func
(
c
*
Client
)
Heartbeat
(
ctx
context
.
Context
)
error
{
if
err
:=
c
.
do
(
ctx
,
http
.
MethodGet
,
"/"
,
nil
,
nil
);
err
!=
nil
{
return
err
}
return
nil
}
cmd/cmd.go
View file @
e72fe794
...
...
@@ -10,7 +10,9 @@ import (
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
...
...
@@ -431,7 +433,6 @@ func generateInteractive(cmd *cobra.Command, model string) error {
usage
()
continue
}
}
else
{
usage
()
continue
...
...
@@ -525,6 +526,38 @@ func RunServer(_ *cobra.Command, _ []string) error {
return
server
.
Serve
(
ln
)
}
func
checkServerHeartbeat
(
_
*
cobra
.
Command
,
_
[]
string
)
error
{
client
:=
api
.
NewClient
()
if
err
:=
client
.
Heartbeat
(
context
.
Background
());
err
!=
nil
{
if
strings
.
Contains
(
err
.
Error
(),
"connection refused"
)
{
if
runtime
.
GOOS
==
"darwin"
{
// if the mac app is available, start it
if
_
,
err
:=
os
.
Stat
(
"/Applications/Ollama.app"
);
err
==
nil
{
if
err
:=
exec
.
Command
(
"/usr/bin/open"
,
"-a"
,
"/Applications/Ollama.app"
)
.
Run
();
err
!=
nil
{
return
err
}
// wait for the server to start
timeout
:=
time
.
After
(
5
*
time
.
Second
)
tick
:=
time
.
Tick
(
500
*
time
.
Millisecond
)
for
{
select
{
case
<-
timeout
:
return
errors
.
New
(
"timed out waiting for server to start"
)
case
<-
tick
:
if
err
:=
client
.
Heartbeat
(
context
.
Background
());
err
==
nil
{
return
nil
// server has started
}
}
}
}
}
return
fmt
.
Errorf
(
"could not connect to ollama server, run 'ollama serve' to start it"
)
}
return
err
}
return
nil
}
func
NewCLI
()
*
cobra
.
Command
{
log
.
SetFlags
(
log
.
LstdFlags
|
log
.
Lshortfile
)
...
...
@@ -543,6 +576,7 @@ func NewCLI() *cobra.Command {
Use
:
"create MODEL"
,
Short
:
"Create a model from a Modelfile"
,
Args
:
cobra
.
MinimumNArgs
(
1
),
PreRunE
:
checkServerHeartbeat
,
RunE
:
CreateHandler
,
}
...
...
@@ -552,6 +586,7 @@ func NewCLI() *cobra.Command {
Use
:
"run MODEL [PROMPT]"
,
Short
:
"Run a model"
,
Args
:
cobra
.
MinimumNArgs
(
1
),
PreRunE
:
checkServerHeartbeat
,
RunE
:
RunHandler
,
}
...
...
@@ -568,6 +603,7 @@ func NewCLI() *cobra.Command {
Use
:
"pull MODEL"
,
Short
:
"Pull a model from a registry"
,
Args
:
cobra
.
MinimumNArgs
(
1
),
PreRunE
:
checkServerHeartbeat
,
RunE
:
PullHandler
,
}
...
...
@@ -577,6 +613,7 @@ func NewCLI() *cobra.Command {
Use
:
"push MODEL"
,
Short
:
"Push a model to a registry"
,
Args
:
cobra
.
MinimumNArgs
(
1
),
PreRunE
:
checkServerHeartbeat
,
RunE
:
PushHandler
,
}
...
...
@@ -586,6 +623,7 @@ func NewCLI() *cobra.Command {
Use
:
"list"
,
Aliases
:
[]
string
{
"ls"
},
Short
:
"List models"
,
PreRunE
:
checkServerHeartbeat
,
RunE
:
ListHandler
,
}
...
...
@@ -593,6 +631,7 @@ func NewCLI() *cobra.Command {
Use
:
"cp"
,
Short
:
"Copy a model"
,
Args
:
cobra
.
MinimumNArgs
(
2
),
PreRunE
:
checkServerHeartbeat
,
RunE
:
CopyHandler
,
}
...
...
@@ -600,6 +639,7 @@ func NewCLI() *cobra.Command {
Use
:
"rm"
,
Short
:
"Remove a model"
,
Args
:
cobra
.
MinimumNArgs
(
1
),
PreRunE
:
checkServerHeartbeat
,
RunE
:
DeleteHandler
,
}
...
...
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