openai

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

README

OpenAI Golang SDK

OpenAI Golang SDK是用于与OpenAI API进行交互的Golang库。

相关文档: OpenAI API Documentation

安装

使用Go Modules可以很方便地安装OpenAI Golang SDK。在你的项目目录中,执行以下命令:

go get github.com/artisancloud/openai

使用

以下是一个简单的例子,演示了如何使用SDK创建一个API客户端,并生成一段文本:

func main() {
	// 创建一个OpenAI API客户端
	config := &openai.ClientConfig{
		V1: openai.V1Config{
			OpenAPIKey: "your-openai-key", // 必填, 你的OpenAI API密钥
			Organization: "your-organization", // 可选, 你的组织id
			HttpDebug: true, // 可选, 是否开启调试模式(使用默认log输出HTTP请求信息)
			ProxyURL: "proxy-url", // 可选, 代理地址, 如果包含基本鉴权, 格式为http://username:password@xxxxxx:port
		},
	}
	client, err := openai.NewClient(config)
	if err != nil {
		panic(err)
	}

	// 调用GPT3.5模型
	req := v1.CreateChatCompletionRequest{
		Model: "gpt-3.5-turbo",
		Messages: []v1.Message{
			{
				Role:    "user",
				Content: "Hello!",
			},
		},
	}
	result, err := client.V1.Chat.CreateChatCompletion(&req)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}

	// 输出结果
	fmt.Println(result)
}

其他类型定义

除OpenAI Golang SDK本身外,还提供了一些类型定义以适应OpenAI中的可选字段。这些类型定义和实用函数可以在openaitype 包中找到。这些类型定义和函数可以帮助您更方便地与OpenAI API交互,并确保API请求格式正确。

许可证

OpenAI Golang SDK采用MIT许可证发布。更多信息请参见许可证文件。

贡献

Powered by Artisan Cloud

Documentation generated by ChatGPT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*v1.V1
	// contains filtered or unexported fields
}

func NewClient

func NewClient(config Config) (*Client, error)

type ClientConfig

type ClientConfig struct {
	V1 V1Config
}

func (*ClientConfig) Default

func (c *ClientConfig) Default()

func (*ClientConfig) Validate

func (c *ClientConfig) Validate() error

type Config

type Config interface {
	Default()
	Validate() error
}

type V1Config

type V1Config struct {
	OpenAPIKey   string
	Organization string
	HttpDebug    bool
	ProxyURL     string
}

func (*V1Config) Default

func (c *V1Config) Default()

func (*V1Config) Validate

func (c *V1Config) Validate() error

Directories

Path Synopsis
api
v1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL