-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_batch_file.py
70 lines (61 loc) · 2.24 KB
/
create_batch_file.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
"""
This script creates a batch file that can be used to submit to the batch API.
Author: Kaicheng Yang <[email protected]>
"""
import json
#######################################
# We will use the same text messages as example
text_messages = [
"The service here is very good!",
"The service here is good.",
"The service here is ok.",
"The service here is not very good.",
"The service here is terrible!",
]
#######################################
# Prompt-related
system_prompt = "You are an expert on sentiment analysis. Your job is to evaluate the sentiment of the given text message."
user_instruction = """
Given the following text message: '{text_message}', please evaluate its sentiment by giving a score in the range of -1 to 1, where -1 means negative and 1 means positive.
Also explain why.
The output should be in JSON format and follow the following schema:
--------------
```json
{{
'score': 0.1,
'explanation': '...'
}}
```
"""
#######################################
# Create tasks
tasks = []
for index, text_message in enumerate(text_messages):
task = {
# The API won't return the input text message, so we need a unique ID for each task
# This way we can merge the results back with the input text message
# Instead of generating the ID on the fly, it's recommended to assign a unique ID to each input message at the beginning
"custom_id": f"text_message_{index}",
"method": "POST",
"url": "/v1/chat/completions",
"body": {
# This is what you would have in your Chat Completions API call
"model": "gpt-3.5-turbo",
"temperature": 0.0,
"response_format": {"type": "json_object"},
"messages": [
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": user_instruction.format(text_message=text_message),
},
],
},
}
tasks.append(task)
#######################################
# Write tasks to a file
task_file_name = "text_message_tasks.jsonl"
with open(task_file_name, "w") as f:
for task in tasks:
f.write(json.dumps(task) + "\n")