Skip to content

Commit

Permalink
Merge pull request #10 from JuliaConstraints/feature
Browse files Browse the repository at this point in the history
Copy to clipboard
  • Loading branch information
Azzaare authored Oct 8, 2024
2 parents 07924d8 + 1c74701 commit 64b1751
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/ConstraintsTranslator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ConstraintsTranslator
import Constraints: USUAL_CONSTRAINTS
import HTTP
import InteractiveUtils
import InteractiveUtils: clipboard
import JSONSchema
import JSON3
import REPL
Expand Down
5 changes: 3 additions & 2 deletions src/llm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions src/translate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down Expand Up @@ -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",
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion templates/ExtractStructure.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. 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"
],
Expand Down

0 comments on commit 64b1751

Please sign in to comment.