Skip to content

Commit

Permalink
Add sources with chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Oct 21, 2024
1 parent 669cd69 commit c2d03eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ 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": "sources", "content": [{"doc_id": "827f0fbb928b375cc457c732a4013aa7", "doctype": "io.cozy.files"}]}}}
server > {"event": "CREATED",
"payload": {"id": "eb17c3205bf1013ddea018c04daba326",
"type": "io.cozy.ai.chat.events",
Expand Down
28 changes: 20 additions & 8 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,6 +150,7 @@ 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":
Expand All @@ -161,6 +164,14 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
}
delta.SetID(msg.ID)
go realtime.GetHub().Publish(inst, realtime.EventCreate, &delta, nil)
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:
// We can ignore done events
}
Expand All @@ -174,6 +185,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 c2d03eb

Please sign in to comment.