chatgpt-客户端实现
package main import ( "context" "fmt" "github.com/sashabaranov/go-openai" "github.com/chzyer/readline" ) func main() { client := openai.NewClient("") messages := make([]openai.ChatCompletionMessage, 0) reader,err := readline.New("> ") if err != nil { panic(err) } defer reader.Close() fmt.Println(" New Chat: ") fmt.Println("---------------------") for { line, _, err := reader.Readline() if err != nil { fmt.Println(err) continue } text := string(line) messages = append(messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleUser, Content: text, }) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, Messages: messages, }, ) if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) continue } content := resp.Choices[0].Message.Content messages = append(messages, openai.ChatCompletionMessage{ Role: openai.ChatMessageRoleAssistant, Content: content, }) fmt.Println(content) } }