Skip to content

Commit

Permalink
Add sources with chat messages (#4478)
Browse files Browse the repository at this point in the history
  • Loading branch information
nono authored Oct 23, 2024
2 parents 669cd69 + a67bdcb commit 232c522
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
8 changes: 8 additions & 0 deletions docs/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ server > {"event": "CREATED",
"type": "io.cozy.ai.chat.events",
"doc": {"object": "delta", "content": "sky ", "position": 1}}}
[...]
server > {"event": "CREATED",
"payload": {"id": "eb17c3205bf1013ddea018c04daba326",
"type": "io.cozy.ai.chat.events",
"doc": {"object": "generated"}}}
server > {"event": "CREATED",
"payload": {"id": "eb17c3205bf1013ddea018c04daba326",
"type": "io.cozy.ai.chat.events",
"doc": {"object": "sources", "content": [{"id": "827f0fbb928b375cc457c732a4013aa7", "doctype": "io.cozy.files"}]}}}
server > {"event": "CREATED",
"payload": {"id": "eb17c3205bf1013ddea018c04daba326",
"type": "io.cozy.ai.chat.events",
Expand Down
39 changes: 28 additions & 11 deletions model/rag/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ type ChatConversation struct {
}

type ChatMessage struct {
ID string `json:"id"`
Role string `json:"role"`
Content string `json:"content"`
CreatedAt time.Time `json:"createdAt"`
ID string `json:"id"`
Role string `json:"role"`
Content string `json:"content"`
Sources []interface{} `json:"sources,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}

const (
Expand Down Expand Up @@ -130,10 +131,11 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
myself, _ := contact.GetMyself(inst)
relatives, _ := getRelatives(inst, myself)
payload := map[string]interface{}{
"messages": chat.Messages,
"myself": myself,
"relatives": relatives,
"stream": true,
"messages": chat.Messages,
"myself": myself,
"relatives": relatives,
"stream": true,
"with_sources": true,
}

res, err := callRAGQuery(inst, payload)
Expand All @@ -148,9 +150,10 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
msg := chat.Messages[len(chat.Messages)-1]
position := 0
var completion string
var sources []interface{}
err = foreachSSE(res.Body, func(event map[string]interface{}) {
switch event["object"] {
case "delta", "done":
case "delta":
event["position"] = position
position++
content, _ := event["content"].(string)
Expand All @@ -161,8 +164,21 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
}
delta.SetID(msg.ID)
go realtime.GetHub().Publish(inst, realtime.EventCreate, &delta, nil)
default:
// We can ignore done events
case "sources":
sources, _ = event["content"].([]interface{})
doc := couchdb.JSONDoc{
Type: consts.ChatEvents,
M: event,
}
doc.SetID(msg.ID)
go realtime.GetHub().Publish(inst, realtime.EventCreate, &doc, nil)
default: // done, generated, etc.
doc := couchdb.JSONDoc{
Type: consts.ChatEvents,
M: event,
}
doc.SetID(msg.ID)
go realtime.GetHub().Publish(inst, realtime.EventCreate, &doc, nil)
}
})
if err != nil {
Expand All @@ -174,6 +190,7 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
ID: uuidv7.String(),
Role: AssistantRole,
Content: completion,
Sources: sources,
CreatedAt: time.Now().UTC(),
}
chat.Messages = append(chat.Messages, answer)
Expand Down

0 comments on commit 232c522

Please sign in to comment.