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
c9f45abe
Commit
c9f45abe
authored
Jul 06, 2023
by
Bruce MacDonald
Committed by
Jeffrey Morgan
Jul 06, 2023
Browse files
resumable downloads
parent
7ee75e19
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
17 deletions
+42
-17
api/client.go
api/client.go
+20
-7
api/types.go
api/types.go
+0
-4
cmd/cmd.go
cmd/cmd.go
+6
-3
server/models.go
server/models.go
+16
-3
No files found.
api/client.go
View file @
c9f45abe
...
@@ -9,6 +9,7 @@ import (
...
@@ -9,6 +9,7 @@ import (
"io"
"io"
"net/http"
"net/http"
"strings"
"strings"
"sync"
)
)
type
Client
struct
{
type
Client
struct
{
...
@@ -64,7 +65,14 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
...
@@ -64,7 +65,14 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
for
{
for
{
line
,
err
:=
reader
.
ReadBytes
(
'\n'
)
line
,
err
:=
reader
.
ReadBytes
(
'\n'
)
if
err
!=
nil
{
if
err
!=
nil
{
if
err
==
io
.
EOF
{
break
break
}
else
{
return
err
// Handle other errors
}
}
if
err
:=
checkError
(
res
,
line
);
err
!=
nil
{
return
err
}
}
callback
(
bytes
.
TrimSuffix
(
line
,
[]
byte
(
"
\n
"
)))
callback
(
bytes
.
TrimSuffix
(
line
,
[]
byte
(
"
\n
"
)))
}
}
...
@@ -128,8 +136,9 @@ func (c *Client) Generate(ctx context.Context, req *GenerateRequest, callback fu
...
@@ -128,8 +136,9 @@ func (c *Client) Generate(ctx context.Context, req *GenerateRequest, callback fu
return
&
res
,
nil
return
&
res
,
nil
}
}
func
(
c
*
Client
)
Pull
(
ctx
context
.
Context
,
req
*
PullRequest
,
callback
func
(
progress
PullProgress
))
(
*
PullResponse
,
error
)
{
func
(
c
*
Client
)
Pull
(
ctx
context
.
Context
,
req
*
PullRequest
,
callback
func
(
progress
PullProgress
))
error
{
var
res
PullResponse
var
wg
sync
.
WaitGroup
wg
.
Add
(
1
)
if
err
:=
c
.
stream
(
ctx
,
http
.
MethodPost
,
"/api/pull"
,
req
,
func
(
progressBytes
[]
byte
)
{
if
err
:=
c
.
stream
(
ctx
,
http
.
MethodPost
,
"/api/pull"
,
req
,
func
(
progressBytes
[]
byte
)
{
/*
/*
Events have the following format for progress:
Events have the following format for progress:
...
@@ -148,10 +157,14 @@ func (c *Client) Pull(ctx context.Context, req *PullRequest, callback func(progr
...
@@ -148,10 +157,14 @@ func (c *Client) Pull(ctx context.Context, req *PullRequest, callback func(progr
fmt
.
Println
(
err
)
fmt
.
Println
(
err
)
return
return
}
}
if
progress
.
Completed
>=
progress
.
Total
{
wg
.
Done
()
}
callback
(
progress
)
callback
(
progress
)
});
err
!=
nil
{
});
err
!=
nil
{
return
nil
,
err
return
err
}
}
return
&
res
,
nil
wg
.
Wait
()
return
nil
}
}
api/types.go
View file @
c9f45abe
...
@@ -28,10 +28,6 @@ type PullProgress struct {
...
@@ -28,10 +28,6 @@ type PullProgress struct {
Percent
float64
`json:"percent"`
Percent
float64
`json:"percent"`
}
}
type
PullResponse
struct
{
Response
string
`json:"response"`
}
type
GenerateRequest
struct
{
type
GenerateRequest
struct
{
Model
string
`json:"model"`
Model
string
`json:"model"`
Prompt
string
`json:"prompt"`
Prompt
string
`json:"prompt"`
...
...
cmd/cmd.go
View file @
c9f45abe
...
@@ -40,7 +40,7 @@ func run(model string) error {
...
@@ -40,7 +40,7 @@ func run(model string) error {
mutex
:=
&
sync
.
Mutex
{}
mutex
:=
&
sync
.
Mutex
{}
var
progressData
api
.
PullProgress
var
progressData
api
.
PullProgress
c
allback
:=
func
(
progress
api
.
PullProgress
)
{
pullC
allback
:=
func
(
progress
api
.
PullProgress
)
{
mutex
.
Lock
()
mutex
.
Lock
()
progressData
=
progress
progressData
=
progress
if
bar
==
nil
{
if
bar
==
nil
{
...
@@ -60,8 +60,11 @@ func run(model string) error {
...
@@ -60,8 +60,11 @@ func run(model string) error {
bar
.
Set
(
int
(
progress
.
Completed
))
bar
.
Set
(
int
(
progress
.
Completed
))
mutex
.
Unlock
()
mutex
.
Unlock
()
}
}
_
,
err
=
client
.
Pull
(
context
.
Background
(),
&
pr
,
c
allback
)
if
err
:
=
client
.
Pull
(
context
.
Background
(),
&
pr
,
pullC
allback
)
;
err
!=
nil
{
return
err
return
err
}
fmt
.
Println
(
"Up to date."
)
return
nil
}
}
func
serve
()
error
{
func
serve
()
error
{
...
...
server/models.go
View file @
c9f45abe
...
@@ -79,6 +79,7 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
...
@@ -79,6 +79,7 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
panic
(
err
)
panic
(
err
)
}
}
// check for resume
// check for resume
alreadyDownloaded
:=
0
fileInfo
,
err
:=
os
.
Stat
(
fileName
)
fileInfo
,
err
:=
os
.
Stat
(
fileName
)
if
err
!=
nil
{
if
err
!=
nil
{
if
!
os
.
IsNotExist
(
err
)
{
if
!
os
.
IsNotExist
(
err
)
{
...
@@ -86,7 +87,8 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
...
@@ -86,7 +87,8 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
}
}
// file doesn't exist, create it now
// file doesn't exist, create it now
}
else
{
}
else
{
req
.
Header
.
Add
(
"Range"
,
"bytes="
+
strconv
.
FormatInt
(
fileInfo
.
Size
(),
10
)
+
"-"
)
alreadyDownloaded
=
int
(
fileInfo
.
Size
())
req
.
Header
.
Add
(
"Range"
,
"bytes="
+
strconv
.
Itoa
(
alreadyDownloaded
)
+
"-"
)
}
}
resp
,
err
:=
client
.
Do
(
req
)
resp
,
err
:=
client
.
Do
(
req
)
...
@@ -96,7 +98,17 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
...
@@ -96,7 +98,17 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
defer
resp
.
Body
.
Close
()
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
!=
http
.
StatusOK
{
if
resp
.
StatusCode
==
http
.
StatusRequestedRangeNotSatisfiable
{
// already downloaded
progressCh
<-
api
.
PullProgress
{
Total
:
alreadyDownloaded
,
Completed
:
alreadyDownloaded
,
Percent
:
100
,
}
return
nil
}
if
resp
.
StatusCode
!=
http
.
StatusOK
&&
resp
.
StatusCode
!=
http
.
StatusPartialContent
{
return
fmt
.
Errorf
(
"failed to download model: %s"
,
resp
.
Status
)
return
fmt
.
Errorf
(
"failed to download model: %s"
,
resp
.
Status
)
}
}
...
@@ -109,7 +121,8 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
...
@@ -109,7 +121,8 @@ func saveModel(model *Model, progressCh chan<- api.PullProgress) error {
totalSize
,
_
:=
strconv
.
Atoi
(
resp
.
Header
.
Get
(
"Content-Length"
))
totalSize
,
_
:=
strconv
.
Atoi
(
resp
.
Header
.
Get
(
"Content-Length"
))
buf
:=
make
([]
byte
,
1024
)
buf
:=
make
([]
byte
,
1024
)
totalBytes
:=
0
totalBytes
:=
alreadyDownloaded
totalSize
+=
alreadyDownloaded
for
{
for
{
n
,
err
:=
resp
.
Body
.
Read
(
buf
)
n
,
err
:=
resp
.
Body
.
Read
(
buf
)
...
...
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