Skip to content

Commit 87c56b3

Browse files
committed
Fix ambiguities
1 parent ef778d1 commit 87c56b3

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

src/Experimental/RAGTools/generation.jl

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ context = build_context(ContextEnumerator(), index, candidates; chunks_window_ma
3737
```
3838
"""
3939
function build_context(contexter::ContextEnumerator,
40-
index::AbstractManagedIndex,
41-
candidates::AbstractCandidateWithChunks;
40+
index::AbstractDocumentIndex,
41+
candidates::AbstractCandidateChunks;
4242
verbose::Bool = true,
4343
chunks_window_margin::Tuple{Int, Int} = (1, 1), kwargs...)
4444
## Checks
@@ -99,7 +99,12 @@ end
9999

100100
# Mutating version that dispatches on the result to the underlying implementation
101101
function build_context!(contexter::ContextEnumerator,
102-
index::Union{AbstractDocumentIndex, AbstractManagedIndex}, result::AbstractRAGResult; kwargs...)
102+
index::AbstractDocumentIndex, result::AbstractRAGResult; kwargs...)
103+
result.context = build_context(contexter, index, result.reranked_candidates; kwargs...)
104+
return result
105+
end
106+
function build_context!(contexter::ContextEnumerator,
107+
index::AbstractManagedIndex, result::AbstractRAGResult; kwargs...)
103108
result.context = build_context(contexter, index, result.reranked_candidates; kwargs...)
104109
return result
105110
end
@@ -144,7 +149,32 @@ Generates an answer using the `aigenerate` function with the provided `result.co
144149
145150
"""
146151
function answer!(
147-
answerer::SimpleAnswerer, index::Union{AbstractDocumentIndex, AbstractManagedIndex}, result::AbstractRAGResult;
152+
answerer::SimpleAnswerer, index::AbstractDocumentIndex, result::AbstractRAGResult;
153+
model::AbstractString = PT.MODEL_CHAT, verbose::Bool = true,
154+
template::Symbol = :RAGAnswerFromContext,
155+
cost_tracker = Threads.Atomic{Float64}(0.0),
156+
kwargs...)
157+
## Checks
158+
placeholders = only(aitemplates(template)).variables # only one template should be found
159+
@assert (:question in placeholders)&&(:context in placeholders) "Provided RAG Template $(template) is not suitable. It must have placeholders: `question` and `context`."
160+
##
161+
(; context, question) = result
162+
conv = aigenerate(template; question,
163+
context = join(context, "\n\n"), model, verbose = false,
164+
return_all = true,
165+
kwargs...)
166+
msg = conv[end]
167+
result.answer = strip(msg.content)
168+
result.conversations[:answer] = conv
169+
## Increment the cost tracker
170+
Threads.atomic_add!(cost_tracker, msg.cost)
171+
verbose &&
172+
@info "Done generating the answer. Cost: \$$(round(msg.cost,digits=3))"
173+
174+
return result
175+
end
176+
function answer!(
177+
answerer::SimpleAnswerer, index::AbstractManagedIndex, result::AbstractRAGResult;
148178
model::AbstractString = PT.MODEL_CHAT, verbose::Bool = true,
149179
template::Symbol = :RAGAnswerFromContext,
150180
cost_tracker = Threads.Atomic{Float64}(0.0),

src/Experimental/RAGTools/preparation.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,12 +741,12 @@ Builds a `PineconeIndex` containing a Pinecone context (API key, index and names
741741
function build_index(
742742
indexer::PineconeIndexer,
743743
context::Pinecone.PineconeContextv3 = Pinecone.init_v3(""),
744-
index::Pinecone.PineconeIndexv3 = "",
745-
namespace::AbstractString,
744+
index::Pinecone.PineconeIndexv3 = nothing,
745+
namespace::AbstractString = "",
746746
verbose::Integer = 1,
747747
index_id = gensym(namespace),
748748
cost_tracker = Threads.Atomic{Float64}(0.0))
749-
@assert !isempty(context.api_key) && !isempty(index) "Pinecone context and index not set"
749+
@assert !isempty(context.apikey) && !isnothing(index) "Pinecone context and index not set"
750750

751751
# TODO: add chunking, embedding, tags?
752752

src/Experimental/RAGTools/rag_interface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ abstract type AbstractTagger <: AbstractIndexingMethod end
138138
### Index itself - return type of `build_index`
139139
abstract type AbstractDocumentIndex end
140140

141+
abstract type AbstractManagedIndex end
142+
141143
"""
142144
AbstractMultiIndex <: AbstractDocumentIndex
143145

src/Experimental/RAGTools/retrieval.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,20 @@ function rerank(reranker::AbstractReranker,
734734
end
735735

736736
function rerank(reranker::NoReranker,
737-
index::Union{AbstractDocumentIndex, AbstractManagedIndex},
737+
index::AbstractDocumentIndex,
738+
question::AbstractString,
739+
candidates::AbstractCandidateChunks;
740+
top_n::Integer = length(candidates),
741+
kwargs...)
742+
# Since this is almost a passthrough strategy, it returns the candidate_chunks unchanged
743+
# but it truncates to `top_n` if necessary
744+
return first(candidates, top_n)
745+
end
746+
747+
function rerank(reranker::NoReranker,
748+
index::AbstractManagedIndex,
738749
question::AbstractString,
739-
candidates::Union{AbstractCandidateChunks, AbstractCandidateWithChunks};
750+
candidates::AbstractCandidateWithChunks;
740751
top_n::Integer = length(candidates),
741752
kwargs...)
742753
# Since this is almost a passthrough strategy, it returns the candidate_chunks unchanged

src/Experimental/RAGTools/types.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ chunkdata(index::ChunkEmbeddingsIndex) = embeddings(index)
136136
# For backward compatibility
137137
const ChunkIndex = ChunkEmbeddingsIndex
138138

139-
abstract type AbstractManagedIndex end
140139
indexid(index::AbstractManagedIndex) = index.id
141140

142141
using Pinecone: Pinecone, PineconeContextv3, PineconeIndexv3

0 commit comments

Comments
 (0)