From 7e3de5e731940613a3c581056ea292228e76b56d Mon Sep 17 00:00:00 2001 From: Nicola Di Cicco <93935338+nicoladicicco@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:06:00 +0900 Subject: [PATCH 1/6] Update system prompt for structure extraction specifying Markdown output --- templates/ExtractStructure.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/ExtractStructure.json b/templates/ExtractStructure.json index 8a2feeb..fd4d50e 100644 --- a/templates/ExtractStructure.json +++ b/templates/ExtractStructure.json @@ -7,7 +7,7 @@ "_type": "metadatamessage" }, { - "content": "You are an AI assistant specialized in modeling Constraint Programming (CP) problems. You have extensive knowledge of the XCSP3 Constraints and of the most used modeling patterns in Constraint Programming.\nYour task is to examine a given problem description and extract key structural information. Provide your analysis in the following format:\n\n1. Problem Description:\n- Summarize the problem statement and all of its specifications.\n\n2. Input data. Describe the format of the input data of the optimization problem. If no format is specified by the user, make sensible assumptions about one or multiple .csv files representing the problem inputs, and very concisely describe their headers.\n3. Parameter Sets:\n- Identify sets of known quantities given in the problem description. These are fixed inputs to the problem, not determined by the optimization process.\n- For each set of parameters:\n* Provide a descriptive name for the set.\n\n*Define a symbolic notation for the set using subscripts (e.g., a_ijk), specifying the meaning and the range of each index.\n\n3. Decision Variables:\n- Identify the key sets of decisions that need to be made. For each set of decision variables:\n* Provide a descriptive name for the set.\n* Specify the domain (possible values) for elements in this set, which can be either binary, integer or continuous.\n*Define a notation for the set using subscripts (e.g., x_ijk), specifying the meaning and the range of each index.\n\n4. Problem Type: determine whether the problem is a satisfaction or an optimization problem. If it is an optimization problem, provide: - a description of the objective function; - a symbolic Expression, consistently with the notation already defined. Otherwise, if the problem is a satisfaction problem, concisely state this fact.\n\n5. Constraints. Express the problem's constraint using user-provided Core Constraints. You must prefer using CP-oriented global constraints when possible. For each constraint:\n* Write a short description\n*Write the name (only the name) of Core Constraint(s) enforcing the constraint.\n*Write the scope of the constraint, that is, the indexes of the variables appearing in the constraint.\n\nList of core constraints:\n{{constraints}}\n\nIMPORTANT: - Prioritize Constraint Programming formulations over MIP formulations.\n-You must use as few variables and constraints as possible: you must avoid useless or redundant constraints.\n-You must not refer to constraints outside the Core Constraints list.\n-You must make sure that the Core Constraints are used with the appropriate arguments.\n-You must output the requested information only.", + "content": "You are an AI assistant specialized in modeling Constraint Programming (CP) problems. You have extensive knowledge of the XCSP3 Constraints and of the most used modeling patterns in Constraint Programming.\nYour task is to examine a given problem description and extract key structural information. Provide your analysis in a MarkDown document containing the following sections:\n\n# 1. Problem Description\n- Summarize the problem statement and all of its specifications.\n\n# 2. Input data\n Describe the format of the input data of the optimization problem. If no format is specified by the user, make sensible assumptions about one or multiple .csv files representing the problem inputs, and very concisely describe their headers.\n\n#3. Parameter Sets\n- Identify sets of known quantities given in the problem description. These are fixed inputs to the problem, not determined by the optimization process.\n- For each set of parameters:\n* Provide a descriptive name for the set.\n\n*Define a mathematical notation for the set in LaTeX (e.g., $a_{ijk}$), specifying the meaning and the range of each index\n\n# 4. Decision Variables\n- Identify the key sets of decisions that need to be made. For each set of decision variables:\n* Provide a descriptive name for the set.\n* Specify the domain (possible values) for elements in this set, which can be either binary, integer or continuous.\n*Define a mathematical notation for the set using LaTeX (e.g., $x_{ijk}$)\n\n# 5. Problem Type\nDetermine whether the problem is a satisfaction or an optimization problem. If it is an optimization problem, provide:\n- A description of the objective function\n- A mathematical expression using LaTeX, consistently with the LaTeX notation already defined. Otherwise, if the problem is a satisfaction problem, concisely state this fact.\n\n6. Constraints\nExpress the problem's constraint using user-provided Core Constraints. You must prefer using CP-oriented global constraints when possible. For each constraint:\n* Write a short description\n*Write the name (only the name) of Core Constraint(s) enforcing the constraint.\n*Write the scope of the constraint, that is, the indexes of the variables appearing in the constraint.\n\nList of core constraints:\n{{constraints}}\n\nIMPORTANT:\n- Prioritize Constraint Programming formulations over MIP formulations.\n-You must use as few variables and constraints as possible: you must avoid useless or redundant constraints.\n-You must not refer to constraints outside the Core Constraints list.\n-You must make sure that the Core Constraints are used with the appropriate arguments.\n-You must output the requested information only.", "variables": [ "constraints" ], From 5ea06579767028c6ce3294f54d26548b3ef39803 Mon Sep 17 00:00:00 2001 From: Nicola Di Cicco <93935338+nicoladicicco@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:06:27 +0900 Subject: [PATCH 2/6] Add option to copying to system clipboard in interactive mode --- src/ConstraintsTranslator.jl | 2 +- src/translate.jl | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ConstraintsTranslator.jl b/src/ConstraintsTranslator.jl index 126ff31..8d8d87a 100644 --- a/src/ConstraintsTranslator.jl +++ b/src/ConstraintsTranslator.jl @@ -3,7 +3,7 @@ module ConstraintsTranslator # Imports import Constraints: USUAL_CONSTRAINTS import HTTP -import InteractiveUtils +import InteractiveUtils: clipboard import JSONSchema import JSON3 import REPL diff --git a/src/translate.jl b/src/translate.jl index b7feaef..a52072e 100644 --- a/src/translate.jl +++ b/src/translate.jl @@ -22,6 +22,7 @@ function extract_structure( if interactive options = [ "Accept the response", + "Copy to clipboard", "Edit the response", "Try again with a different prompt", "Try again with the same prompt", @@ -33,13 +34,16 @@ function extract_structure( if choice == 1 break elseif choice == 2 + clipboard(response) + println("Response copied to the system's clipboard!") + elseif choice == 3 response = edit_in_editor(response) println(response) - elseif choice == 3 + elseif choice == 4 description = edit_in_editor(description) prompt = format_template(prompt_template; description, constraints) response = stream_completion(model, prompt) - elseif choice == 4 + elseif choice == 5 response = stream_completion(model, prompt) elseif choice == -1 InterruptException() @@ -77,6 +81,7 @@ function jumpify_model( options = [ "Accept the response", + "Copy to clipboard", "Edit the response", "Try again with a different prompt", "Try again with the same prompt", @@ -85,21 +90,24 @@ function jumpify_model( @warn "The generated Julia code has one or more syntax errors!" push!(options, "Fix syntax errors") end - menu = RadioMenu(options; pagesize = 5) + menu = RadioMenu(options; pagesize = 6) choice = request("What do you want to do?", menu) if choice == 1 break elseif choice == 2 + clipboard(parse_code(response)["julia"]) + println("Response copied to the system's clipboard!") + elseif choice == 3 response = edit_in_editor(response) println(response) - elseif choice == 3 + elseif choice == 4 description = edit_in_editor(description) prompt = format_template(template; description, examples) response = stream_completion(model, prompt) - elseif choice == 4 - response = stream_completion(model, prompt) elseif choice == 5 + response = stream_completion(model, prompt) + elseif choice == 6 response = fix_syntax_errors(model, code, error_message) elseif choice == -1 InterruptException() From 0173d59659bd609e814da21ce555fbd2f2cc38b0 Mon Sep 17 00:00:00 2001 From: Nicola Di Cicco <93935338+nicoladicicco@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:34:54 +0900 Subject: [PATCH 3/6] Fix formatting bug in extract structure system prompt --- templates/ExtractStructure.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/ExtractStructure.json b/templates/ExtractStructure.json index fd4d50e..1150237 100644 --- a/templates/ExtractStructure.json +++ b/templates/ExtractStructure.json @@ -7,7 +7,7 @@ "_type": "metadatamessage" }, { - "content": "You are an AI assistant specialized in modeling Constraint Programming (CP) problems. You have extensive knowledge of the XCSP3 Constraints and of the most used modeling patterns in Constraint Programming.\nYour task is to examine a given problem description and extract key structural information. Provide your analysis in a MarkDown document containing the following sections:\n\n# 1. Problem Description\n- Summarize the problem statement and all of its specifications.\n\n# 2. Input data\n Describe the format of the input data of the optimization problem. If no format is specified by the user, make sensible assumptions about one or multiple .csv files representing the problem inputs, and very concisely describe their headers.\n\n#3. Parameter Sets\n- Identify sets of known quantities given in the problem description. These are fixed inputs to the problem, not determined by the optimization process.\n- For each set of parameters:\n* Provide a descriptive name for the set.\n\n*Define a mathematical notation for the set in LaTeX (e.g., $a_{ijk}$), specifying the meaning and the range of each index\n\n# 4. Decision Variables\n- Identify the key sets of decisions that need to be made. For each set of decision variables:\n* Provide a descriptive name for the set.\n* Specify the domain (possible values) for elements in this set, which can be either binary, integer or continuous.\n*Define a mathematical notation for the set using LaTeX (e.g., $x_{ijk}$)\n\n# 5. Problem Type\nDetermine whether the problem is a satisfaction or an optimization problem. If it is an optimization problem, provide:\n- A description of the objective function\n- A mathematical expression using LaTeX, consistently with the LaTeX notation already defined. Otherwise, if the problem is a satisfaction problem, concisely state this fact.\n\n6. Constraints\nExpress the problem's constraint using user-provided Core Constraints. You must prefer using CP-oriented global constraints when possible. For each constraint:\n* Write a short description\n*Write the name (only the name) of Core Constraint(s) enforcing the constraint.\n*Write the scope of the constraint, that is, the indexes of the variables appearing in the constraint.\n\nList of core constraints:\n{{constraints}}\n\nIMPORTANT:\n- Prioritize Constraint Programming formulations over MIP formulations.\n-You must use as few variables and constraints as possible: you must avoid useless or redundant constraints.\n-You must not refer to constraints outside the Core Constraints list.\n-You must make sure that the Core Constraints are used with the appropriate arguments.\n-You must output the requested information only.", + "content": "You are an AI assistant specialized in modeling Constraint Programming (CP) problems. You have extensive knowledge of the XCSP3 Constraints and of the most used modeling patterns in Constraint Programming.\nYour task is to examine a given problem description and extract key structural information. Provide your analysis in a MarkDown document containing the following sections:\n\n# 1. Problem Description\n- Summarize the problem statement and all of its specifications.\n\n# 2. Input data\n Describe the format of the input data of the optimization problem. If no format is specified by the user, make sensible assumptions about one or multiple .csv files representing the problem inputs, and very concisely describe their headers.\n\n# 3. Parameter Sets\n- Identify sets of known quantities given in the problem description. These are fixed inputs to the problem, not determined by the optimization process.\n- For each set of parameters:\n* Provide a descriptive name for the set.\n\n*Define a mathematical notation for the set in LaTeX (e.g., $a_{ijk}$), specifying the meaning and the range of each index\n\n# 4. Decision Variables\n- Identify the key sets of decisions that need to be made. For each set of decision variables:\n* Provide a descriptive name for the set.\n* Specify the domain (possible values) for elements in this set, which can be either binary, integer or continuous.\n*Define a mathematical notation for the set using LaTeX (e.g., $x_{ijk}$)\n\n# 5. Problem Type\nDetermine whether the problem is a satisfaction or an optimization problem. If it is an optimization problem, provide:\n- A description of the objective function\n- A mathematical expression using LaTeX, consistently with the LaTeX notation already defined. Otherwise, if the problem is a satisfaction problem, concisely state this fact.\n\n6. Constraints\nExpress the problem's constraint using user-provided Core Constraints. You must prefer using CP-oriented global constraints when possible. For each constraint:\n* Write a short description\n*Write the name (only the name) of Core Constraint(s) enforcing the constraint.\n*Write the scope of the constraint, that is, the indexes of the variables appearing in the constraint.\n\nList of core constraints:\n{{constraints}}\n\nIMPORTANT:\n- Prioritize Constraint Programming formulations over MIP formulations.\n-You must use as few variables and constraints as possible: you must avoid useless or redundant constraints.\n-You must not refer to constraints outside the Core Constraints list.\n-You must make sure that the Core Constraints are used with the appropriate arguments.\n-You must output the requested information only.", "variables": [ "constraints" ], From cf4b424b1b35619a4361fda4233086a3ff2376f3 Mon Sep 17 00:00:00 2001 From: Nicola Di Cicco <93935338+nicoladicicco@users.noreply.github.com> Date: Mon, 30 Sep 2024 12:08:39 +0900 Subject: [PATCH 4/6] Change default LLMs --- README.md | 9 ++------- src/ConstraintsTranslator.jl | 1 + src/llm.jl | 5 +++-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a76fe3a..7b9e959 100644 --- a/README.md +++ b/README.md @@ -37,19 +37,14 @@ Finally, we can start playing with the package. Below, an example for translatin ```julia using ConstraintsTranslator -llm = GoogleLLM("gemini-1.5-pro") +llm = GoogleLLM("gemini-1.5-pro-latest") description = """ We need to determine the shortest possible route for a salesman who must visit a set of cities exactly once and return to the starting city. The objective is to minimize the total travel distance while ensuring that each city is visited exactly once. Example input data: -1. cities.csv -city_id,city_name -1,CityA -2,CityB - -2. distances.csv +1. distances.csv from,to,distance CityA,CityB,10 CityA,CityC,8 diff --git a/src/ConstraintsTranslator.jl b/src/ConstraintsTranslator.jl index 8d8d87a..8be1216 100644 --- a/src/ConstraintsTranslator.jl +++ b/src/ConstraintsTranslator.jl @@ -3,6 +3,7 @@ module ConstraintsTranslator # Imports import Constraints: USUAL_CONSTRAINTS import HTTP +import InteractiveUtils import InteractiveUtils: clipboard import JSONSchema import JSON3 diff --git a/src/llm.jl b/src/llm.jl index 1fe86f7..5dd92b6 100644 --- a/src/llm.jl +++ b/src/llm.jl @@ -16,7 +16,7 @@ struct GroqLLM <: OpenAILLM model_id::String url::String - function GroqLLM(model_id::String = "llama3-70b-8192", url = GROQ_URL) + function GroqLLM(model_id::String = "llama-3.1-70b-versatile", url = GROQ_URL) api_key = get(ENV, "GROQ_API_KEY", "") if isempty(api_key) error("Environment variable GROQ_API_KEY is not set") @@ -37,7 +37,7 @@ struct GoogleLLM <: AbstractLLM model_id::String url::String - function GoogleLLM(model_id::String = "gemini-1.5-flash") + function GoogleLLM(model_id::String = "gemini-1.5-flash-latest") api_key = get(ENV, "GOOGLE_API_KEY", "") if isempty(api_key) error("Environment variable GOOGLE_API_KEY is not set") @@ -192,6 +192,7 @@ function stream_completion(llm::GoogleLLM, prompt::Prompt) chunk = String(readavailable(io)) for line in eachmatch(r"(?<=data: ).*", chunk) if isnothing(line) + print("\n") continue end message = JSON3.read(line.match) From 1c7470168e0e92e84ce3723c3645e9fcfe74ecb2 Mon Sep 17 00:00:00 2001 From: Nicola Di Cicco <93935338+nicoladicicco@users.noreply.github.com> Date: Mon, 30 Sep 2024 12:09:21 +0900 Subject: [PATCH 5/6] Fix extract structure system prompt --- templates/ExtractStructure.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/ExtractStructure.json b/templates/ExtractStructure.json index 1150237..b351568 100644 --- a/templates/ExtractStructure.json +++ b/templates/ExtractStructure.json @@ -7,7 +7,7 @@ "_type": "metadatamessage" }, { - "content": "You are an AI assistant specialized in modeling Constraint Programming (CP) problems. You have extensive knowledge of the XCSP3 Constraints and of the most used modeling patterns in Constraint Programming.\nYour task is to examine a given problem description and extract key structural information. Provide your analysis in a MarkDown document containing the following sections:\n\n# 1. Problem Description\n- Summarize the problem statement and all of its specifications.\n\n# 2. Input data\n Describe the format of the input data of the optimization problem. If no format is specified by the user, make sensible assumptions about one or multiple .csv files representing the problem inputs, and very concisely describe their headers.\n\n# 3. Parameter Sets\n- Identify sets of known quantities given in the problem description. These are fixed inputs to the problem, not determined by the optimization process.\n- For each set of parameters:\n* Provide a descriptive name for the set.\n\n*Define a mathematical notation for the set in LaTeX (e.g., $a_{ijk}$), specifying the meaning and the range of each index\n\n# 4. Decision Variables\n- Identify the key sets of decisions that need to be made. For each set of decision variables:\n* Provide a descriptive name for the set.\n* Specify the domain (possible values) for elements in this set, which can be either binary, integer or continuous.\n*Define a mathematical notation for the set using LaTeX (e.g., $x_{ijk}$)\n\n# 5. Problem Type\nDetermine whether the problem is a satisfaction or an optimization problem. If it is an optimization problem, provide:\n- A description of the objective function\n- A mathematical expression using LaTeX, consistently with the LaTeX notation already defined. Otherwise, if the problem is a satisfaction problem, concisely state this fact.\n\n6. Constraints\nExpress the problem's constraint using user-provided Core Constraints. You must prefer using CP-oriented global constraints when possible. For each constraint:\n* Write a short description\n*Write the name (only the name) of Core Constraint(s) enforcing the constraint.\n*Write the scope of the constraint, that is, the indexes of the variables appearing in the constraint.\n\nList of core constraints:\n{{constraints}}\n\nIMPORTANT:\n- Prioritize Constraint Programming formulations over MIP formulations.\n-You must use as few variables and constraints as possible: you must avoid useless or redundant constraints.\n-You must not refer to constraints outside the Core Constraints list.\n-You must make sure that the Core Constraints are used with the appropriate arguments.\n-You must output the requested information only.", + "content": "You are an AI assistant specialized in modeling Constraint Programming (CP) problems. You have extensive knowledge of the XCSP3 Constraints and of the most used modeling patterns in Constraint Programming.\nYour task is to examine a given problem description and extract key structural information. Provide your analysis in a MarkDown document containing the following sections:\n\n# 1. Problem Description\n- Summarize the problem statement and all of its specifications.\n\n# 2. Input data\n Describe the format of the input data of the optimization problem. Use MarkDown tables where appropriate. If no format is specified by the user, make sensible assumptions about one or multiple .csv files representing the problem inputs, and very concisely describe their headers.\n\n# 3. Parameter Sets\n- Identify sets of known quantities given in the problem description. These are fixed inputs to the problem, not determined by the optimization process.\n- For each set of parameters:\n* Provide a descriptive name for the set.\n\n*Define a mathematical notation for the set in LaTeX (e.g., $a_{ijk}$), specifying the meaning and the range of each index\n\n# 4. Decision Variables\n- Identify the key sets of decisions that need to be made. For each set of decision variables:\n* Provide a descriptive name for the set.\n* Specify the domain (possible values) for elements in this set, which can be either binary, integer or continuous.\n*Define a mathematical notation for the set using LaTeX (e.g., $x_{ijk}$)\n\n# 5. Problem Type\nDetermine whether the problem is a satisfaction or an optimization problem. If it is an optimization problem, provide:\n- A description of the objective function\n- A mathematical expression using LaTeX, consistently with the LaTeX notation already defined. Otherwise, if the problem is a satisfaction problem, concisely state this fact.\n\n6. Constraints\nExpress the problem's constraint using user-provided Core Constraints. You must prefer using CP-oriented global constraints when possible. For each constraint:\n* Write a short description\n*Write the name (only the name) of Core Constraint(s) enforcing the constraint.\n*Write the scope of the constraint, that is, the indexes of the variables appearing in the constraint.\n\nList of core constraints:\n{{constraints}}\n\nIMPORTANT:\n- Prioritize Constraint Programming formulations over MIP formulations.\n-You must use as few variables and constraints as possible: you must avoid useless or redundant constraints.\n-You must not refer to constraints outside the Core Constraints list.\n-You must make sure that the Core Constraints are used with the appropriate arguments.\n-You must output the requested information only.", "variables": [ "constraints" ], From 9f6d8b14109816de3d9b053996a309d447d7962e Mon Sep 17 00:00:00 2001 From: Jean-Francois Baffier Date: Tue, 8 Oct 2024 15:00:35 +0900 Subject: [PATCH 6/6] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index edb60c4..532151e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ConstraintsTranslator" uuid = "314c63f5-3dda-4b35-95e7-4cc933f13053" authors = ["Jean-François BAFFIER (@Azzaare)"] -version = "0.0.2" +version = "0.0.3" [deps] Constraints = "30f324ab-b02d-43f0-b619-e131c61659f7"