aispec

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: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChatBase

func ChatBase(url string, model string, msg string, fs []Function, opt func() ([]poc.PocConfigOption, error), streamHandler func(io.Reader)) (string, error)

func ChatBasedExtractData

func ChatBasedExtractData(url string, model string, msg string, fields map[string]any, opt func() ([]poc.PocConfigOption, error), streamHandler func(io.Reader)) (map[string]any, error)

func ChatWithStream

func ChatWithStream(url string, model string, msg string, opt func() ([]poc.PocConfigOption, error)) (io.Reader, error)

func DetailsToString

func DetailsToString(details []ChatDetail) string

func ExtractDataBase

func ExtractDataBase(
	url string, model string, input string,
	description string, paramRaw map[string]any,
	opt func() ([]poc.PocConfigOption, error),
	streamHandler func(io.Reader),
) (map[string]any, error)

func Register

func Register(name string, gateway func() AIGateway)

Types

type AIConfig

type AIConfig struct {
	// gateway network config
	BaseURL string
	Domain  string
	NoHttps bool

	// basic model
	Model    string
	Timeout  float64
	Deadline time.Time

	APIKey        string
	Proxy         string
	StreamHandler func(io.Reader)
	Type          string
}

func NewDefaultAIConfig

func NewDefaultAIConfig(opts ...AIConfigOption) *AIConfig

type AIConfigOption

type AIConfigOption func(*AIConfig)

func WithAPIKey

func WithAPIKey(k string) AIConfigOption

func WithBaseURL

func WithBaseURL(baseURL string) AIConfigOption

func WithDebugStream

func WithDebugStream(h ...bool) AIConfigOption

func WithDomain

func WithDomain(domain string) AIConfigOption

func WithModel

func WithModel(model string) AIConfigOption

func WithNoHttps

func WithNoHttps(b bool) AIConfigOption

func WithProxy

func WithProxy(p string) AIConfigOption

func WithStreamHandler

func WithStreamHandler(h func(io.Reader)) AIConfigOption

func WithTimeout

func WithTimeout(timeout float64) AIConfigOption

func WithType

func WithType(t string) AIConfigOption

type AIGateway

type AIGateway interface {
	Chatter
	FunctionCaller
	Configurable
}

func Lookup

func Lookup(name string) (AIGateway, bool)

type ChatChoice

type ChatChoice struct {
	Index        int        `json:"index"`
	Message      ChatDetail `json:"message"`
	FinishReason string     `json:"finish_reason"`
}

func ChatExBase

func ChatExBase(url string, model string, details []ChatDetail, function []Function, opt func() ([]poc.PocConfigOption, error), streamHandler func(closer io.Reader)) ([]ChatChoice, error)

type ChatCompletion

type ChatCompletion struct {
	ID      string       `json:"id"`
	Object  string       `json:"object"`
	Created int64        `json:"created"`
	Choices []ChatChoice `json:"choices"`
	Usage   ChatUsage    `json:"usage"`
	Error   *ChatError   `json:"error,omitempty"`
}

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 ChatError

type ChatError struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Param   string `json:"param"`
	Code    string `json:"code"`
}

type ChatMessage

type ChatMessage struct {
	Model    string       `json:"model"`
	Messages []ChatDetail `json:"messages"`
	Tools    []Tool       `json:"tools,omitempty"`
	Stream   bool         `json:"stream"`
}

func NewChatMessage

func NewChatMessage(model string, messages []ChatDetail, funcs ...Function) *ChatMessage

type ChatUsage

type ChatUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

type Chatter

type Chatter interface {
	Chat(string, ...Function) (string, error)
	ChatEx([]ChatDetail, ...Function) ([]ChatChoice, error)
	ChatStream(string) (io.Reader, error)
}

type Configurable

type Configurable interface {
	LoadOption(opt ...AIConfigOption)
	BuildHTTPOptions() ([]poc.PocConfigOption, error)
}

type FuncReturn

type FuncReturn struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type Function

type Function struct {
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Parameters  Parameters `json:"parameters"`
}

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

! 已弃用

type FunctionCaller

type FunctionCaller interface {
	ExtractData(data string, desc string, fields map[string]any) (map[string]any, error)
}

type Parameters

type Parameters struct {
	Type       string              `json:"type"`
	Properties map[string]Property `json:"properties"`
	Required   []string            `json:"required"`
}

type Property

type Property struct {
	Type        string   `json:"type"`
	Description string   `json:"description"`
	Enum        []string `json:"enum,omitempty"`
}

type Tool

type Tool struct {
	Type     string   `json:"type"`
	Function Function `json:"function"`
}

type ToolCall

type ToolCall struct {
	ID       string     `json:"id"`
	Type     string     `json:"type"`
	Function FuncReturn `json:"function"`
}

Jump to

Keyboard shortcuts

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