Skip to content

all-in-aigc/openai-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openai-go

openai go sdk

Preparation

sign in openai platform, and get your own API_KEY.

Quick Start

  1. install openai-go sdk
go get -u github.com/all-in-aigc/openai-go
  1. request api with openai-go client
package main

import (
	"fmt"
	"log"
	"time"

	openai "github.com/all-in-aigc/openai-go"
)

func main() {
	apiKey := "xxx" // your openai apikey, or azure openai apikey

	// new openai client
	cli, _ := openai.NewClient(&openai.Options{
		ApiKey:  apiKey,
		Timeout: 30 * time.Second,
		Debug:   true,
		BaseUri: "https://xxx.com/openai", // use a proxy api, default baseuri is https://api.openai.com

		// use azure openai
		// BaseUri: "https://xxx.openai.azure.com/openai/deployments/gpt-35-turbo-16k", // your azure openai endpoint
		// ApiVersion: "2023-07-01-preview", // azure openai api version
	})

	// request api
	uri := "/v1/models"

	res, err := cli.Get(uri)

	if err != nil {
		log.Fatalf("request api failed: %v", err)
	}

	for _, v := range res.Get("data").Array() {
		fmt.Printf("model id: %s\n", v.Get("id").String())
	}
}

see available apis in OpenAI documents

Examples

there are some examples under the examples folder, check and see how to request other apis.

cli := getClient()

uri := "/v1/chat/completions"
params := map[string]interface{}{
	"model":       "gpt-3.5-turbo",
	"messages":      []map[string]interface{}{
		{"role": "user", "content": "hello"},
	},
}

res, err := cli.Post(uri, params)
if err != nil {
	log.Fatalf("request api failed: %v", err)
}

message := res.Get("choices.0.message.content").String()

fmt.Printf("message is: %s", message)
// Output: xxx
userQuestion := "What is the weather like in Boston?"

uri := "/v1/chat/completions"
params := map[string]interface{}{
	"model": "gpt-3.5-turbo",
	"messages": []map[string]interface{}{
		{
			"role":    "user",
			"content": userQuestion,
		},
	},
	"functions": getFuncs(),
}

res, err := cli.Post(uri, params)
if err != nil {
	log.Fatalf("request api failed: %v", err)
}

funcName := res.Get("choices.0.message.function_call.name").String()
funcArgs := res.Get("choices.0.message.function_call.arguments").String()

if funcName == "" || funcArgs == "" {
	log.Fatalf("function call get args failed: %s", res)
}

fmt.Printf("function call name: %s, args: %v", funcName, funcArgs)
uri := "/v1/completions"
params := map[string]interface{}{
	"model":       "text-davinci-003",
	"prompt":      "say hello three times",
	"max_tokens":  2048,
	"temperature": 0.9,
	"n":           1,
	"stream":      false,
}

res, err := cli.Post(uri, params)

if err != nil {
	log.Fatalf("request api failed: %v", err)
}

fmt.Println(res.GetString("choices.0.text"))
uri := "/v1/edits"
params := map[string]interface{}{
	"model":       "text-davinci-edit-001",
	"input":       "Are you hapy today?",
	"instruction": "fix mistake",
	"temperature": 0.9,
	"n":           1,
}

res, err := cli.Post(uri, params)

if err != nil {
	log.Fatalf("request api failed: %v", err)
}

fmt.Println(res.GetString("choices.0.text"))
uri := "/v1/images/generations"
params := map[string]interface{}{
	"prompt":          "a beautiful girl with big eyes",
	"n":               1,
	"size":            "256x256",
	"response_format": "url",
}

res, err := cli.Post(uri, params)

if err != nil {
	log.Fatalf("request api failed: %v", err)
}

fmt.Println(res.GetString("data.0.url"))

More

if this project is helpful to you, buy me a coffee😄

Buy Me A Coffee