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
0859d509
Unverified
Commit
0859d509
authored
Jul 10, 2023
by
Michael Yang
Committed by
GitHub
Jul 10, 2023
Browse files
Merge pull request #58 from jmorganca/generate-errors
return error in generate response
parents
66bbf059
edba935d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
7 deletions
+38
-7
api/client.go
api/client.go
+27
-1
api/types.go
api/types.go
+1
-0
cmd/cmd.go
cmd/cmd.go
+7
-2
server/routes.go
server/routes.go
+3
-4
No files found.
api/client.go
View file @
0859d509
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"bytes"
"bytes"
"context"
"context"
"encoding/json"
"encoding/json"
"fmt"
"io"
"io"
"net/http"
"net/http"
"net/url"
"net/url"
...
@@ -25,6 +26,18 @@ func NewClient(hosts ...string) *Client {
...
@@ -25,6 +26,18 @@ func NewClient(hosts ...string) *Client {
}
}
}
}
func
StatusError
(
status
int
,
message
...
string
)
error
{
if
status
<
400
{
return
nil
}
if
len
(
message
)
>
0
&&
len
(
message
[
0
])
>
0
{
return
fmt
.
Errorf
(
"%d %s: %s"
,
status
,
http
.
StatusText
(
status
),
message
[
0
])
}
return
fmt
.
Errorf
(
"%d %s"
,
status
,
http
.
StatusText
(
status
))
}
type
options
struct
{
type
options
struct
{
requestBody
io
.
Reader
requestBody
io
.
Reader
responseFunc
func
(
bts
[]
byte
)
error
responseFunc
func
(
bts
[]
byte
)
error
...
@@ -70,7 +83,20 @@ func (c *Client) stream(ctx context.Context, method, path string, fns ...func(*o
...
@@ -70,7 +83,20 @@ func (c *Client) stream(ctx context.Context, method, path string, fns ...func(*o
if
opts
.
responseFunc
!=
nil
{
if
opts
.
responseFunc
!=
nil
{
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
scanner
:=
bufio
.
NewScanner
(
response
.
Body
)
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
if
err
:=
opts
.
responseFunc
(
scanner
.
Bytes
());
err
!=
nil
{
var
errorResponse
struct
{
Error
string
`json:"error"`
}
bts
:=
scanner
.
Bytes
()
if
err
:=
json
.
Unmarshal
(
bts
,
&
errorResponse
);
err
!=
nil
{
return
err
}
if
err
:=
StatusError
(
response
.
StatusCode
,
errorResponse
.
Error
);
err
!=
nil
{
return
err
}
if
err
:=
opts
.
responseFunc
(
bts
);
err
!=
nil
{
return
err
return
err
}
}
}
}
...
...
api/types.go
View file @
0859d509
...
@@ -15,6 +15,7 @@ func (e Error) Error() string {
...
@@ -15,6 +15,7 @@ func (e Error) Error() string {
if
e
.
Message
==
""
{
if
e
.
Message
==
""
{
return
fmt
.
Sprintf
(
"%d %v"
,
e
.
Code
,
strings
.
ToLower
(
http
.
StatusText
(
int
(
e
.
Code
))))
return
fmt
.
Sprintf
(
"%d %v"
,
e
.
Code
,
strings
.
ToLower
(
http
.
StatusText
(
int
(
e
.
Code
))))
}
}
return
e
.
Message
return
e
.
Message
}
}
...
...
cmd/cmd.go
View file @
0859d509
...
@@ -100,14 +100,19 @@ func generate(model, prompt string) error {
...
@@ -100,14 +100,19 @@ func generate(model, prompt string) error {
}
}
}()
}()
client
.
Generate
(
context
.
Background
(),
&
api
.
GenerateRequest
{
Model
:
model
,
Prompt
:
prompt
},
func
(
resp
api
.
GenerateResponse
)
error
{
request
:=
api
.
GenerateRequest
{
Model
:
model
,
Prompt
:
prompt
}
fn
:=
func
(
resp
api
.
GenerateResponse
)
error
{
if
!
spinner
.
IsFinished
()
{
if
!
spinner
.
IsFinished
()
{
spinner
.
Finish
()
spinner
.
Finish
()
}
}
fmt
.
Print
(
resp
.
Response
)
fmt
.
Print
(
resp
.
Response
)
return
nil
return
nil
})
}
if
err
:=
client
.
Generate
(
context
.
Background
(),
&
request
,
fn
);
err
!=
nil
{
return
err
}
fmt
.
Println
()
fmt
.
Println
()
fmt
.
Println
()
fmt
.
Println
()
...
...
server/routes.go
View file @
0859d509
...
@@ -4,7 +4,6 @@ import (
...
@@ -4,7 +4,6 @@ import (
"embed"
"embed"
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"io"
"io"
"log"
"log"
"math"
"math"
...
@@ -46,7 +45,7 @@ func generate(c *gin.Context) {
...
@@ -46,7 +45,7 @@ func generate(c *gin.Context) {
req
.
PredictOptions
=
&
api
.
DefaultPredictOptions
req
.
PredictOptions
=
&
api
.
DefaultPredictOptions
}
}
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"
message
"
:
err
.
Error
()})
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"
error
"
:
err
.
Error
()})
return
return
}
}
...
@@ -66,7 +65,7 @@ func generate(c *gin.Context) {
...
@@ -66,7 +65,7 @@ func generate(c *gin.Context) {
model
,
err
:=
llama
.
New
(
req
.
Model
,
modelOpts
)
model
,
err
:=
llama
.
New
(
req
.
Model
,
modelOpts
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"Loading the model failed:"
,
err
.
Error
())
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
err
.
Error
()
}
)
return
return
}
}
defer
model
.
Free
()
defer
model
.
Free
()
...
@@ -80,7 +79,7 @@ func generate(c *gin.Context) {
...
@@ -80,7 +79,7 @@ func generate(c *gin.Context) {
if
template
:=
templates
.
Lookup
(
match
);
template
!=
nil
{
if
template
:=
templates
.
Lookup
(
match
);
template
!=
nil
{
var
sb
strings
.
Builder
var
sb
strings
.
Builder
if
err
:=
template
.
Execute
(
&
sb
,
req
);
err
!=
nil
{
if
err
:=
template
.
Execute
(
&
sb
,
req
);
err
!=
nil
{
fmt
.
Println
(
"Prompt template failed:"
,
err
.
Error
())
c
.
JSON
(
http
.
StatusInternalServerError
,
gin
.
H
{
"error"
:
err
.
Error
()
}
)
return
return
}
}
...
...
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