openai

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Exports = map[string]interface{}{
	"TranslateToChinese": translate,
	"FunctionCall":       functionCall,
	"Chat":               chat,
	"ChatEx":             chatEx,
	"NewSession":         NewSession,

	"apiKey":      WithAPIKey,
	"localAPIKey": WithAPIKeyFromYakitHome,
	"proxy":       WithProxy,
	"domain":      WithDomain,
	"yakDomain":   WithYakDomain,
	"model":       WithModel,

	"newFunction":       WithFunction,
	"functionParamType": WithFunctionParameterType,
	"functionProperty":  WithFunctionProperty,
	"functionRequired":  WithFunctionRequired,

	"systemMessage":     aispec.NewSystemChatDetail,
	"userMessage":       aispec.NewUserChatDetail,
	"assistantMessage":  aispec.NewAIChatDetail,
	"toolMessage":       aispec.NewToolChatDetail,
	"toolMessageWithID": aispec.NewToolChatDetailWithID,
}

Functions

func ExtractDataByAi

func ExtractDataByAi(data string, desc string, params map[string]string, opts ...ConfigOption) (map[string]any, error)

Types

type Client

type Client struct {
	Proxy        string
	APIKey       string
	Organization string
	ChatModel    string

	// Role in Org! public model, the role is user
	Role   string
	Domain string

	// function call
	Functions  []aispec.Function
	Parameters aispec.Parameters
}

func NewOpenAIClient

func NewOpenAIClient(opts ...ConfigOption) *Client

func NewRawOpenAIClient

func NewRawOpenAIClient(opts ...ConfigOption) *Client

func (*Client) Chat

func (c *Client) Chat(data string, funcs ...aispec.Function) (string, error)

func (*Client) ChatEx

func (c *Client) ChatEx(messages []aispec.ChatDetail, funcs ...aispec.Function) ([]aispec.ChatChoice, error)

func (*Client) TranslateToChinese

func (c *Client) TranslateToChinese(data string) (string, error)

type ConfigOption

type ConfigOption func(client *Client)

func WithAPIKey

func WithAPIKey(i string) ConfigOption

apiKey 设置 OpenAI的API Key Example: ``` result = openai.TranslateToChinese("Hello, world!", openai.apiKey("sk-xxx")) ```

func WithAPIKeyFromYakitHome

func WithAPIKeyFromYakitHome() ConfigOption

localAPIKey 从 $YAKIT_HOME/openai-key.txt 中获取 API Key Example: ``` result = openai.TranslateToChinese("Hello, world!", openai.apiKeyFromYakitHome()) ```

func WithDomain

func WithDomain(i string) ConfigOption

domain 设置 OpenAI的第三方加速域名,用于加速访问 Example: ``` result = openai.TranslateToChinese("Hello, world!", openai.apiKey("sk-xxx"), openai.domain("api.ai.yaklang.com")) ```

func WithFunction

func WithFunction(name, description string, opts ...ConfigOption) ConfigOption

newFunction 设置新的函数调用 详情请参考 https://platform.openai.com/docs/guides/function-calling @param {string} name 函数名称 @param {string} description 函数描述 @param {ConfigOption} ...opts 配置选项,接收openai.functionParamType,openai.functionProperty,openai.functionRequired @return {ConfigOption} 配置选项 Example: ``` f = openai.newFunction( "get_current_weather", "Get the current weather in a given location", openai.functionProperty("location", "string", "The city and state, e.g. San Francisco, CA"), openai.functionRequired("location"), ) d = openai.ChatEx( [ openai.userMessage("What is the weather like in Boston?") ], f, openai.proxy("http://127.0.0.1:7890"), )~ println(d.FunctionCallResult()) ```

func WithFunctionParameterType

func WithFunctionParameterType(i string) ConfigOption

functionParamType 设置函数调用时的参数类型,默认为 "object" Example: ``` resultMap = openai.FunctionCall( "What is the weather like in Boston?", "get_current_weather", "Get the current weather in a given location", openai.apiKey("sk-xxxx"), openai.proxy("http://127.0.0.1:7890"), openai.functionProperty("location", "string", "The city and state, e.g. San Francisco, CA"), openai.functionRequired("location")) ```

func WithFunctionProperty

func WithFunctionProperty(name, typ, description string, enum ...[]string) ConfigOption

functionProperty 设置函数调用时的单个参数属性 Example: ``` resultMap = openai.FunctionCall( "What is the weather like in Boston?", "get_current_weather", "Get the current weather in a given location", openai.apiKey("sk-xxxx"), openai.proxy("http://127.0.0.1:7890"), openai.functionProperty("location", "string", "The city and state, e.g. San Francisco, CA"), openai.functionRequired("location")) ```

func WithFunctionRequired

func WithFunctionRequired(names ...string) ConfigOption

functionRequired 设置函数调用时的必须参数 Example: ``` resultMap = openai.FunctionCall( "What is the weather like in Boston?", "get_current_weather", "Get the current weather in a given location", openai.apiKey("sk-xxxx"), openai.proxy("http://127.0.0.1:7890"), openai.functionProperty("location", "string", "The city and state, e.g. San Francisco, CA"), openai.functionRequired("location")) ```

func WithModel

func WithModel(i string) ConfigOption

model 设置 OpenAI的大语言模型 Example: ``` result = openai.TranslateToChinese("Hello, world!", openai.apiKey("sk-xxx"), openai.model("gpt-4-0613")) ```

func WithProxy

func WithProxy(i string) ConfigOption

proxy 设置调用 OpenAI 时使用的代理 Example: ``` result = openai.TranslateToChinese("Hello, world!", openai.apiKey("sk-xxx"), openai.proxy("http://127.0.0.1:7890")) ```

func WithYakDomain

func WithYakDomain() ConfigOption

yakDomain 设置 OpenAI的第三方加速域名为 Yaklang.io 提供的第三方加速域名,用于加速访问 Example: ``` result = openai.TranslateToChinese("Hello, world!", openai.apiKey("sk-xxx"), openai.yakDomain()) ```

type GetawayClient

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

func (*GetawayClient) BuildHTTPOptions

func (g *GetawayClient) BuildHTTPOptions() ([]poc.PocConfigOption, error)

func (*GetawayClient) Chat

func (g *GetawayClient) Chat(s string, function ...aispec.Function) (string, error)

func (*GetawayClient) ChatEx

func (g *GetawayClient) ChatEx(details []aispec.ChatDetail, function ...aispec.Function) ([]aispec.ChatChoice, error)

func (*GetawayClient) ChatStream

func (g *GetawayClient) ChatStream(s string) (io.Reader, error)

func (*GetawayClient) ExtractData

func (g *GetawayClient) ExtractData(msg string, desc string, fields map[string]any) (map[string]any, error)

func (*GetawayClient) LoadOption

func (g *GetawayClient) LoadOption(opt ...aispec.AIConfigOption)

type Session

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

func NewSession

func NewSession(opt ...ConfigOption) *Session

func (*Session) Chat

func (s *Session) Chat(message aispec.ChatDetail, opts ...ConfigOption) (aispec.ChatDetails, error)

Jump to

Keyboard shortcuts

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