openai/openai-go

Public

mirrored from https://github.com/openai/openai-goAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.7.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/chat-completion-streaming/main.go

39lines · modecode

1package main
2
3import (
4 "context"
5
6 "github.com/openai/openai-go"
7)
8
9func main() {
10 client := openai.NewClient()
11
12 ctx := context.Background()
13
14 question := "Write me a haiku"
15
16 print("> ")
17 println(question)
18 println()
19
20 stream := client.Chat.Completions.NewStreaming(ctx, openai.ChatCompletionNewParams{
21 Messages: []openai.ChatCompletionMessageParamUnion{
22 openai.UserMessage(question),
23 },
24 Seed: openai.Int(0),
25 Model: openai.ChatModelGPT4o,
26 })
27
28 for stream.Next() {
29 evt := stream.Current()
30 if len(evt.Choices) > 0 {
31 print(evt.Choices[0].Delta.Content)
32 }
33 }
34 println()
35
36 if err := stream.Err(); err != nil {
37 panic(err.Error())
38 }
39}