Skip to content

blackestwhite/gopenai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GopenAI - golang open ai client

Features

  • Supports streaming/SSE for chat completions
  • Image generation using OpenAI's DALL-E

How to use

basic usage(with SSE):

package main

import (
    "log"
    "github.com/blackestwhite/gopenai"
)

func main() {
    key := "YOUR-OPEN-AI-KEY"

    instance := gopenai.Setup(key)

    p := gopenai.ChatCompletionRequestBody{
        Model: gopenai.ModelGPT3_5Turbo,
        Messages: []gopenai.Message{
            {Role: "user", Content: "hi"},
        },
    }

    resultCh, errCh := instance.GenerateChatCompletionStream(p)

    for {
        select {
        case chunk, ok := <-resultCh:
            if !ok {
                resultCh = nil
            } else {
                log.Println(chunk)
            }
        case err, ok := <-errCh:
            if ok {
                log.Printf("Error: %v", err)
            }
            errCh = nil
        }

        if resultCh == nil && errCh == nil {
            break
        }
    }
}

image generation

package main

import (
    "log"
    "github.com/blackestwhite/gopenai"
)

func main() {
    key := "YOUR-OPEN-AI-KEY"

    instance := gopenai.Setup(key)

    prompt := "a cute persian cat"

    res, _ := instance.GenerateImage(prompt)
    println(res.Data[0].URL) // prints the url of the generated image
}

Donations

ETH: blackestwhite.eth

TON: blackestwhite.ton

About

Golang open ai client

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages