Commit
Β·
3205c31
1
Parent(s):
02c81d2
update template.py
Browse files- template.py +27 -13
template.py
CHANGED
@@ -3,23 +3,37 @@ from datasets import load_dataset
|
|
3 |
from openai import OpenAI
|
4 |
from together import Together
|
5 |
|
6 |
-
#
|
7 |
GT_DATASET_NAME = "kostis-init/CP-Bench"
|
8 |
DATASET_SPLIT = "train"
|
9 |
PROBLEM_ID_COLUMN = "id"
|
10 |
PROBLEM_DESCRIPTION_COLUMN = "description"
|
11 |
PROBLEM_DATA_COLUMN = "input_data"
|
12 |
PROBLEM_DECISION_VARS_COLUMN = "decision_variables"
|
13 |
-
#
|
14 |
|
15 |
#######################################################################
|
16 |
-
#
|
17 |
# You can use this as a starting point for your own approach. #
|
18 |
#######################################################################
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
LLM_TEMPERATURE = 0.5 # Controls the randomness of the output (the lower, the more deterministic)
|
24 |
LLM_SEED = 42 # Seed for reproducibility (optional, but recommended)
|
25 |
LLM_MAX_TOKENS = 3000 # Maximum number of tokens in the generated model (adjust based on your needs)
|
@@ -28,7 +42,8 @@ LLM_TOP_P = 1.0 # Top-p sampling parameter (1.0 means no filtering)
|
|
28 |
TARGET_MODELLING_FRAMEWORK = "CPMpy"
|
29 |
OUTPUT_FILE = "template_submission.jsonl"
|
30 |
|
31 |
-
# TODO: Write the main instruction given to the LLM to generate the model.
|
|
|
32 |
SYSTEM_PROMPT_TEMPLATE = f"""You are an expert in constraint programming.
|
33 |
Your task is to convert the given natural language problem description into a complete and runnable {TARGET_MODELLING_FRAMEWORK} model.
|
34 |
The model should be self-contained.
|
@@ -38,19 +53,18 @@ The keys in the JSON output should correspond to the decision variables relevant
|
|
38 |
Do not include any explanations or introductory text, just the model code between triple backticks.
|
39 |
For example:
|
40 |
```python
|
41 |
-
|
42 |
```
|
43 |
"""
|
44 |
|
45 |
-
# TODO:
|
46 |
def generate_model_with_llm(problem_id: str, description: str, input_data: str, decision_variables: [str]) -> str:
|
47 |
|
48 |
user_prompt = f"Problem Description:\n{description}\n\n"
|
49 |
-
if decision_variables:
|
50 |
-
user_prompt += (f"The solution should be a JSON object. "
|
51 |
-
f"The key(s) should strictly be: {', '.join(decision_variables)}.\n\n")
|
52 |
if input_data:
|
53 |
user_prompt += f"Input Data:\n{input_data}\n\n"
|
|
|
|
|
54 |
user_prompt += f"Generate the {TARGET_MODELLING_FRAMEWORK} model."
|
55 |
|
56 |
messages = [
|
@@ -82,7 +96,7 @@ def generate_model_with_llm(problem_id: str, description: str, input_data: str,
|
|
82 |
print(f" [LLM Call] Error generating model for problem {problem_id}: {type(e).__name__} - {e}")
|
83 |
return f"# Error generating model for problem {problem_id}: {type(e).__name__} - {e}\n# Please check the LLM configuration and try again."
|
84 |
|
85 |
-
#
|
86 |
def main():
|
87 |
print(f"Starting model generation script for {TARGET_MODELLING_FRAMEWORK}.")
|
88 |
print(f"Loading dataset '{GT_DATASET_NAME}'...")
|
|
|
3 |
from openai import OpenAI
|
4 |
from together import Together
|
5 |
|
6 |
+
# === DATASET CONFIGURATION (DO NOT MODIFY) ===
|
7 |
GT_DATASET_NAME = "kostis-init/CP-Bench"
|
8 |
DATASET_SPLIT = "train"
|
9 |
PROBLEM_ID_COLUMN = "id"
|
10 |
PROBLEM_DESCRIPTION_COLUMN = "description"
|
11 |
PROBLEM_DATA_COLUMN = "input_data"
|
12 |
PROBLEM_DECISION_VARS_COLUMN = "decision_variables"
|
13 |
+
# ==============================================
|
14 |
|
15 |
#######################################################################
|
16 |
+
# Template script to generate constraint models using LLMs. #
|
17 |
# You can use this as a starting point for your own approach. #
|
18 |
#######################################################################
|
19 |
|
20 |
+
# === CHOOSE LLM CLIENT AND MODEL CONFIGURATION ===
|
21 |
+
|
22 |
+
# Example 1: OpenAI (e.g., GPT-4o)
|
23 |
+
# LLM_CLIENT = OpenAI(api_key="YOUR_API_KEY")
|
24 |
+
# LLM_ID = "gpt-4o"
|
25 |
+
|
26 |
+
# Example 2: DeepSeek (or any other OpenAI-compatible API)
|
27 |
+
# LLM_CLIENT = OpenAI(api_key="DEEPSEEK_API_KEY", base_url="https://api.deepseek.com")
|
28 |
+
# LLM_ID = "deepseek-chat"
|
29 |
+
|
30 |
+
# Example 3: Together.ai
|
31 |
+
# LLM_CLIENT = Together(api_key="TOGETHER_API_KEY")
|
32 |
+
# LLM_ID = "mistralai/Mixtral-8x22B-Instruct-v0.1"
|
33 |
+
|
34 |
+
LLM_CLIENT = OpenAI(api_key="YOUR_API_KEY") # TODO: Set your API key or switch client above
|
35 |
+
LLM_ID = "gpt-4o" # TODO: Change to your chosen model (name it as per the LLM provider's documentation)
|
36 |
+
|
37 |
LLM_TEMPERATURE = 0.5 # Controls the randomness of the output (the lower, the more deterministic)
|
38 |
LLM_SEED = 42 # Seed for reproducibility (optional, but recommended)
|
39 |
LLM_MAX_TOKENS = 3000 # Maximum number of tokens in the generated model (adjust based on your needs)
|
|
|
42 |
TARGET_MODELLING_FRAMEWORK = "CPMpy"
|
43 |
OUTPUT_FILE = "template_submission.jsonl"
|
44 |
|
45 |
+
# === TODO: Write the main instruction given to the LLM to generate the model. ===
|
46 |
+
|
47 |
SYSTEM_PROMPT_TEMPLATE = f"""You are an expert in constraint programming.
|
48 |
Your task is to convert the given natural language problem description into a complete and runnable {TARGET_MODELLING_FRAMEWORK} model.
|
49 |
The model should be self-contained.
|
|
|
53 |
Do not include any explanations or introductory text, just the model code between triple backticks.
|
54 |
For example:
|
55 |
```python
|
56 |
+
# model code here
|
57 |
```
|
58 |
"""
|
59 |
|
60 |
+
# === MAIN LOGIC, TODO: You can adapt this function to try different prompting strategies ===
|
61 |
def generate_model_with_llm(problem_id: str, description: str, input_data: str, decision_variables: [str]) -> str:
|
62 |
|
63 |
user_prompt = f"Problem Description:\n{description}\n\n"
|
|
|
|
|
|
|
64 |
if input_data:
|
65 |
user_prompt += f"Input Data:\n{input_data}\n\n"
|
66 |
+
if decision_variables:
|
67 |
+
user_prompt += f"The model must output a JSON with these keys: {', '.join(decision_variables)}.\n\n"
|
68 |
user_prompt += f"Generate the {TARGET_MODELLING_FRAMEWORK} model."
|
69 |
|
70 |
messages = [
|
|
|
96 |
print(f" [LLM Call] Error generating model for problem {problem_id}: {type(e).__name__} - {e}")
|
97 |
return f"# Error generating model for problem {problem_id}: {type(e).__name__} - {e}\n# Please check the LLM configuration and try again."
|
98 |
|
99 |
+
# === MAIN EXECUTION LOOP (No need to change) ===
|
100 |
def main():
|
101 |
print(f"Starting model generation script for {TARGET_MODELLING_FRAMEWORK}.")
|
102 |
print(f"Loading dataset '{GT_DATASET_NAME}'...")
|