-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_response_o1.py
80 lines (61 loc) · 2.48 KB
/
generate_response_o1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
from openai import OpenAI
from dotenv import load_dotenv
import os
import copy
# Load the environment variables from the .env file
load_dotenv()
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
client_openai = OpenAI(api_key=OPENAI_API_KEY)
def run_openai(messages, client):
# Send the prompt to OpenAI's GPT-4
response = client.chat.completions.create(
model="o1-preview",
messages=messages,
)
answer = response.choices[0].message.content
return answer
def generate_feature_engineer_templates(SOLUTION, question):
"""
Generates FEATURE_ENGINEER template with substituted texts.
"""
# Deep copy the original template to avoid modifying it
updated_template = copy.deepcopy(SOLUTION)
# Substitute the placeholder in the user document
for entry in updated_template:
if entry["role"] == "user":
entry["content"] = entry["content"].format(question=question)
return updated_template
SOLUTION = [
{
"role": "user",
"content": """
Please help me solve the given math question.
## Expected Output Format:
Present your final answer in a JSON format. This should include:
- A `float` value or a mathematical algebraic expression for the answer.
Your output should be formatted as a JSON object enclosed in Markdown code blocks tagged with 'json'. For example:
```json
{{
"answer": "<answer>",
}}
```
"Here is the question: {question}."
"""
}
]
def process_math_problems(input_file, output_file):
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line in infile:
problem = json.loads(line) # Convert JSON line to dictionary
for key, value in problem.items():
question = value['question']
updated_template = generate_feature_engineer_templates(SOLUTION, question)
response = run_openai(updated_template, client_openai)
output_json = json.dumps({key: response}) # Convert response to JSON line
outfile.write(output_json + '\n') # Write to file
# Specify your input and output files
input_file_path = 'final-odyssey-math-with-levels.jsonl'
output_file_path = 'jsonl/gpt-4-o1-preview-solution.jsonl'
# Call the processing function
process_math_problems(input_file_path, output_file_path)