Documentation
¶
Index ¶
- Variables
- func DetailsToString(details []ChatDetail) string
- type ChatChoice
- type ChatCompletion
- type ChatDetail
- type ChatDetails
- type ChatError
- type ChatMessage
- type ChatUsage
- type Client
- type ConfigOption
- func WithAPIKey(i string) ConfigOption
- func WithAPIKeyFromYakitHome() ConfigOption
- func WithDomain(i string) ConfigOption
- func WithFunction(name, description string, opts ...ConfigOption) ConfigOption
- func WithFunctionParameterType(i string) ConfigOption
- func WithFunctionProperty(name, typ, description string, enum ...[]string) ConfigOption
- func WithFunctionRequired(names ...string) ConfigOption
- func WithModel(i string) ConfigOption
- func WithProxy(i string) ConfigOption
- func WithYakDomain() ConfigOption
- type FuncReturn
- type Function
- type FunctionCall
- type Parameters
- type Property
- type Session
- type Tool
- type ToolCall
Constants ¶
This section is empty.
Variables ¶
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": NewSystemChatDetail, "userMessage": NewUserChatDetail, "assistantMessage": NewAIChatDetail, "toolMessage": NewToolChatDetail, "toolMessageWithID": NewToolChatDetailWithID, }
Functions ¶
func DetailsToString ¶
func DetailsToString(details []ChatDetail) string
Types ¶
type ChatChoice ¶
type ChatChoice struct { Index int `json:"index"` Message ChatDetail `json:"message"` FinishReason string `json:"finish_reason"` }
type ChatCompletion ¶
type ChatDetail ¶
type ChatDetail struct { Role string `json:"role"` Name string `json:"name,omitempty"` Content string `json:"content"` ToolCalls []*ToolCall `json:"tool_calls,omitempty"` ToolCallID string `json:"tool_call_id,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` }
func NewAIChatDetail ¶
func NewAIChatDetail(content string) ChatDetail
assistantMessage 根据传入的内容构造并返回一个 OpenAI 助手信息 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ ```
func NewSystemChatDetail ¶
func NewSystemChatDetail(content string) ChatDetail
systemMessage 根据传入的内容构造并返回一个 OpenAI 系统信息 Example: ``` d = openai.ChatEx( [ openai.systemMessage("The weather in Boston is 72 degrees and sunny."), openai.userMessage("What is the weather like today?"), ], )~ ```
func NewToolChatDetail ¶
func NewToolChatDetail(name, content string) ChatDetail
toolMessage 根据传入的函数名,内容构造并返回一个 OpenAI 工具信息,用于指示工具返回结果 Example: ``` session = openai.NewSession( openai.proxy("http://127.0.0.1:7890") ) result = session.Chat(openai.userMessage("What is the weather like in Boston?"), 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"), ), )~ result = session.Chat(openai.toolMessage("get_current_weather", `{"degree":72,"weather":"sunny"}`))~ println(result.String()) ```
func NewToolChatDetailWithID ¶
func NewToolChatDetailWithID(id, name, content string) ChatDetail
toolMessageWithID 根据传入的ID,函数名,内容构造并返回一个 OpenAI 工具信息,用于指示工具返回结果 Example: ``` session = openai.NewSession( openai.proxy("http://127.0.0.1:7890") ) result = session.Chat(openai.userMessage("What is the weather like in Boston?"), 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"), ), )~ result = session.Chat(openai.toolMessage("get_current_weather", `{"degree":72,"weather":"sunny"}`))~ println(result.String()) ```
func NewUserChatDetail ¶
func NewUserChatDetail(content string) ChatDetail
userMessage 根据传入的内容构造并返回一个 OpenAI 用户信息 Example: ``` d = openai.ChatEx( [ openai.systemMessage("The weather in Boston is 72 degrees and sunny."), openai.userMessage("What is the weather like today?"), ], )~ ```
type ChatDetails ¶
type ChatDetails []ChatDetail
func (ChatDetails) ChatMessages ¶
func (details ChatDetails) ChatMessages() []ChatDetail
ChatMessages 返回一个 ChatDetail 切片 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ println(d.ChatMessages()) ```
func (ChatDetails) FirstString ¶
func (details ChatDetails) FirstString() string
String 返回第一个消息 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ println(d.String()) ```
func (ChatDetails) FunctionCallResult ¶
func (details ChatDetails) FunctionCallResult() map[string]any
FunctionCallResult 返回函数调用的结果 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like in Boston?") ], 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"), ), openai.proxy("http://127.0.0.1:7890"), )~ println(d.FunctionCallResult()) ```
func (ChatDetails) String ¶
func (details ChatDetails) String() string
String 返回消息切片中包含的所有消息 Example: ``` d = openai.ChatEx( [ openai.userMessage("What is the weather like today?"), openai.assistantMessage("72 degrees and sunny."), openai.userMessage("What will the temperature be tomorrow?"), ], )~ println(d.String()) ```
type ChatMessage ¶
type ChatMessage struct { Model string `json:"model"` Messages []ChatDetail `json:"messages"` Tools []Tool `json:"tools,omitempty"` }
func NewChatMessage ¶
func NewChatMessage(model string, messages []ChatDetail, funcs ...Function) *ChatMessage
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 []Function Parameters Parameters }
func NewOpenAIClient ¶
func NewOpenAIClient(opts ...ConfigOption) *Client
func NewRawOpenAIClient ¶
func NewRawOpenAIClient(opts ...ConfigOption) *Client
func (*Client) ChatEx ¶
func (c *Client) ChatEx(messages []ChatDetail, funcs ...Function) ([]ChatChoice, 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 ¶ added in v1.3.1
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 ¶ added in v1.3.1
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 ¶ added in v1.3.1
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 ¶ added in v1.3.1
func WithYakDomain() ConfigOption
yakDomain 设置 OpenAI的第三方加速域名为 Yaklang.io 提供的第三方加速域名,用于加速访问 Example: ``` result = openai.TranslateToChinese("Hello, world!", openai.apiKey("sk-xxx"), openai.yakDomain()) ```
type FuncReturn ¶
type Function ¶ added in v1.3.1
type Function struct { Name string `json:"name"` Description string `json:"description"` Parameters Parameters `json:"parameters"` }
type FunctionCall ¶ added in v1.3.1
! 已弃用
type Parameters ¶ added in v1.3.1
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 ChatDetail, opts ...ConfigOption) (ChatDetails, error)
type ToolCall ¶
type ToolCall struct { ID string `json:"id"` Type string `json:"type"` Function FuncReturn `json:"function"` }