forked from tensorzero/tensorzero
-
Notifications
You must be signed in to change notification settings - Fork 0
241 lines (203 loc) · 7.59 KB
/
batch-completion-cron.yml
File metadata and controls
241 lines (203 loc) · 7.59 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
name: Batch Inference Completion-time Cron Job
on:
schedule:
# Runs every hour at minute 0
- cron: "0 * * * *"
workflow_dispatch: # Allow manual triggering
env:
# Fixed input for all batch jobs
BATCH_INPUT: |
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-4", "messages": [{"role": "user", "content": "What is the capital of France?"}], "max_tokens": 100}}
jobs:
openai-batch:
if: github.repository == 'tensorzero/tensorzero'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Install dependencies
run: |
pip install openai requests
- name: Create batch input file
run: |
mkdir -p batch_inputs
echo '${{ env.BATCH_INPUT }}' > batch_inputs/input.jsonl
- name: OpenAI Batch Inference
env:
OPENAI_API_KEY: ${{ secrets.BATCH_CRON_OPENAI_API_KEY }}
run: |
python -c "
import openai
import json
from datetime import datetime
import secrets
import string
# Generate random string
random_str = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(10))
client = openai.OpenAI()
# Upload batch input file
batch_input = {
'custom_id': 'request-1',
'method': 'POST',
'url': '/v1/chat/completions',
'body': {
'model': 'gpt-4',
'messages': [{
'role': 'user',
'content': f'My id is {random_str}. What is the capital of France?'
}],
'max_tokens': 100
}
}
with open('batch_inputs/input.jsonl', 'w') as f:
f.write(json.dumps(batch_input) + '\n')
batch_input_file = client.files.create(
file=open('batch_inputs/input.jsonl', 'rb'),
purpose='batch'
)
# Create batch job
batch_job = client.batches.create(
input_file_id=batch_input_file.id,
endpoint='/v1/chat/completions',
completion_window='24h',
metadata={'source': 'github-actions', 'timestamp': datetime.now().isoformat()}
)
print(f'OpenAI Batch Job Created: {batch_job.id}')
print(f'Status: {batch_job.status}')
"
- name: Cleanup
run: |
rm -rf batch_inputs/
google-vertex-batch:
if: github.repository == 'tensorzero/tensorzero'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Install dependencies
run: |
pip install google-cloud-aiplatform google-cloud-storage
- name: Google Vertex AI Batch Inference
env:
GOOGLE_APPLICATION_CREDENTIALS_JSON: ${{ secrets.GCP_JWT_KEY }}
GOOGLE_CLOUD_PROJECT: tensorzero-public
GOOGLE_CLOUD_REGION: us-east1
run: |
# Create service account key file
echo '${{ secrets.GCP_JWT_KEY }}' > /tmp/gcp-key.json
export GOOGLE_APPLICATION_CREDENTIALS=/tmp/gcp-key.json
python -c "
from google.cloud import aiplatform
from google.cloud import storage
import json
from datetime import datetime
import uuid
import secrets
import string
# Generate random string
random_str = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(10))
# Initialize Vertex AI
aiplatform.init(
project='tensorzero-public',
location='us-east1'
)
# Create batch prediction input
batch_input = {
'request': {
'contents': [{
'role': 'user',
'parts': [{
'text': f'My id is {random_str}. What is the capital of France?'
}]
}]
}
}
# Upload to GCS (you'll need a bucket)
storage_client = storage.Client()
bucket_name = 'tensorzero-batch-tests-input'
bucket = storage_client.bucket(bucket_name)
# Create unique filename
timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
input_blob_name = f'batch-inputs/gemini-input-{timestamp}.jsonl'
output_blob_name = f'batch-outputs/gemini-output-{timestamp}/'
# Upload input file
blob = bucket.blob(input_blob_name)
blob.upload_from_string(json.dumps(batch_input))
# Create batch prediction job
job = aiplatform.BatchPredictionJob.submit(
job_display_name=f'gemini-batch-{timestamp}',
model_name='publishers/google/models/gemini-2.0-flash-001',
instances_format='jsonl',
predictions_format='jsonl',
gcs_source=f'gs://{bucket_name}/{input_blob_name}',
gcs_destination_prefix=f'gs://{bucket_name}/{output_blob_name}',
)
print(f'Vertex AI Batch Job Created: {job.name}')
print(f'State: {job.state}')
"
- name: Cleanup
run: |
rm -f /tmp/gcp-key.json
anthropic-batch:
if: github.repository == 'tensorzero/tensorzero'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Install dependencies
run: |
pip install anthropic
- name: Anthropic Batch Inference
env:
ANTHROPIC_API_KEY: ${{ secrets.BATCH_CRON_ANTHROPIC_API_KEY }}
run: |
python -c "
import anthropic
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
from anthropic.types.messages.batch_create_params import Request
import json
from datetime import datetime
import secrets
import string
# Generate random string
random_str = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(10))
client = anthropic.Anthropic()
batch_job = client.messages.batches.create(
requests=[
Request(
custom_id='my-first-request',
params=MessageCreateParamsNonStreaming(
model='claude-3-7-sonnet-20250219',
max_tokens=1024,
messages=[{
'role': 'user',
'content': f'My id is {random_str}. What is the capital of France?',
}]
)
),
]
)
print(f'Anthropic Batch Job Created: {batch_job.id}')
print(f'Processing Status: {batch_job.processing_status}')
"
log-completion:
if: github.repository == 'tensorzero/tensorzero'
needs: [openai-batch, google-vertex-batch, anthropic-batch]
runs-on: ubuntu-latest
steps:
- name: Log completion
run: |
echo "Batch inference jobs submitted at $(date)"
echo "All three providers have been triggered successfully"