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
291bb97e
Commit
291bb97e
authored
Jul 06, 2023
by
Michael Yang
Browse files
client request options
parent
b0618a46
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
31 deletions
+55
-31
api/client.go
api/client.go
+55
-31
No files found.
api/client.go
View file @
291bb97e
...
@@ -25,19 +25,35 @@ func NewClient(hosts ...string) *Client {
...
@@ -25,19 +25,35 @@ func NewClient(hosts ...string) *Client {
}
}
}
}
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
string
,
path
string
,
reqData
any
,
fn
func
(
bts
[]
byte
)
error
)
error
{
type
options
struct
{
var
reqBody
io
.
Reader
requestBody
io
.
Reader
var
data
[]
byte
responseFunc
func
(
bts
[]
byte
)
error
var
err
error
}
if
reqData
!=
nil
{
data
,
err
=
json
.
Marshal
(
reqData
)
func
OptionRequestBody
(
data
any
)
func
(
*
options
)
{
if
err
!=
nil
{
bts
,
err
:=
json
.
Marshal
(
data
)
return
err
if
err
!=
nil
{
}
panic
(
err
)
reqBody
=
bytes
.
NewReader
(
data
)
}
return
func
(
opts
*
options
)
{
opts
.
requestBody
=
bytes
.
NewReader
(
bts
)
}
}
func
OptionResponseFunc
(
fn
func
([]
byte
)
error
)
func
(
*
options
)
{
return
func
(
opts
*
options
)
{
opts
.
responseFunc
=
fn
}
}
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
,
path
string
,
fns
...
func
(
*
options
))
error
{
var
opts
options
for
_
,
fn
:=
range
fns
{
fn
(
&
opts
)
}
}
request
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
method
,
c
.
base
.
JoinPath
(
path
)
.
String
(),
req
Body
)
request
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
method
,
c
.
base
.
JoinPath
(
path
)
.
String
(),
opts
.
request
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -51,10 +67,12 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
...
@@ -51,10 +67,12 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
}
}
defer
response
.
Body
.
Close
()
defer
response
.
Body
.
Close
()
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
if
opts
.
responseFunc
!=
nil
{
for
scanner
.
Scan
()
{
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
if
err
:=
fn
(
scanner
.
Bytes
());
err
!=
nil
{
for
scanner
.
Scan
()
{
return
err
if
err
:=
opts
.
responseFunc
(
scanner
.
Bytes
());
err
!=
nil
{
return
err
}
}
}
}
}
...
@@ -64,25 +82,31 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
...
@@ -64,25 +82,31 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
type
GenerateResponseFunc
func
(
GenerateResponse
)
error
type
GenerateResponseFunc
func
(
GenerateResponse
)
error
func
(
c
*
Client
)
Generate
(
ctx
context
.
Context
,
req
*
GenerateRequest
,
fn
GenerateResponseFunc
)
error
{
func
(
c
*
Client
)
Generate
(
ctx
context
.
Context
,
req
*
GenerateRequest
,
fn
GenerateResponseFunc
)
error
{
return
c
.
stream
(
ctx
,
http
.
MethodPost
,
"/api/generate"
,
req
,
func
(
bts
[]
byte
)
error
{
return
c
.
stream
(
ctx
,
http
.
MethodPost
,
"/api/generate"
,
var
resp
GenerateResponse
OptionRequestBody
(
req
),
if
err
:=
json
.
Unmarshal
(
bts
,
&
resp
);
err
!=
nil
{
OptionResponseFunc
(
func
(
bts
[]
byte
)
error
{
return
err
var
resp
GenerateResponse
}
if
err
:=
json
.
Unmarshal
(
bts
,
&
resp
);
err
!=
nil
{
return
err
return
fn
(
resp
)
}
})
return
fn
(
resp
)
}),
)
}
}
type
PullProgressFunc
func
(
PullProgress
)
error
type
PullProgressFunc
func
(
PullProgress
)
error
func
(
c
*
Client
)
Pull
(
ctx
context
.
Context
,
req
*
PullRequest
,
fn
PullProgressFunc
)
error
{
func
(
c
*
Client
)
Pull
(
ctx
context
.
Context
,
req
*
PullRequest
,
fn
PullProgressFunc
)
error
{
return
c
.
stream
(
ctx
,
http
.
MethodPost
,
"/api/pull"
,
req
,
func
(
bts
[]
byte
)
error
{
return
c
.
stream
(
ctx
,
http
.
MethodPost
,
"/api/pull"
,
var
resp
PullProgress
OptionRequestBody
(
req
),
if
err
:=
json
.
Unmarshal
(
bts
,
&
resp
);
err
!=
nil
{
OptionResponseFunc
(
func
(
bts
[]
byte
)
error
{
return
err
var
resp
PullProgress
}
if
err
:=
json
.
Unmarshal
(
bts
,
&
resp
);
err
!=
nil
{
return
err
return
fn
(
resp
)
}
})
return
fn
(
resp
)
}),
)
}
}
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