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
b0e63bfb
Commit
b0e63bfb
authored
Jul 06, 2023
by
Michael Yang
Browse files
simplify api client
parent
c4b9e849
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
25 deletions
+21
-25
api/client.go
api/client.go
+19
-11
cmd/cmd.go
cmd/cmd.go
+2
-14
No files found.
api/client.go
View file @
b0e63bfb
...
@@ -5,14 +5,24 @@ import (
...
@@ -5,14 +5,24 @@ import (
"bytes"
"bytes"
"context"
"context"
"encoding/json"
"encoding/json"
"fmt"
"io"
"io"
"net/http"
"net/http"
"net/url"
)
)
type
Client
struct
{
type
Client
struct
{
URL
string
base
url
.
URL
HTTP
http
.
Client
}
func
NewClient
(
hosts
...
string
)
*
Client
{
host
:=
"127.0.0.1:11434"
if
len
(
hosts
)
>
0
{
host
=
hosts
[
0
]
}
return
&
Client
{
base
:
url
.
URL
{
Scheme
:
"http"
,
Host
:
host
},
}
}
}
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
string
,
path
string
,
reqData
any
,
fn
func
(
bts
[]
byte
)
error
)
error
{
func
(
c
*
Client
)
stream
(
ctx
context
.
Context
,
method
string
,
path
string
,
reqData
any
,
fn
func
(
bts
[]
byte
)
error
)
error
{
...
@@ -27,23 +37,21 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
...
@@ -27,23 +37,21 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
reqBody
=
bytes
.
NewReader
(
data
)
reqBody
=
bytes
.
NewReader
(
data
)
}
}
url
:=
fmt
.
Sprintf
(
"%s%s"
,
c
.
URL
,
path
)
request
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
method
,
c
.
base
.
JoinPath
(
path
)
.
String
(),
reqBody
)
req
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
method
,
url
,
reqBody
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
uest
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
req
uest
.
Header
.
Set
(
"Accept"
,
"application/json"
)
res
,
err
:=
c
.
HTTP
.
Do
(
req
)
res
ponse
,
err
:=
http
.
DefaultClient
.
Do
(
req
uest
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
defer
res
.
Body
.
Close
()
defer
res
ponse
.
Body
.
Close
()
scanner
:=
bufio
.
NewScanner
(
res
.
Body
)
scanner
:=
bufio
.
NewScanner
(
res
ponse
.
Body
)
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
if
err
:=
fn
(
scanner
.
Bytes
());
err
!=
nil
{
if
err
:=
fn
(
scanner
.
Bytes
());
err
!=
nil
{
return
err
return
err
...
...
cmd/cmd.go
View file @
b0e63bfb
...
@@ -36,10 +36,7 @@ func RunRun(cmd *cobra.Command, args []string) error {
...
@@ -36,10 +36,7 @@ func RunRun(cmd *cobra.Command, args []string) error {
}
}
func
pull
(
model
string
)
error
{
func
pull
(
model
string
)
error
{
client
,
err
:=
NewAPIClient
()
client
:=
api
.
NewClient
()
if
err
!=
nil
{
return
err
}
var
bar
*
progressbar
.
ProgressBar
var
bar
*
progressbar
.
ProgressBar
return
client
.
Pull
(
return
client
.
Pull
(
...
@@ -68,10 +65,7 @@ func RunGenerate(_ *cobra.Command, args []string) error {
...
@@ -68,10 +65,7 @@ func RunGenerate(_ *cobra.Command, args []string) error {
}
}
func
generate
(
model
string
,
prompts
...
string
)
error
{
func
generate
(
model
string
,
prompts
...
string
)
error
{
client
,
err
:=
NewAPIClient
()
client
:=
api
.
NewClient
()
if
err
!=
nil
{
return
err
}
for
_
,
prompt
:=
range
prompts
{
for
_
,
prompt
:=
range
prompts
{
client
.
Generate
(
context
.
Background
(),
&
api
.
GenerateRequest
{
Model
:
model
,
Prompt
:
prompt
},
func
(
resp
api
.
GenerateResponse
)
error
{
client
.
Generate
(
context
.
Background
(),
&
api
.
GenerateRequest
{
Model
:
model
,
Prompt
:
prompt
},
func
(
resp
api
.
GenerateResponse
)
error
{
...
@@ -121,12 +115,6 @@ func RunServer(_ *cobra.Command, _ []string) error {
...
@@ -121,12 +115,6 @@ func RunServer(_ *cobra.Command, _ []string) error {
return
server
.
Serve
(
ln
)
return
server
.
Serve
(
ln
)
}
}
func
NewAPIClient
()
(
*
api
.
Client
,
error
)
{
return
&
api
.
Client
{
URL
:
"http://localhost:11434"
,
},
nil
}
func
NewCLI
()
*
cobra
.
Command
{
func
NewCLI
()
*
cobra
.
Command
{
log
.
SetFlags
(
log
.
LstdFlags
|
log
.
Lshortfile
)
log
.
SetFlags
(
log
.
LstdFlags
|
log
.
Lshortfile
)
...
...
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