package main
import (
"context"
"fmt"
"os"
"time"
adc "github.com/AceDataCloud/SDK/go"
)
func main() {
client, err := adc.NewClient(adc.WithAPIToken(os.Getenv("ACEDATACLOUD_API_TOKEN")))
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
t0 := time.Now()
chunks, errs := client.OpenAI().Chat().Completions().CreateStream(ctx, adc.ChatCompletionRequest{
Model: "gpt-4o-mini",
Messages: []map[string]any{{"role": "user", "content": "Count from 1 to 5 separated by spaces. Just the numbers."}},
MaxTokens: 30,
})
first := int64(-1)
cnt := 0
collected := ""
for chunk := range chunks {
if first < 0 {
first = time.Since(t0).Milliseconds()
}
cnt++
if ch, ok := chunk["choices"].([]any); ok && len(ch) > 0 {
if c0, ok := ch[0].(map[string]any); ok {
if d, ok := c0["delta"].(map[string]any); ok {
if s, ok := d["content"].(string); ok {
collected += s
}
}
}
}
}
if e, ok := <-errs; ok && e != nil {
fmt.Println("stream_err", e)
}
fmt.Println("total_elapsed_ms", time.Since(t0).Milliseconds())
fmt.Println("first_chunk_ms", first)
fmt.Println("chunks", cnt)
fmt.Println("collected", collected)
}