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
7804b8fa
Unverified
Commit
7804b8fa
authored
Oct 12, 2023
by
Bruce MacDonald
Committed by
GitHub
Oct 12, 2023
Browse files
validate api options fields from map (#711)
parent
56497663
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
1 deletion
+13
-1
api/types.go
api/types.go
+9
-0
server/routes.go
server/routes.go
+4
-1
No files found.
api/types.go
View file @
7804b8fa
...
...
@@ -205,6 +205,8 @@ type Options struct {
NumThread
int
`json:"num_thread,omitempty"`
}
var
ErrInvalidOpts
=
fmt
.
Errorf
(
"invalid options"
)
func
(
opts
*
Options
)
FromMap
(
m
map
[
string
]
interface
{})
error
{
valueOpts
:=
reflect
.
ValueOf
(
opts
)
.
Elem
()
// names of the fields in the options struct
typeOpts
:=
reflect
.
TypeOf
(
opts
)
.
Elem
()
// types of the fields in the options struct
...
...
@@ -218,6 +220,7 @@ func (opts *Options) FromMap(m map[string]interface{}) error {
}
}
invalidOpts
:=
[]
string
{}
for
key
,
val
:=
range
m
{
if
opt
,
ok
:=
jsonOpts
[
key
];
ok
{
field
:=
valueOpts
.
FieldByName
(
opt
.
Name
)
...
...
@@ -281,8 +284,14 @@ func (opts *Options) FromMap(m map[string]interface{}) error {
return
fmt
.
Errorf
(
"unknown type loading config params: %v"
,
field
.
Kind
())
}
}
}
else
{
invalidOpts
=
append
(
invalidOpts
,
key
)
}
}
if
len
(
invalidOpts
)
>
0
{
return
fmt
.
Errorf
(
"%w: %v"
,
ErrInvalidOpts
,
strings
.
Join
(
invalidOpts
,
", "
))
}
return
nil
}
...
...
server/routes.go
View file @
7804b8fa
...
...
@@ -68,7 +68,6 @@ func load(ctx context.Context, workDir string, model *Model, reqOpts map[string]
}
if
err
:=
opts
.
FromMap
(
reqOpts
);
err
!=
nil
{
log
.
Printf
(
"could not merge model options: %v"
,
err
)
return
err
}
...
...
@@ -186,6 +185,10 @@ func GenerateHandler(c *gin.Context) {
// TODO: set this duration from the request if specified
sessionDuration
:=
defaultSessionDuration
if
err
:=
load
(
c
.
Request
.
Context
(),
workDir
,
model
,
req
.
Options
,
sessionDuration
);
err
!=
nil
{
if
errors
.
Is
(
err
,
api
.
ErrInvalidOpts
)
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
err
.
Error
()})
return
}
c
.
JSON
(
http
.
StatusInternalServerError
,
gin
.
H
{
"error"
:
err
.
Error
()})
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