Skip to content

Instantly share code, notes, and snippets.

@zakkor
Created June 1, 2023 10:40
Show Gist options
  • Save zakkor/78d78c45d1ccae72ba4ed465aa18996e to your computer and use it in GitHub Desktop.
Save zakkor/78d78c45d1ccae72ba4ed465aa18996e to your computer and use it in GitHub Desktop.
AI
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
"github.com/sashabaranov/go-openai"
)
func main() {
inputb, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
input := string(inputb)
client := openai.NewClient("OPENAI_API_KEY")
resp, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: fmt.Sprintf("%s:\n%s", input, os.Args[1:]),
},
},
})
if err != nil {
panic(err)
}
for {
resp, err := resp.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
return
}
panic(err)
}
if resp.Choices[0].FinishReason == "stop" {
return
}
fmt.Print(resp.Choices[0].Delta.Content)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment