-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_es_wildchat.py
147 lines (141 loc) · 6.18 KB
/
create_es_wildchat.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
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
import os
import json
from datasets import load_dataset
from elasticsearch import Elasticsearch, helpers
# Load the dataset
dataset = load_dataset('allenai/WildChat-1M')
# Initialize Elasticsearch client
#es = Elasticsearch('http://localhost:9200')
es = Elasticsearch('https://localhost:9200', basic_auth=('elastic', os.getenv('ES_PASSWD')), ssl_assert_fingerprint=os.getenv('ES_FINGERPRINT'))
# Prepare the index settings and mappings
index_name = 'wildchat'
mappings = {
"properties": {
"conversation_hash": {"type": "keyword"},
"model": {"type": "keyword"},
"timestamp": {"type": "date"},
"conversation": {
"type": "nested",
"properties": {
"content": {"type": "text"},
"country": {"type": "keyword"},
"hashed_ip": {"type": "keyword"},
"header": {
"type": "object",
"properties": {
"accept-language": {"type": "keyword"},
"user-agent": {"type": "keyword"}
}
},
"language": {"type": "keyword"},
"redacted": {"type": "boolean"},
"role": {"type": "keyword"},
"state": {"type": "keyword"},
"timestamp": {"type": "date"},
"toxic": {"type": "boolean"},
"turn_identifier": {"type": "integer"}
}
},
"turn": {"type": "integer"},
"language": {"type": "keyword"},
"openai_moderation": {
"type": "nested",
"properties": {
"categories": {
"type": "object",
"properties": {
"harassment": {"type": "boolean"},
"harassment/threatening": {"type": "boolean"},
"harassment_threatening": {"type": "boolean"},
"hate": {"type": "boolean"},
"hate/threatening": {"type": "boolean"},
"hate_threatening": {"type": "boolean"},
"self-harm": {"type": "boolean"},
"self-harm/instructions": {"type": "boolean"},
"self-harm/intent": {"type": "boolean"},
"self_harm": {"type": "boolean"},
"self_harm_instructions": {"type": "boolean"},
"self_harm_intent": {"type": "boolean"},
"sexual": {"type": "boolean"},
"sexual/minors": {"type": "boolean"},
"sexual_minors": {"type": "boolean"},
"violence": {"type": "boolean"},
"violence/graphic": {"type": "boolean"},
"violence_graphic": {"type": "boolean"}
}
},
"category_scores": {
"type": "object",
"properties": {
"harassment": {"type": "float"},
"harassment/threatening": {"type": "float"},
"harassment_threatening": {"type": "float"},
"hate": {"type": "float"},
"hate/threatening": {"type": "float"},
"hate_threatening": {"type": "float"},
"self-harm": {"type": "float"},
"self-harm/instructions": {"type": "float"},
"self-harm/intent": {"type": "float"},
"self_harm": {"type": "float"},
"self_harm_instructions": {"type": "float"},
"self_harm_intent": {"type": "float"},
"sexual": {"type": "float"},
"sexual/minors": {"type": "float"},
"sexual_minors": {"type": "float"},
"violence": {"type": "float"},
"violence/graphic": {"type": "float"},
"violence_graphic": {"type": "float"}
}
},
"flagged": {"type": "boolean"}
}
},
"detoxify_moderation": {
"type": "nested",
"properties": {
"identity_attack": {"type": "float"},
"insult": {"type": "float"},
"obscene": {"type": "float"},
"severe_toxicity": {"type": "float"},
"sexual_explicit": {"type": "float"},
"threat": {"type": "float"},
"toxicity": {"type": "float"}
}
},
"toxic": {"type": "boolean"},
"redacted": {"type": "boolean"},
"state": {"type": "keyword"},
"country": {"type": "keyword"},
"hashed_ip": {"type": "keyword"},
"header": {
"type": "object",
"properties": {
"accept-language": {"type": "keyword"},
"user-agent": {"type": "keyword"}
}
}
}
}
# Create the index with the specified mappings
if es.indices.exists(index=index_name):
es.indices.delete(index=index_name)
assert not es.indices.exists(index=index_name)
if not es.indices.exists(index=index_name):
es.indices.create(index=index_name, mappings=mappings)
import tqdm
# Prepare data for bulk indexing
actions = []
chunk_size = 10000 # Set your desired chunk size
for i, record in enumerate(tqdm.tqdm(dataset['train']), 1):
action = {
"_index": index_name,
"_source": record
}
actions.append(action)
if i % chunk_size == 0:
helpers.bulk(es, actions)
actions = [] # Reset actions after each chunk is indexed
# Index any remaining actions
if actions:
helpers.bulk(es, actions)
print("Indexing completed.")