forked from DataTalksClub/llm-zoomcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_data.py
More file actions
153 lines (128 loc) · 6.38 KB
/
Copy pathgenerate_data.py
File metadata and controls
153 lines (128 loc) · 6.38 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import time
import random
import uuid
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from db import save_conversation, save_feedback, get_db_connection
# Set the timezone to CET (Europe/Berlin)
tz = ZoneInfo("Europe/Berlin")
# List of sample questions and answers
SAMPLE_QUESTIONS = [
"What is machine learning?",
"How does linear regression work?",
"Explain the concept of overfitting.",
"What is the difference between supervised and unsupervised learning?",
"How does cross-validation help in model evaluation?",
]
SAMPLE_ANSWERS = [
"Machine learning is a subset of artificial intelligence that focuses on the development of algorithms and statistical models that enable computer systems to improve their performance on a specific task through experience.",
"Linear regression is a statistical method used to model the relationship between a dependent variable and one or more independent variables by fitting a linear equation to observed data.",
"Overfitting occurs when a machine learning model learns the training data too well, including its noise and fluctuations, resulting in poor generalization to new, unseen data.",
"Supervised learning involves training models on labeled data, while unsupervised learning deals with finding patterns in unlabeled data without predefined outputs.",
"Cross-validation is a technique used to assess how well a model will generalize to an independent dataset. It involves partitioning the data into subsets, training the model on a subset, and validating it on the remaining data.",
]
COURSES = ["machine-learning-zoomcamp", "data-engineering-zoomcamp", "mlops-zoomcamp"]
MODELS = ["ollama/phi3", "openai/gpt-3.5-turbo", "openai/gpt-4o", "openai/gpt-4o-mini"]
RELEVANCE = ["RELEVANT", "PARTLY_RELEVANT", "NON_RELEVANT"]
def generate_synthetic_data(start_time, end_time):
current_time = start_time
conversation_count = 0
print(f"Starting historical data generation from {start_time} to {end_time}")
while current_time < end_time:
conversation_id = str(uuid.uuid4())
question = random.choice(SAMPLE_QUESTIONS)
answer = random.choice(SAMPLE_ANSWERS)
course = random.choice(COURSES)
model = random.choice(MODELS)
relevance = random.choice(RELEVANCE)
openai_cost = 0
if model.startswith("openai/"):
openai_cost = random.uniform(0.001, 0.1)
answer_data = {
"answer": answer,
"response_time": random.uniform(0.5, 5.0),
"relevance": relevance,
"relevance_explanation": f"This answer is {relevance.lower()} to the question.",
"model_used": model,
"prompt_tokens": random.randint(50, 200),
"completion_tokens": random.randint(50, 300),
"total_tokens": random.randint(100, 500),
"eval_prompt_tokens": random.randint(50, 150),
"eval_completion_tokens": random.randint(20, 100),
"eval_total_tokens": random.randint(70, 250),
"openai_cost": openai_cost,
}
save_conversation(conversation_id, question, answer_data, course, current_time)
print(
f"Saved conversation: ID={conversation_id}, Time={current_time}, Course={course}, Model={model}"
)
if random.random() < 0.7:
feedback = 1 if random.random() < 0.8 else -1
save_feedback(conversation_id, feedback, current_time)
print(
f"Saved feedback for conversation {conversation_id}: {'Positive' if feedback > 0 else 'Negative'}"
)
current_time += timedelta(minutes=random.randint(1, 15))
conversation_count += 1
if conversation_count % 10 == 0:
print(f"Generated {conversation_count} conversations so far...")
print(
f"Historical data generation complete. Total conversations: {conversation_count}"
)
def generate_live_data():
conversation_count = 0
print("Starting live data generation...")
while True:
current_time = datetime.now(tz)
# current_time = None
conversation_id = str(uuid.uuid4())
question = random.choice(SAMPLE_QUESTIONS)
answer = random.choice(SAMPLE_ANSWERS)
course = random.choice(COURSES)
model = random.choice(MODELS)
relevance = random.choice(RELEVANCE)
openai_cost = 0
if model.startswith("openai/"):
openai_cost = random.uniform(0.001, 0.1)
answer_data = {
"answer": answer,
"response_time": random.uniform(0.5, 5.0),
"relevance": relevance,
"relevance_explanation": f"This answer is {relevance.lower()} to the question.",
"model_used": model,
"prompt_tokens": random.randint(50, 200),
"completion_tokens": random.randint(50, 300),
"total_tokens": random.randint(100, 500),
"eval_prompt_tokens": random.randint(50, 150),
"eval_completion_tokens": random.randint(20, 100),
"eval_total_tokens": random.randint(70, 250),
"openai_cost": openai_cost,
}
save_conversation(conversation_id, question, answer_data, course, current_time)
print(
f"Saved live conversation: ID={conversation_id}, Time={current_time}, Course={course}, Model={model}"
)
if random.random() < 0.7:
feedback = 1 if random.random() < 0.8 else -1
save_feedback(conversation_id, feedback, current_time)
print(
f"Saved feedback for live conversation {conversation_id}: {'Positive' if feedback > 0 else 'Negative'}"
)
conversation_count += 1
if conversation_count % 10 == 0:
print(f"Generated {conversation_count} live conversations so far...")
time.sleep(1)
if __name__ == "__main__":
print(f"Script started at {datetime.now(tz)}")
end_time = datetime.now(tz)
start_time = end_time - timedelta(hours=6)
print(f"Generating historical data from {start_time} to {end_time}")
generate_synthetic_data(start_time, end_time)
print("Historical data generation complete.")
print("Starting live data generation... Press Ctrl+C to stop.")
try:
generate_live_data()
except KeyboardInterrupt:
print(f"Live data generation stopped at {datetime.now(tz)}.")
finally:
print(f"Script ended at {datetime.now(tz)}")