gogpt

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 10 Imported by: 1

README

gogpt: Go语言编写的OpenAI API SDK

文档英文翻译来自 gpt3.5-turbo

English

gogpt 是一个使用 Go 语言编写的 OpenAI API SDK。本 SDK 可以帮助您轻松地与 OpenAI GPT API 进行交互,以生成自然语言文本。支持自定义请求链接,因此可使开发者无缝切换到 api2d等第三方api。

安装

使用以下命令安装 Gogpt:

go get github.com/levinion/gogpt

使用

首先,您需要注册 OpenAI API 并获取您的 API 密钥。或是注册 api2d API 并获取 Key。

导入 gogpt:

import "github.com/levinion/gogpt"

创建一个 gogpt 实例并使用您的 API 密钥进行身份验证:

c:=gogpt.NewContext().SetHeader(
	&gogpt.Header{
        //使用gogpt定义的常量,或自定义url
		Url : gogpt.API2D_STREAM_URL,   
        //在此输入key,前面的"Bearer "不可省略(注意Bearer后有空格)
		Auth: "Bearer <Openai or api2d Key>",
	},
)

使用 SetOutput 方法指定输出对象,然后用Continue 方法来生成文本:

c.SetOutput(os.Stdout)
c.Continue("请用go语言写出计算过程,并给出运行结果")

示例

package main

import (
	"os"
	"github.com/levinion/gogpt"
)

func main(){
    //打开一个文件,若不存在则创建
	file,err:=os.OpenFile("test.txt",os.O_CREATE|os.O_WRONLY,os.ModePerm)
	if err!=nil{
		panic(err)
	}
    //获取上下文,并设置请求信息
	c:=gogpt.NewContext().SetHeader(
		&gogpt.Header{
			Url : gogpt.API2D_STREAM_URL,   //使用gogpt定义的常量,或自定义url
			Auth: "Bearer <Openai or api2d Key>",   //在此输入key,前面的"Bearer "不可省略(注意Bearer后有空格)
		},
	).
	SetMaxTokens(200).      //设置单次请求最大tokens
    SetMaxTurns(2).         //设置最大交互回合数,默认为1,表示不进行上下文交互
	SetStream().            //设置使用流式请求方式
	SetOutput(file).        //设置输出流(传入一个io.Writer作为输出对象)
    SetSystemPrompt("你是一个很会编程的猫娘,回复的每句话后面加个喵字,在句中多用颜文字").   //利用系统对角色做先期设定
	Continue("银河系有多大")      //发送请求并接受回复

    c.Continue("请用go语言写出计算过程")       //发送第二次请求,此时发送的请求中已包括第一回合问答数据
}

输出结果:

银河系超级无敵大呢~ /(≧▽≦)/~喵
好的,以下是用 Go 语言写出计算过程的示例代码:

\```
package main

import "fmt"

func main() {
    lightYear := 9.46e12 // 1 光年等于 9.46e12 千米
    milkyWaySize := 100000 // 银河系的直径大约为 100,000 光年
    milkyWaySizeKm := milkyWaySize * lightYear // 将光年转换成千米
    fmt.Printf("银河系的直径大约为 %.2f 千米喵\n", milkyWaySizeKm)
}
\```

代码运行后输出:

\```
银河系的直径大约为 9,460,000,000,000,000.00 千米喵
\```

贡献

欢迎贡献代码或报告问题!请在提交 PR 之前确保您的代码通过了测试。

Documentation

Index

Constants

View Source
const (
	API2D_URL        = "https://openai.api2d.net/v1/chat/completions"
	API2D_STREAM_URL = "https://stream.api2d.net/v1/chat/completions"
	OPENAI_URL       = "https://api.openai.com/v1/chat/completions"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

func NewContext

func NewContext() *Context

func NewContextWithWriter added in v1.1.3

func NewContextWithWriter(writer io.Writer) *Context

func (*Context) Clear added in v1.1.4

func (c *Context) Clear()

func (*Context) Continue

func (c *Context) Continue(prompt string) *Context

func (*Context) GetContent

func (c *Context) GetContent() string

func (*Context) SetBody

func (c *Context) SetBody(opt map[string]any) *Context

func (*Context) SetHeader

func (c *Context) SetHeader(header *Header) *Context

func (*Context) SetMaxTokens added in v1.0.1

func (c *Context) SetMaxTokens(max_tokens int) *Context

func (*Context) SetMaxTurns added in v1.0.1

func (c *Context) SetMaxTurns(max_turns int) *Context

func (*Context) SetModel added in v1.1.1

func (c *Context) SetModel(model string) *Context

func (*Context) SetOutput added in v1.0.1

func (c *Context) SetOutput(output io.Writer) *Context

func (*Context) SetSystemPrompt

func (c *Context) SetSystemPrompt(prompt string) *Context

func (*Context) ToStreamContextWithCannel added in v1.1.3

func (c *Context) ToStreamContextWithCannel(ch chan string) *StreamContext

func (*Context) ToStreamContextWithWriter added in v1.1.3

func (c *Context) ToStreamContextWithWriter() *StreamContext
type Header struct {
	Url  string
	Auth string
}

type StreamContext added in v1.1.3

type StreamContext struct {
	*Context
	// contains filtered or unexported fields
}

func NewStreamContext added in v1.1.3

func NewStreamContext() *StreamContext

func NewStreamContextWithChannel added in v1.1.3

func NewStreamContextWithChannel(ch chan string) *StreamContext

func NewStreamContextWithWriter added in v1.1.3

func NewStreamContextWithWriter(writer io.Writer) *StreamContext

func (*StreamContext) Continue added in v1.1.3

func (c *StreamContext) Continue(prompt string) *StreamContext

func (*StreamContext) SetBody added in v1.1.3

func (c *StreamContext) SetBody(opt map[string]any) *StreamContext

func (*StreamContext) SetChannel added in v1.1.3

func (c *StreamContext) SetChannel(ch chan string) *StreamContext

func (*StreamContext) SetHeader added in v1.1.3

func (c *StreamContext) SetHeader(header *Header) *StreamContext

func (*StreamContext) SetMaxTokens added in v1.1.3

func (c *StreamContext) SetMaxTokens(max_tokens int) *StreamContext

func (*StreamContext) SetMaxTurns added in v1.1.3

func (c *StreamContext) SetMaxTurns(max_turns int) *StreamContext

func (*StreamContext) SetModel added in v1.1.3

func (c *StreamContext) SetModel(model string) *StreamContext

func (*StreamContext) SetOutput added in v1.1.3

func (c *StreamContext) SetOutput(output io.Writer) *StreamContext

func (*StreamContext) SetSystemPrompt added in v1.1.3

func (c *StreamContext) SetSystemPrompt(prompt string) *StreamContext

Jump to

Keyboard shortcuts

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