-
Notifications
You must be signed in to change notification settings - Fork 5
/
preparation.jl
137 lines (117 loc) · 4.51 KB
/
preparation.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
docdata_to_source(data::AbstractDict)
Creates a source path from a given DocStr record
"""
function docdata_to_source(data::AbstractDict)
linenumber = get(data, :linenumber, 0)
mod = get(data, :module, "-") |> string
func = get(data, :binding, "-") |> string
path = get(data, :path, "-") |> string
string(mod, "::", path, "::", linenumber, "::", func)
end
"""
docextract(d::DocStr, sep::AbstractString = "\n\n")
Extracts the documentation from a DocStr record.
Separates the individual docs within `DocStr` with `sep`.
"""
function docextract(d::DocStr, sep::AbstractString = "\n\n")
io = IOBuffer()
for part in d.text
if part isa String
write(io, part, sep)
# else
# @info d.data[:binding] typeof(part)
# @info part
end
end
docs = String(take!(io))
source = hasproperty(d, :data) ? docdata_to_source(d.data) : ""
return docs, source
end
"""
docextract(d::MultiDoc, sep::AbstractString = "\n\n")
Extracts the documentation from a MultiDoc record (separates the individual docs within `DocStr` with `sep`)
"""
function docextract(d::MultiDoc, sep::AbstractString = "\n\n")
docs, sources = String[], String[]
for v in values(d.docs)
doc, source = docextract(v, sep)
push!(docs, doc)
push!(sources, source)
end
return docs, sources
end
"""
docextract(mod::Module)
Extracts the documentation from a given (loaded) module.
"""
function docextract(mod::Module)
all_docs, all_sources = String[], String[]
# Module doc might be in README.md instead of the META dict
push!(all_docs, doc(mod) |> stripmd)
push!(all_sources, string(nameof(mod), "::", "/README.md", "::", 0, "::", nameof(mod)))
dict = meta(mod; autoinit = false)
isnothing(dict) && return all_docs, all_sources
for (k, v) in dict
docs, sources = docextract(v)
append!(all_docs, docs)
sources = !isnothing(pkgdir(mod)) ? remove_pkgdir.(sources, Ref(mod)) : sources
append!(all_sources, sources)
end
all_docs, all_sources
end
"""
docextract(modules::Vector{Module} = Base.Docs.modules)
Extracts the documentation from a vector of `modules`.
"""
function docextract(modules::Vector{Module} = Base.Docs.modules)
all_docs, all_sources = String[], String[]
for mod in modules
docs, sources = docextract(mod)
append!(all_docs, docs)
append!(all_sources, sources)
end
all_docs, all_sources
end
"""
RT.build_index(mod::Module; verbose::Int = 1, kwargs...)
Build `index` from the documentation of a given module `mod`.
"""
function RT.build_index(mod::Module; verbose::Int = 1, kwargs...)
global RAG_CONFIG, RAG_KWARGS
## Extract docstrings
all_docs, all_sources = docextract(mod)
## Extract current configuration
chunker_kwargs_ = (; sources = all_sources)
chunker_kwargs = haskey(kwargs, :chunker_kwargs) ?
merge(kwargs.chunker_kwargs, chunker_kwargs_) : chunker_kwargs_
embedder_kwargs_ = RT.getpropertynested(
RAG_KWARGS[], [:retriever_kwargs], :embedder_kwargs, nothing)
embedder_kwargs = haskey(kwargs, :embedder_kwargs) ?
merge(kwargs.embedder_kwargs, embedder_kwargs_) : embedder_kwargs_
new_index = RT.build_index(RAG_CONFIG[].indexer, all_docs;
embedder_kwargs, chunker = TextChunker(), chunker_kwargs,
verbose, index_id = nameof(mod), kwargs...)
end
"""
RT.build_index(modules::Vector{Module} = Base.Docs.modules; verbose::Int = 1,
kwargs...)
Build index from the documentation of the currently loaded modules.
If `modules` is empty, it will use all currently loaded modules.
"""
function RT.build_index(modules::Vector{Module} = Base.Docs.modules; verbose::Int = 1,
kwargs...)
global RAG_CONFIG, RAG_KWARGS
all_docs, all_sources = docextract(modules)
## Extract current configuration
chunker_kwargs_ = (; sources = all_sources)
chunker_kwargs = haskey(kwargs, :chunker_kwargs) ?
merge(kwargs.chunker_kwargs, chunker_kwargs_) : chunker_kwargs_
embedder_kwargs_ = RT.getpropertynested(
RAG_KWARGS[], [:retriever_kwargs], :embedder_kwargs, nothing)
embedder_kwargs = haskey(kwargs, :embedder_kwargs) ?
merge(kwargs.embedder_kwargs, embedder_kwargs_) : embedder_kwargs_
new_index = RT.build_index(RAG_CONFIG[].indexer, all_docs;
embedder_kwargs, chunker = TextChunker(), chunker_kwargs,
verbose, index_id = nameof(mod), kwargs...)
end