From a6a308fdcb8cab1bdf804b93b2b1892348c75d25 Mon Sep 17 00:00:00 2001 From: J S <49557684+svilupp@users.noreply.github.com> Date: Mon, 6 May 2024 20:07:07 +0100 Subject: [PATCH] add meta-prompting --- CHANGELOG.md | 16 ++++- Project.toml | 2 + app.jl | 96 +++++++++++++++++++------ src/ProToPortal.jl | 10 ++- src/llm.jl | 26 +++++-- src/meta_prompting.jl | 134 +++++++++++++++++++++++++++++++++++ src/serialization.jl | 23 ++++++ src/utils.jl | 2 +- src/view.jl | 113 +++++++++++++++++------------ src/view_chat.jl | 2 +- src/view_meta.jl | 75 ++++++++++++++++++++ templates/MetaExpertAsk.json | 1 + 12 files changed, 423 insertions(+), 77 deletions(-) create mode 100644 src/meta_prompting.jl create mode 100644 src/serialization.jl create mode 100644 src/view_meta.jl create mode 100644 templates/MetaExpertAsk.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b12d54..42b2c4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- First iteration of the GUI -### Fixed \ No newline at end of file +Chat tab +- added delete icon to the last message in the conversation (for easy deletion) +- added a button in "Advanced Settings" to "Fork a conversation" (save to history for reference, but continue from a fresh copy) +- added a focus when a template is selected (on expand, on template selection, etc) + +Meta-prompting tab +- Add an experimental meta-prompting experience based on [arxiv](https://arxiv.org/pdf/2401.12954). See the tab "Meta-Prompting" for more details. + +### Fixed + +## [0.1.0] + +### Added +- The first iteration of the GUI released \ No newline at end of file diff --git a/Project.toml b/Project.toml index 72155f1..193b77d 100644 --- a/Project.toml +++ b/Project.toml @@ -4,12 +4,14 @@ authors = ["J S <49557684+svilupp@users.noreply.github.com> and contributors"] version = "0.2.0-DEV" [deps] +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" GenieFramework = "a59fdf5c-6bf0-4f5d-949c-a137c9e2f353" GenieSession = "03cc5b98-4f21-4eb6-99f2-22eced81f962" PromptingTools = "670122d1-24a8-4d70-bfce-740807c42192" [compat] Aqua = "0.7" +Dates = "<0.0.1, 1" GenieFramework = "2.1" GenieSession = "1" PromptingTools = "0.23" diff --git a/app.jl b/app.jl index 5b14e07..34932d9 100644 --- a/app.jl +++ b/app.jl @@ -41,6 +41,9 @@ const HISTORY_SAVE = get(ENV, "PROTO_HISTORY_SAVE", true) @in selected_page = "chat" @in ministate = true # configuration + ## @in chat_tracker_tokens_in = 0 + ## @in chat_tracker_tokens_out = 0 + ## @in chat_tracker_cost = 0.0 @in model = isempty(PT.GROQ_API_KEY) ? "gpt4t" : "gllama370" @in model_input = "" @in model_submit = false @@ -80,6 +83,15 @@ const HISTORY_SAVE = get(ENV, "PROTO_HISTORY_SAVE", true) @in chat_reset = false @in chat_rm_last_msg = false @in chat_fork = false + # Meta Prompting + @in meta_submit = false + @in meta_reset = false + @in meta_disabled = false + @in meta_question = "" + @in meta_rounds_max = 5 + @in meta_rounds_current = 0 + @in meta_displayed = Dict{Symbol, Any}[] + @in meta_rm_last_msg = false # Template browser @in template_filter = "" @in template_submit = false @@ -118,23 +130,10 @@ const HISTORY_SAVE = get(ENV, "PROTO_HISTORY_SAVE", true) end @onbutton chat_reset begin @info "> Chat Reset!" - timestamp = Dates.format(now(), "YYYYmmdd_HHMMSS") - name = "Conv. @ $timestamp" - ## update the chat with edits by the user - conv_rendered = render_messages(conv_displayed, chat_template_variables) - # Conv. display is already up-to-date, no need to update it! - label = label_conversation(conv_rendered; model = model) - if HISTORY_SAVE - label_clean = replace(label, r"[:\s\"]+" => "_") |> lowercase - ## save to disk + to chat history - path = joinpath( - HISTORY_DIR, "conversation__$(timestamp)__$(label_clean).json") - PT.save_conversation(path, conv_rendered) - @info "> Chat saved to $path" - end - history = push!(history, - Dict(:name => name, :label => label, :messages => conv_rendered)) - + record = save_conversation( + conv_displayed; save = HISTORY_SAVE, save_path = HISTORY_DIR, + variables = chat_template_variables, model = model) + history = push!(history, record) ## clean the chat conv_displayed = empty!(conv_displayed) chat_template_variables = empty!(chat_template_variables) @@ -142,8 +141,7 @@ const HISTORY_SAVE = get(ENV, "PROTO_HISTORY_SAVE", true) chat_disabled, chat_advanced_expanded, chat_template_expanded = false, false, false # set defaults again chat_code_airetry, chat_code_eval = false, false - chat_code_prefix = "" - chat_temperature = 0.7 + chat_code_prefix, chat_temperature = "", 0.7 end @onbutton chat_submit begin chat_disabled = true @@ -233,6 +231,47 @@ const HISTORY_SAVE = get(ENV, "PROTO_HISTORY_SAVE", true) chat_reset = true conv_displayed = conv_displayed_temp end + ### Meta-prompting + @onbutton meta_submit begin + meta_disabled = true + if meta_rounds_current < meta_rounds_max + # we skip prepare_conversation to avoid create user+system prompt when we start, just grab the messages + conv_current = render_messages(meta_displayed) + while meta_rounds_current < meta_rounds_max + meta_rounds_current = meta_rounds_current + 1 + ## update conv, but indicate if it's final_answer + early_stop, conv_current = meta_prompt_step!( + conv_current; counter = meta_rounds_current, model = model, question = meta_question) + meta_displayed = [msg2display(msg; id) + for (id, msg) in enumerate(conv_current)] + early_stop && break + end + elseif meta_question != "" + @info "> Meta-prompting follow up question!" + conv = prepare_conversation(meta_displayed; question = meta_question) + conv_current = send_to_model(conv; model = model) + meta_displayed = [msg2display(msg; id) + for (id, msg) in enumerate(conv_current)] + end + meta_disabled, meta_question = false, "" + end + @onbutton meta_reset begin + @info "> Meta-Prompting Reset!" + record = save_conversation( + meta_displayed; save = HISTORY_SAVE, save_path = HISTORY_DIR, + model = model, file_prefix = "conversation__meta") + history = push!(history, record) + ## clean the messages + meta_rounds_current = 0 + meta_displayed = empty!(meta_displayed) + meta_disabled, meta_question, meta_rounds_current = false, "", 0 + end + @onbutton meta_rm_last_msg begin + @info "> Deleting last turn!" + meta_rounds_current = meta_rounds_current - 1 + pop!(meta_displayed) + meta_displayed = meta_displayed + end ### Template browsing behavior @onbutton template_submit begin @info "> Template filter: $template_filter" @@ -296,7 +335,7 @@ end this.$refs.tpl_select.focus(); }); }, - filterFn (val, update) { + filterFn(val, update) { if (val === '') { update(() => { // reset to full option list @@ -310,7 +349,7 @@ end this.chat_template_options = this.chat_template_options_all.filter(v => v.toLowerCase().indexOf(needle) > -1) }) }, - filterFnAuto (val, update) { + filterFnAuto(val, update) { if (val === '') { update(() => { // reset to full option list @@ -325,8 +364,7 @@ end this.chat_auto_template_options = this.chat_auto_template_options_all.filter(v => v.toLowerCase().indexOf(needle) > -1) }) }, - copyToClipboard: function(index) { - console.log(index); + copyToClipboard(index) { const str = this.conv_displayed[index].content; // extract the content of the element in position `index` const el = document.createElement('textarea'); // Create a