curl -v -H "cookie: $cookie" -X POST -d '{"role":"","content":""}' -H "content-type: application/json" http://localhost:3000/api/askstream
…
curl -v
https://api.openai.com/v1/chat/completions
\
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
],
"stream": true
}'
* Trying 104.18.6.192:443...
* Connected to api.openai.com (104.18.6.192) port 443
* ALPN: curl offers h2,http/1.1
* (304) (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/cert.pem
* CApath: none
* (304) (IN), TLS handshake, Server hello (2):
* (304) (IN), TLS handshake, Unknown (8):
* (304) (IN), TLS handshake, Certificate (11):
* (304) (IN), TLS handshake, CERT verify (15):
* (304) (IN), TLS handshake, Finished (20):
* (304) (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256
* ALPN: server accepted h2
* Server certificate:
* subject: CN=api.openai.com
* start date: May 1 23:05:16 2024 GMT
* expire date: Jul 30 23:05:15 2024 GMT
* subjectAltName: host "api.openai.com" matched cert's "api.openai.com"
* issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1P5
* SSL certificate verify ok.
* using HTTP/2
* [HTTP/2] [1] OPENED stream for https://api.openai.com/v1/chat/completions
* [HTTP/2] [1] [:method: POST]
* [HTTP/2] [1] [:scheme: https]
* [HTTP/2] [1] [:authority: api.openai.com]
* [HTTP/2] [1] [:path: /v1/chat/completions]
* [HTTP/2] [1] [user-agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]
* [HTTP/2] [1] [accept: */*]
* [HTTP/2] [1] [content-type: application/json]
* [HTTP/2] [1] [authorization: Bearer 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]
* [HTTP/2] [1] [content-length: 233]
> POST /v1/chat/completions HTTP/2
> Host: api.openai.com
> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
> Accept: */*
> Content-Type: application/json
> Authorization: Bearer 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
> Content-Length: 233
>
< HTTP/2 200
< date: Sun, 02 Jun 2024 11:53:35 GMT
< content-type: text/event-stream; charset=utf-8
< openai-organization: touch-elephant
< openai-processing-ms: 93
< openai-version: 2020-10-01
< strict-transport-security: max-age=15724800; includeSubDomains
< x-ratelimit-limit-requests: 5000
< x-ratelimit-limit-tokens: 450000
< x-ratelimit-remaining-requests: 4999
< x-ratelimit-remaining-tokens: 449972
< x-ratelimit-reset-requests: 12ms
< x-ratelimit-reset-tokens: 3ms
< x-request-id: req_b7843aabf92af55b97dd9d65af634894
< cf-cache-status: DYNAMIC
< set-cookie: __cf_bm=50QGiKgcCsgkHD4pT6e.pX9JsU8wDPehbNFzepwTwzg-1717329215-1.0.1.1-DyB0eS8tHAck4x7TNNQ1ipVlHOGvb0ejgNxSVIJTlHp9c6nQgpJu8_mNGKqbeStkxifl6zrjHx5SB1IYdHyOBQ; path=/; expires=Sun, 02-Jun-24 12:23:35 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
< set-cookie: _cfuvid=q7RjIkSyRCGf_aNL0O9gw_JtXC1.mJxMIoP1qElkt3U-1717329215261-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
< server: cloudflare
< cf-ray: 88d73e686efb5bec-LIS
< alt-svc: h3=":443"; ma=86400
<
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":"Hi"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":" there"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":"!"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":" How"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":" can"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":" I"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":" assist"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":" you"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":" today"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{"content":"?"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9Ve4E4W0hHUTSLqWHEO2ReZocJqll","object":"chat.completion.chunk","created":1717329214,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_319be4768e","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: [DONE]
* Connection #0 to host api.openai.com left intact
assistant flow (runs.stream)
https://platform.openai.com/docs/assistants/overview?context=with-streaming
const run = openai.beta.threads.runs.stream(thread.id, {
assistant_id: assistant.id
})
.on('textCreated', (text) => process.stdout.write('\nassistant > '))
.on('textDelta', (textDelta, snapshot) => process.stdout.write(textDelta.value))
.on('toolCallCreated', (toolCall) => process.stdout.write(`\nassistant > ${toolCall.type}\n\n`))
.on('toolCallDelta', (toolCallDelta, snapshot) => {
if (toolCallDelta.type === 'code_interpreter') {
if (toolCallDelta.code_interpreter.input) {
process.stdout.write(toolCallDelta.code_interpreter.input);
}
if (toolCallDelta.code_interpreter.outputs) {
process.stdout.write("\noutput >\n");
toolCallDelta.code_interpreter.outputs.forEach(output => {
if (output.type === "logs") {
process.stdout.write(`\n${output.logs}\n`);
}
});
}
}
});
assistant ??? (runs.create)
const assistantStream2 = await openai.beta.threads.runs
.create(threadId, {
assistant_id: assistantId,
stream: true
})
for await (const foo of assistantStream2){
console.log(foo)
}
completions.create
https://platform.openai.com/docs/api-reference/streaming
async function main() {
const stream = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "Say this is a test" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
}