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
d9e60f63
Unverified
Commit
d9e60f63
authored
Dec 12, 2023
by
Patrick Devine
Committed by
GitHub
Dec 12, 2023
Browse files
add image support to the chat api (#1490)
parent
4251b342
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
10 deletions
+14
-10
api/types.go
api/types.go
+3
-2
server/images.go
server/images.go
+9
-7
server/routes.go
server/routes.go
+2
-1
No files found.
api/types.go
View file @
d9e60f63
...
...
@@ -59,6 +59,7 @@ type ChatRequest struct {
type
Message
struct
{
Role
string
`json:"role"`
// one of ["system", "user", "assistant"]
Content
string
`json:"content"`
Images
[]
ImageData
`json:"images, omitempty"`
}
type
ChatResponse
struct
{
...
...
server/images.go
View file @
d9e60f63
...
...
@@ -86,9 +86,10 @@ func (m *Model) Prompt(p PromptVars) (string, error) {
return
prompt
.
String
(),
nil
}
func
(
m
*
Model
)
ChatPrompt
(
msgs
[]
api
.
Message
)
(
string
,
error
)
{
func
(
m
*
Model
)
ChatPrompt
(
msgs
[]
api
.
Message
)
(
string
,
[]
api
.
ImageData
,
error
)
{
// build the prompt from the list of messages
var
prompt
strings
.
Builder
var
currentImages
[]
api
.
ImageData
currentVars
:=
PromptVars
{
First
:
true
,
}
...
...
@@ -108,35 +109,36 @@ func (m *Model) ChatPrompt(msgs []api.Message) (string, error) {
case
"system"
:
if
currentVars
.
System
!=
""
{
if
err
:=
writePrompt
();
err
!=
nil
{
return
""
,
err
return
""
,
nil
,
err
}
}
currentVars
.
System
=
msg
.
Content
case
"user"
:
if
currentVars
.
Prompt
!=
""
{
if
err
:=
writePrompt
();
err
!=
nil
{
return
""
,
err
return
""
,
nil
,
err
}
}
currentVars
.
Prompt
=
msg
.
Content
currentImages
=
msg
.
Images
case
"assistant"
:
currentVars
.
Response
=
msg
.
Content
if
err
:=
writePrompt
();
err
!=
nil
{
return
""
,
err
return
""
,
nil
,
err
}
default
:
return
""
,
fmt
.
Errorf
(
"invalid role: %s, role must be one of [system, user, assistant]"
,
msg
.
Role
)
return
""
,
nil
,
fmt
.
Errorf
(
"invalid role: %s, role must be one of [system, user, assistant]"
,
msg
.
Role
)
}
}
// Append the last set of vars if they are non-empty
if
currentVars
.
Prompt
!=
""
||
currentVars
.
System
!=
""
{
if
err
:=
writePrompt
();
err
!=
nil
{
return
""
,
err
return
""
,
nil
,
err
}
}
return
prompt
.
String
(),
nil
return
prompt
.
String
(),
currentImages
,
nil
}
type
ManifestV2
struct
{
...
...
server/routes.go
View file @
d9e60f63
...
...
@@ -994,7 +994,7 @@ func ChatHandler(c *gin.Context) {
checkpointLoaded
:=
time
.
Now
()
prompt
,
err
:=
model
.
ChatPrompt
(
req
.
Messages
)
prompt
,
images
,
err
:=
model
.
ChatPrompt
(
req
.
Messages
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
err
.
Error
()})
return
...
...
@@ -1037,6 +1037,7 @@ func ChatHandler(c *gin.Context) {
Format
:
req
.
Format
,
CheckpointStart
:
checkpointStart
,
CheckpointLoaded
:
checkpointLoaded
,
Images
:
images
,
}
if
err
:=
loaded
.
runner
.
Predict
(
c
.
Request
.
Context
(),
predictReq
,
fn
);
err
!=
nil
{
ch
<-
gin
.
H
{
"error"
:
err
.
Error
()}
...
...
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