Skip to content

Commit

Permalink
New query composition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sixzero committed Jan 16, 2025
1 parent e7de19d commit 45545dc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
4 changes: 0 additions & 4 deletions archived/file_io/Persistable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ using JLD2
PersistableState(path::String) = (mkpath(path); new(expanduser(abspath(expanduser((path))))))
end



# persist!(conv::ConversationCTX) = save_message(conv)
# save_message(conv::ConversationCTX) = save_conversation_to_file(conv)
get_conversation_filename(p::PersistableState,conv_id::String) = (files = filter(f -> endswith(f, "_$(conv_id).log"), readdir(p.path)); isempty(files) ? nothing : joinpath(p.path, first(files)))

function generate_overview(conv::CONV, conv_id::String, p::PersistableState)
Expand Down
4 changes: 2 additions & 2 deletions playground/questions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ using EasyContext: create_voyage_embedder, create_combined_index_builder
# ReduceGPTReranker(),
# ContextNode(),
# ])
using EasyContext: QuestionCTX
using EasyContext: QueryWithHistory
# CodebaseContextV3(;project_paths=["."]),
# # MultiIndexBuilder(;builders=[
# # EmbeddingIndexBuilder(top_k=50),
Expand All @@ -349,7 +349,7 @@ question = "Now I need you to create a julia script which could use ai to genera
"
question = "Could you please implement me with ReplMaker or ReplMaker.jl an AI chat? The basic point would be what currently the improved_readline gives back in the loop, it should be done by this ReplMaker thing."

question_acc = QuestionCTX()
question_acc = QueryWithHistory()
ctx_question = question_acc(question)
file_chunks = Workspace(["."])(FullFileChunker())
file_chunks_selected = create_combined_index_builder(top_k=30)(file_chunks, ctx_question)
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/CTX_julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function init_julia_context(;
)
end

function process_julia_context(enabled, julia_context::JuliaCTX, ctx_question; age_tracker=nothing, io::Union{IO, Nothing}=nothing)
function process_julia_context(julia_context::JuliaCTX, ctx_question; enabled=true, age_tracker=nothing, io::Union{IO, Nothing}=nothing)
!enabled && return ""
jl_simi_filter = julia_context.jl_simi_filter
jl_pkg_index = julia_context.jl_pkg_index
Expand Down
71 changes: 50 additions & 21 deletions src/contexts/CTX_question.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@

@kwdef mutable struct QuestionCTX
@kwdef mutable struct QueryWithHistory
questions::Vector{String}=String[]
max_questions::Int=4
max_questions::Int=3
end

function (qa::QuestionCTX)(question::AbstractString)
push!(qa.questions, question)
function (qa::QueryWithHistory)(query::AbstractString)
push!(qa.questions, query)
length(qa.questions) > qa.max_questions && popfirst!(qa.questions)

if length(qa.questions) > 1
history = join(["$i. $msg" for (i, msg) in enumerate(qa.questions[1:end-1])], "\n")
return """
<PastUserQuestions>
$history
</PastUserQuestions>
<UserQuestion>
$(length(qa.questions)). $(qa.questions[end])
</UserQuestion>
"""
else
return """
<UserQuestion>
$(qa.questions[end])
</UserQuestion>
"""
history = length(qa.questions) > 1 ? join(["$i. $msg" for (i, msg) in enumerate(qa.questions[1:end-1])], "\n") : ""
current = qa.questions[end]

return (history, current)
end

function format_history_query((history, current)::Tuple{String,String}, extra_contexts::Vector{Pair{String,String}}=Pair{String,String}[])
parts = String[]

# Past user queries first
!isempty(history) && push!(parts, """User query history:\n$history""")

# Extra contexts in the middle
for (title, content) in extra_contexts
!isempty(content) && push!(parts, """$title:\n$content""")
end

# Current query last
push!(parts, """Latest user query:\n$current""")

join(parts, "\n\n")
end

@kwdef mutable struct QueryWithHistoryAndAIMsg
question_history::QueryWithHistory=QueryWithHistory()
max_assistant::Int=1
end

function (conv::QueryWithHistoryAndAIMsg)(query::AbstractString, session::Session, ctx_shell::AbstractString="")
ai_history = String[]

# Get AI history first
for msg in reverse(session.messages)
if msg.role == :assistant && length(ai_history) < conv.max_assistant
pushfirst!(ai_history, msg.content)
end
end

# Build extra contexts
contexts = Pair{String,String}[]
!isempty(ai_history) && push!(contexts, "AI responses" => join(ai_history, "\n"))
!isempty(ctx_shell) && push!(contexts, "Shell context" => ctx_shell)

# Format with all parts in order
format_history_query(conv.question_history(query), contexts)
end

add_response!(conv::QueryWithHistoryAndAIMsg, response::AbstractString) =
push!(conv.messages, create_AI_message(response))

0 comments on commit 45545dc

Please sign in to comment.