@@ -37,7 +37,8 @@ context = build_context(ContextEnumerator(), index, candidates; chunks_window_ma
37
37
```
38
38
"""
39
39
function build_context (contexter:: ContextEnumerator ,
40
- index:: AbstractDocumentIndex , candidates:: AbstractCandidateChunks ;
40
+ index:: AbstractManagedIndex ,
41
+ candidates:: AbstractCandidateWithChunks ;
41
42
verbose:: Bool = true ,
42
43
chunks_window_margin:: Tuple{Int, Int} = (1 , 1 ), kwargs... )
43
44
# # Checks
@@ -63,27 +64,31 @@ function build_context(contexter::ContextEnumerator,
63
64
return context
64
65
end
65
66
66
- using Pinecone: Pinecone, query
67
- using JSON3: JSON3, read
68
- """
69
- build_context(contexter::ContextEnumerator,
70
- index::AbstractPTPineconeIndex;
71
- verbose::Bool = true,
72
- top_k::Int = 10,
73
- kwargs...)
74
-
75
- Build context strings by querying Pinecone.
76
- ```
77
- """
78
67
function build_context (contexter:: ContextEnumerator ,
79
- index:: AbstractPTPineconeIndex ;
68
+ index:: AbstractManagedIndex ,
69
+ candidates:: AbstractCandidateWithChunks ;
80
70
verbose:: Bool = true ,
81
- top_k:: Int = 10 ,
82
- kwargs... )
83
- pinecone_results = Pinecone. query (index. pinecone_context, index. pinecone_index, index. embedding, top_k, index. namespace, false , true )
84
- results_json = JSON3. read (pinecone_results)
85
- context = results_json. matches[1 ]. metadata. content
71
+ chunks_window_margin:: Tuple{Int, Int} = (1 , 1 ), kwargs... )
72
+ # # Checks
73
+ @assert chunks_window_margin[1 ] >= 0 && chunks_window_margin[2 ] >= 0 " Both `chunks_window_margin` values must be non-negative"
74
+
75
+ context = String[]
76
+ for (i, position) in enumerate (positions (candidates))
77
+ # # select the right index
78
+ id = candidates isa MultiCandidateChunks ? candidates. index_ids[i] :
79
+ candidates. index_id
80
+ index_ = index isa AbstractChunkIndex ? index : index[id]
81
+ isnothing (index_) && continue
86
82
83
+ chunks_ = chunks (candidates)[
84
+ max (1 , position - chunks_window_margin[1 ]): min (end ,
85
+ position + chunks_window_margin[2 ])]
86
+ # # Check if surrounding chunks are from the same source
87
+ is_same_source = sources (candidates)[
88
+ max (1 , position - chunks_window_margin[1 ]): min (end ,
89
+ position + chunks_window_margin[2 ])] .== sources (candidates)[position]
90
+ push! (context, " $(i) . $(join (chunks_[is_same_source], " \n " )) " )
91
+ end
87
92
return context
88
93
end
89
94
94
99
95
100
# Mutating version that dispatches on the result to the underlying implementation
96
101
function build_context! (contexter:: ContextEnumerator ,
97
- index:: AbstractDocumentIndex , result:: AbstractRAGResult ; kwargs... )
102
+ index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ; kwargs... )
98
103
result. context = build_context (contexter, index, result. reranked_candidates; kwargs... )
99
104
return result
100
105
end
@@ -114,6 +119,7 @@ function answer!(
114
119
throw (ArgumentError (" Answerer $(typeof (answerer)) not implemented" ))
115
120
end
116
121
122
+ # TODO : update docs signature
117
123
"""
118
124
answer!(
119
125
answerer::SimpleAnswerer, index::AbstractDocumentIndex, result::AbstractRAGResult;
@@ -138,7 +144,7 @@ Generates an answer using the `aigenerate` function with the provided `result.co
138
144
139
145
"""
140
146
function answer! (
141
- answerer:: SimpleAnswerer , index:: AbstractDocumentIndex , result:: AbstractRAGResult ;
147
+ answerer:: SimpleAnswerer , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ;
142
148
model:: AbstractString = PT. MODEL_CHAT, verbose:: Bool = true ,
143
149
template:: Symbol = :RAGAnswerFromContext ,
144
150
cost_tracker = Threads. Atomic {Float64} (0.0 ),
@@ -186,11 +192,13 @@ Refines the answer by executing a web search using the Tavily API. This method a
186
192
struct TavilySearchRefiner <: AbstractRefiner end
187
193
188
194
function refine! (
189
- refiner:: AbstractRefiner , index:: AbstractDocumentIndex , result:: AbstractRAGResult ;
195
+ refiner:: AbstractRefiner , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ;
190
196
kwargs... )
191
197
throw (ArgumentError (" Refiner $(typeof (refiner)) not implemented" ))
192
198
end
193
199
200
+
201
+ # TODO : update docs signature
194
202
"""
195
203
refine!(
196
204
refiner::NoRefiner, index::AbstractChunkIndex, result::AbstractRAGResult;
199
207
Simple no-op function for `refine!`. It simply copies the `result.answer` and `result.conversations[:answer]` without any changes.
200
208
"""
201
209
function refine! (
202
- refiner:: NoRefiner , index:: AbstractDocumentIndex , result:: AbstractRAGResult ;
210
+ refiner:: NoRefiner , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ;
203
211
kwargs... )
204
212
result. final_answer = result. answer
205
213
if haskey (result. conversations, :answer )
@@ -208,6 +216,8 @@ function refine!(
208
216
return result
209
217
end
210
218
219
+
220
+ # TODO : update docs signature
211
221
"""
212
222
refine!(
213
223
refiner::SimpleRefiner, index::AbstractDocumentIndex, result::AbstractRAGResult;
@@ -234,7 +244,7 @@ This method uses the same context as the original answer, however, it can be mod
234
244
- `cost_tracker`: An atomic counter to track the cost of the operation.
235
245
"""
236
246
function refine! (
237
- refiner:: SimpleRefiner , index:: AbstractDocumentIndex , result:: AbstractRAGResult ;
247
+ refiner:: SimpleRefiner , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ;
238
248
verbose:: Bool = true ,
239
249
model:: AbstractString = PT. MODEL_CHAT,
240
250
template:: Symbol = :RAGAnswerRefiner ,
@@ -262,6 +272,8 @@ function refine!(
262
272
return result
263
273
end
264
274
275
+
276
+ # TODO : update docs signature
265
277
"""
266
278
refine!(
267
279
refiner::TavilySearchRefiner, index::AbstractDocumentIndex, result::AbstractRAGResult;
@@ -312,7 +324,7 @@ pprint(result)
312
324
```
313
325
"""
314
326
function refine! (
315
- refiner:: TavilySearchRefiner , index:: AbstractDocumentIndex , result:: AbstractRAGResult ;
327
+ refiner:: TavilySearchRefiner , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ;
316
328
verbose:: Bool = true ,
317
329
model:: AbstractString = PT. MODEL_CHAT,
318
330
include_answer:: Bool = true ,
@@ -377,13 +389,13 @@ Overload this method to add custom postprocessing steps, eg, logging, saving con
377
389
"""
378
390
struct NoPostprocessor <: AbstractPostprocessor end
379
391
380
- function postprocess! (postprocessor:: AbstractPostprocessor , index:: AbstractDocumentIndex ,
392
+ function postprocess! (postprocessor:: AbstractPostprocessor , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} ,
381
393
result:: AbstractRAGResult ; kwargs... )
382
394
throw (ArgumentError (" Postprocessor $(typeof (postprocessor)) not implemented" ))
383
395
end
384
396
385
397
function postprocess! (
386
- :: NoPostprocessor , index:: AbstractDocumentIndex , result:: AbstractRAGResult ; kwargs... )
398
+ :: NoPostprocessor , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ; kwargs... )
387
399
return result
388
400
end
389
401
@@ -416,6 +428,7 @@ It uses `ContextEnumerator`, `SimpleAnswerer`, `SimpleRefiner`, and `NoPostproce
416
428
postprocessor:: AbstractPostprocessor = NoPostprocessor ()
417
429
end
418
430
431
+ # TODO : update docs signature
419
432
"""
420
433
generate!(
421
434
generator::AbstractGenerator, index::AbstractDocumentIndex, result::AbstractRAGResult;
@@ -483,7 +496,7 @@ result = generate!(index, result)
483
496
```
484
497
"""
485
498
function generate! (
486
- generator:: AbstractGenerator , index:: AbstractDocumentIndex , result:: AbstractRAGResult ;
499
+ generator:: AbstractGenerator , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} , result:: AbstractRAGResult ;
487
500
verbose:: Integer = 1 ,
488
501
api_kwargs:: NamedTuple = NamedTuple (),
489
502
contexter:: AbstractContextBuilder = generator. contexter,
@@ -672,7 +685,7 @@ PT.pprint(result)
672
685
673
686
For easier manipulation of nested kwargs, see utilities `getpropertynested`, `setpropertynested`, `merge_kwargs_nested`.
674
687
"""
675
- function airag (cfg:: AbstractRAGConfig , index:: AbstractDocumentIndex ;
688
+ function airag (cfg:: AbstractRAGConfig , index:: Union{ AbstractDocumentIndex, AbstractManagedIndex} ;
676
689
question:: AbstractString ,
677
690
verbose:: Integer = 1 , return_all:: Bool = false ,
678
691
api_kwargs:: NamedTuple = NamedTuple (),
0 commit comments