Iann
05/26/2023, 9:32 PMChaika
05/26/2023, 9:32 PMChaika
05/26/2023, 9:33 PM*.example.com/*
or */*
worker route, AAAA
*.example.com
100::
Proxied
record)harris
05/26/2023, 9:33 PMIann
05/26/2023, 9:33 PMJames
05/26/2023, 9:33 PMChaika
05/26/2023, 9:33 PMIann
05/26/2023, 9:34 PMChaika
05/26/2023, 9:34 PMChaika
05/26/2023, 9:35 PM*/*
to match all or *.example.com/*
to match all subdomains besides apex, service: WorkerChaika
05/26/2023, 9:36 PM*.workers.example.com
)harris
05/26/2023, 9:47 PMhttps://cdn.discordapp.com/attachments/779390076219686943/1111772739167080468/image.png▾
https://cdn.discordapp.com/attachments/779390076219686943/1111772739380969542/image.png▾
kian
05/26/2023, 9:49 PMkian
05/26/2023, 9:49 PMfetch
subrequest isn't going to use up any CPUharris
05/26/2023, 9:49 PMharris
05/26/2023, 9:52 PMts
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.OPENAI_API_KEY}`,
},
body,
})
.then((res) => response.body)
.then((body) => {
const reader = body.getReader()
// do stuff with reader
})
kian
05/26/2023, 9:56 PMdo stuff with reader
?kian
05/26/2023, 9:57 PMharris
05/26/2023, 10:03 PMts
const startTime = Date.now()
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.OPENAI_API_KEY}`,
},
body,
})
if (!response.ok) {
console.log(
`Unexpected response from OpenAI API: ${response.status} ${response.statusText}`
)
return
}
if (!response.body) {
console.log("No response body")
return
}
const reader = response.body.getReader()
// Get first byte
let chunk = await reader.read()
// Log first byte time
const firstByteTime = Date.now() - startTime
// Read the rest of the response
while (!chunk.done) {
chunk = await reader.read()
}
harris
05/26/2023, 10:03 PMkian
05/26/2023, 10:04 PMharris
05/26/2023, 10:04 PMharris
05/26/2023, 10:04 PMkian
05/26/2023, 10:04 PMconst response = await ...
and then return response
, couldn't you?harris
05/26/2023, 10:04 PMharris
05/26/2023, 10:05 PMts
const totalTime = Date.now() - startTime
console.log("Time to first byte: " + firstByteTime + "ms")
console.log("Total response time: " + totalTime + "ms")
// Log response time
const client = new Client(env.DB_URL + "?sslmode=require")
await client.connect()
const text =
"INSERT INTO response_times(model, date, ttfb, duration) VALUES($1, $2, $3, $4) RETURNING *"
const values = [
"gpt-4",
new Date(event.scheduledTime),
firstByteTime,
totalTime,
]
try {
const res = await client.query(text, values)
console.log(res.rows[0])
} catch (err) {
console.error(err)
} finally {
ctx.waitUntil(client.end())
}
Isaac McFadyen | YYZ01
05/26/2023, 10:09 PMIsaac McFadyen | YYZ01
05/26/2023, 10:09 PMIsaac McFadyen | YYZ01
05/26/2023, 10:10 PMIsaac McFadyen | YYZ01
05/26/2023, 10:10 PM