File tree 3 files changed +37
-0
lines changed
3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1
1
# ellmer (development version)
2
2
3
+ * ` chat_gemini() ` can now handle responses that include citation metadata
4
+ (#358 ).
5
+
3
6
* ` live_browser() ` now initializes ` shinychat::chat_ui() ` with the messages from
4
7
the chat turns, rather than replaying the turns server-side (#381 ).
5
8
Original file line number Diff line number Diff line change @@ -325,6 +325,10 @@ merge_optional <- function(merge_func) {
325
325
function (left , right , path = NULL ) {
326
326
if (is.null(left ) && is.null(right )) {
327
327
NULL
328
+ } else if (is.null(left )) {
329
+ right
330
+ } else if (is.null(right )) {
331
+ left
328
332
} else {
329
333
merge_func(left , right , path )
330
334
}
@@ -405,6 +409,7 @@ merge_parts <- function() {
405
409
}
406
410
407
411
# Put it all together...
412
+ # https://ai.google.dev/api/generate-content#v1beta.GenerateContentResponse
408
413
merge_gemini_chunks <- merge_objects(
409
414
candidates = merge_candidate_lists(
410
415
content = merge_objects(
Original file line number Diff line number Diff line change @@ -76,3 +76,32 @@ test_that("can merge text output", {
76
76
))
77
77
expect_equal(out $ candidates [[1 ]]$ finishReason , " STOP" )
78
78
})
79
+
80
+ test_that(" can handle citations" , {
81
+ # based on "Write me a 5-paragraph essay on the history of the tidyverse."
82
+ messages <- c(
83
+ ' {"candidates": [{"content": {"parts": [{"text": "a"}]}, "role": "model"}]}' ,
84
+ ' {"candidates": [{
85
+ "content": {"parts": [{"text": "a"}]},
86
+ "role": "model",
87
+ "citationMetadata": {
88
+ "citationSources": [
89
+ {
90
+ "startIndex": 1,
91
+ "endIndex": 2,
92
+ "uri": "https://example.com",
93
+ "license": ""
94
+ }
95
+ ]
96
+ }
97
+ }]}'
98
+ )
99
+ chunks <- lapply(messages , jsonlite :: parse_json )
100
+
101
+ out <- merge_gemini_chunks(chunks [[1 ]], chunks [[2 ]])
102
+ source <- out $ candidates [[1 ]]$ citationMetadata $ citationSources [[1 ]]
103
+ expect_equal(source $ startIndex , 1 )
104
+ expect_equal(source $ endIndex , 2 )
105
+ expect_equal(source $ uri , " https://example.com" )
106
+ expect_equal(source $ license , " " )
107
+ })
You can’t perform that action at this time.
0 commit comments