Skip to content

Latest commit

 

History

History
119 lines (91 loc) · 2.46 KB

README_zh.md

File metadata and controls

119 lines (91 loc) · 2.46 KB

GOpenAI

GoDoc Go Report Card

GOpenAI 是一个非官方的OpenAI API实现, 遵循官方的OpenAPI 规范.

GOpenAI提供了类似于Python风格的API,易于理解和使用。

  • openai.Completion.Create(...)
  • openai.Edit.Create(...)
  • ...

用法

创建 Completion

package main

import (
	"fmt"
	"os"

	"github.com/wikylyu/gopenai"
	"github.com/wikylyu/gopenai/completions"
	"github.com/wikylyu/gopenai/models"
)

func main() {
	openai := gopenai.New(&gopenai.Config{
		ApiKey: os.Getenv("OPENAI_API_KEY"),
	})

	prompt := []string{"Say this is a test"}
	resp, err := openai.Completion.Create(&completions.CreateRequest{
		Model:       models.ModelTextDavinci003,
		Prompt:      prompt,
		MaxTokens:   256,
		Temperature: 0,
	})
	if err != nil {
		panic(err)
	}
	for _, choice := range resp.Choices {
		fmt.Printf("%s\n", choice.Text)
	}
}

更多用法, 请参见 examples/ 目录.

错误处理

API方法可能返回两种类型的错误。

  1. OpenAI API错误
  2. 网络或通用错误

您可以使用以下代码来处理错误。

resp,err:=openai.Completion.Create(...)
if err!=nil{
	if apierr:=err.(*api.Error);apierr!=nil{
		/* OpenAI api错误,请读取apierr.Message或apierr.Type以确定确切的错误原因 */
	}else {
		/* 网络错误 */
	}
}

命令行工具

更多详情请参见 tool/ 目录。

API List

  • Model

    • Create
    • Retrieve
    • Delete
  • Completion

    • Create
  • Chat

    • Create
  • Edit

    • Create
  • Images

    • Create
    • Edit
    • Variation
  • Embeddings

    • Create
  • Audio

    • Transcribe
    • Translate
  • Files

    • Create
    • Retrieve
    • Download
  • Fine-tunes

    • Create
    • Retrieve
    • List
    • Cancel
    • Events
  • Moderations

    • Create
  • Engines

Development

Engines 已经被官方废弃,不会实现; 请使用Models作为替代。