Documentation
¶
Overview ¶
Package ai provide LLM Function Calling features
Index ¶
- Constants
- Variables
- func DecodeRequest[T any](r *http.Request, w http.ResponseWriter, logger *slog.Logger) (T, error)
- func DecorateHandler(h http.Handler, decorates ...func(handler http.Handler) http.Handler) http.Handler
- func FromTracerContext(ctx context.Context) trace.Tracer
- func FromTransIDContext(ctx context.Context) string
- func NewReducer(conn *mem.FrameConn, cred *auth.Credential) yomo.StreamFunction
- func NewServeMux(h *Handler) *http.ServeMux
- func NewSource(conn *mem.FrameConn, cred *auth.Credential) yomo.Source
- func RespondWithError(w http.ResponseWriter, code int, err error, logger *slog.Logger)
- func Serve(config *Config, logger *slog.Logger, source yomo.Source, ...) error
- func SetStreamHeader(w http.ResponseWriter) http.Header
- func WithCallerContext(ctx context.Context, caller *Caller) context.Context
- func WithTracerContext(ctx context.Context, tracer trace.Tracer) context.Context
- func WithTransIDContext(ctx context.Context, transID string) context.Context
- type BasicAPIServer
- type CallSyncer
- type Caller
- type Config
- type EventResponseWriter
- type Handler
- type Provider
- type ReduceMessage
- type Server
- type Service
- func (srv *Service) GetChatCompletions(ctx context.Context, req openai.ChatCompletionRequest, transID string, ...) error
- func (srv *Service) GetInvoke(ctx context.Context, userInstruction, baseSystemMessage, transID string, ...) (*ai.InvokeResponse, error)
- func (srv *Service) LoadOrCreateCaller(r *http.Request) (*Caller, error)
- type ServiceOptions
- type StreamRecorder
- type SystemPromptOp
- type ToolCallResult
Constants ¶
const (
// DefaultZipperAddr is the default endpoint of the zipper
DefaultZipperAddr = "localhost:9000"
)
Variables ¶
var ( // ErrConfigNotFound is the error when the ai config was not found ErrConfigNotFound = errors.New("ai config was not found") // ErrConfigFormatError is the error when the ai config format is incorrect ErrConfigFormatError = errors.New("ai config format is incorrect") )
var ( // RequestTimeout is the timeout for the request, default is 90 seconds RequestTimeout = 90 * time.Second // RunFunctionTimeout is the timeout for awaiting the function response, default is 60 seconds RunFunctionTimeout = 60 * time.Second )
Functions ¶
func DecodeRequest ¶ added in v1.18.11
DecodeRequest decodes the request body into given type.
func DecorateHandler ¶ added in v1.18.14
func DecorateHandler(h http.Handler, decorates ...func(handler http.Handler) http.Handler) http.Handler
DecorateHandler decorates the http.Handler.
func FromTracerContext ¶ added in v1.18.18
FromTransIDContext returns the transID from the request context
func FromTransIDContext ¶ added in v1.18.8
FromTransIDContext returns the transID from the request context
func NewReducer ¶ added in v1.19.2
func NewReducer(conn *mem.FrameConn, cred *auth.Credential) yomo.StreamFunction
NewReducer creates a new instance of memory StreamFunction.
func NewServeMux ¶ added in v1.18.14
NewServeMux creates a new http.ServeMux for the llm bridge server.
func RespondWithError ¶ added in v1.18.7
RespondWithError writes an error to response according to the OpenAI API spec.
func Serve ¶
func Serve(config *Config, logger *slog.Logger, source yomo.Source, reducer yomo.StreamFunction) error
Serve starts the Basic API Server
func SetStreamHeader ¶ added in v1.19.6
func SetStreamHeader(w http.ResponseWriter) http.Header
func WithCallerContext ¶ added in v1.18.11
WithCallerContext adds the caller to the request context
func WithTracerContext ¶ added in v1.18.18
WithTracerContext adds the tracer to the request context
Types ¶
type BasicAPIServer ¶
type BasicAPIServer struct {
// contains filtered or unexported fields
}
BasicAPIServer provides restful service for end user
func NewBasicAPIServer ¶
func NewBasicAPIServer(config *Config, provider provider.LLMProvider, source yomo.Source, reducer yomo.StreamFunction, logger *slog.Logger) (*BasicAPIServer, error)
NewBasicAPIServer creates a new restful service
type CallSyncer ¶ added in v1.18.11
type CallSyncer interface { // Call fires a bunch of function callings, and wait the result of these function callings. // The result only contains the messages with role=="tool". // If some function callings failed, the content will be returned as the failed reason. Call(ctx context.Context, transID string, reqID string, toolCalls []openai.ToolCall) ([]ToolCallResult, error) // Close close the CallSyncer. if close, you can't use this CallSyncer anymore. Close() error }
CallSyncer fires a bunch of function callings, and wait the result of these function callings. every tool call has a toolCallID, which is used to identify the function calling, Note that one tool call can only be responded once.
func NewCallSyncer ¶ added in v1.18.11
func NewCallSyncer(logger *slog.Logger, sourceCh chan<- ai.FunctionCall, reduceCh <-chan ReduceMessage, timeout time.Duration) CallSyncer
NewCallSyncer creates a new CallSyncer.
type Caller ¶ added in v1.18.11
type Caller struct { CallSyncer // contains filtered or unexported fields }
Caller calls the invoke function and keeps the metadata and system prompt.
func FromCallerContext ¶ added in v1.18.11
FromCallerContext returns the caller from the request context
func NewCaller ¶ added in v1.18.11
func NewCaller(source yomo.Source, reducer yomo.StreamFunction, md metadata.M, callTimeout time.Duration) (*Caller, error)
NewCaller returns a new caller.
func (*Caller) GetSystemPrompt ¶ added in v1.18.13
func (c *Caller) GetSystemPrompt() (prompt string, op SystemPromptOp)
GetSystemPrompt gets the system prompt
func (*Caller) SetSystemPrompt ¶ added in v1.18.11
func (c *Caller) SetSystemPrompt(prompt string, op SystemPromptOp)
SetSystemPrompt sets the system prompt
type Config ¶
type Config struct { Server Server `yaml:"server"` // Server is the configuration of the BasicAPIServer Providers map[string]Provider `yaml:"providers"` // Providers is the configuration of llm provider }
Config is the configuration of AI bridge. The configuration looks like:
bridge:
ai: server: host: http://localhost port: 8000 credential: token:<CREDENTIAL> provider: openai providers: azopenai: api_endpoint: https://<RESOURCE>.openai.azure.com deployment_id: <DEPLOYMENT_ID> api_key: <API_KEY> api_version: <API_VERSION> openai: api_key: api_endpoint: gemini: api_key: cloudflare_azure: endpoint: https://gateway.ai.cloudflare.com/v1/<CF_GATEWAY_ID>/<CF_GATEWAY_NAME> api_key: <AZURE_API_KEY> resource: <AZURE_OPENAI_RESOURCE> deployment_id: <AZURE_OPENAI_DEPLOYMENT_ID> api_version: <AZURE_OPENAI_API_VERSION>
type EventResponseWriter ¶ added in v1.19.6
type EventResponseWriter interface { // EventResponseWriter should implement http.ResponseWriter http.ResponseWriter // EventResponseWriter should implement http.Flusher http.Flusher // EventResponseWriter should implement StreamRecorder StreamRecorder // SetStreamHeader sets the stream headers of the underlying ResponseWriter. SetStreamHeader() http.Header // WriteStreamEvent writes the event to the underlying ResponseWriter. WriteStreamEvent(any) error // WriteStreamDone writes the done event to the underlying ResponseWriter. WriteStreamDone() error }
EventResponseWriter is the interface for writing events to the underlying ResponseWriter.
func NewResponseWriter ¶ added in v1.18.18
func NewResponseWriter(w http.ResponseWriter) EventResponseWriter
NewResponseWriter returns a new ResponseWriter.
type Handler ¶ added in v1.18.13
type Handler struct {
// contains filtered or unexported fields
}
Handler handles the http request.
func NewHandler ¶ added in v1.18.13
NewHandler return a hander that handles chat completions requests.
func (*Handler) HandleChatCompletions ¶ added in v1.18.13
func (h *Handler) HandleChatCompletions(w http.ResponseWriter, r *http.Request)
HandleChatCompletions is the handler for POST /chat/completions
func (*Handler) HandleInvoke ¶ added in v1.18.13
func (h *Handler) HandleInvoke(w http.ResponseWriter, r *http.Request)
HandleInvoke is the handler for POST /invoke
func (*Handler) HandleOverview ¶ added in v1.18.13
func (h *Handler) HandleOverview(w http.ResponseWriter, r *http.Request)
HandleOverview is the handler for GET /overview
type ReduceMessage ¶ added in v1.18.13
type ReduceMessage struct { // ReqID indentifies the message. ReqID string // Message is the message. Message openai.ChatCompletionMessage }
ReduceMessage is the message from the reducer.
type Server ¶
type Server struct { Addr string `yaml:"addr"` // Addr is the address of the server Provider string `yaml:"provider"` // Provider is the llm provider to use }
Server is the configuration of the BasicAPIServer, which is the endpoint for end user access
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the service layer for llm bridge server. service is responsible for handling the logic from handler layer.
func NewService ¶
func NewService(provider provider.LLMProvider, opt *ServiceOptions) *Service
NewService creates a new service for handling the logic from handler layer.
func (*Service) GetChatCompletions ¶
func (srv *Service) GetChatCompletions(ctx context.Context, req openai.ChatCompletionRequest, transID string, caller *Caller, w EventResponseWriter, tracer trace.Tracer) error
GetChatCompletions accepts openai.ChatCompletionRequest and responds to http.ResponseWriter.
type ServiceOptions ¶ added in v1.18.14
type ServiceOptions struct { // Logger is the logger for the service Logger *slog.Logger // CredentialFunc is the function for getting the credential from the request CredentialFunc func(r *http.Request) (string, error) // CallerCacheSize is the size of the caller's cache CallerCacheSize int // CallerCacheTTL is the time to live of the callers cache CallerCacheTTL time.Duration // CallerCallTimeout is the timeout for awaiting the function response. CallerCallTimeout time.Duration // SourceBuilder should builds an unconnected source. SourceBuilder func(credential string) yomo.Source // ReducerBuilder should builds an unconnected reducer. ReducerBuilder func(credential string) yomo.StreamFunction // MetadataExchanger exchanges metadata from the credential. MetadataExchanger func(credential string) (metadata.M, error) }
ServiceOptions is the option for creating service
type StreamRecorder ¶ added in v1.19.6
type StreamRecorder interface { // RecordIsStream records the stream status of the ResponseWriter. RecordIsStream(bool) // IsStream returns the stream status of the ResponseWriter. IsStream() bool // RecordError records the error of the request. RecordError(error) // GetError returns the error of the request. GetError() error // RecordTTFT records the TTFT(Time to First Token) of the request. RecordTTFT(time.Time) // GetTTFT returns the TTFT(Time to First Token) of the request. GetTTFT() time.Time }
StreamRecorder records the stream status of the ResponseWriter.
func NewStreamRecorder ¶ added in v1.19.6
func NewStreamRecorder() StreamRecorder
NewStreamRecorder returns a new StreamRecorder.
type SystemPromptOp ¶ added in v1.18.15
type SystemPromptOp int
SystemPromptOp defines the operation of system prompt
const ( SystemPromptOpOverwrite SystemPromptOp = 0 SystemPromptOpDisabled SystemPromptOp = 1 SystemPromptOpPrefix SystemPromptOp = 2 )
type ToolCallResult ¶ added in v1.19.6
type ToolCallResult struct { // FunctionName is the name of the function calling. FunctionName string // ToolCallID is the tool call id. ToolCallID string // Content is the result of the function calling. Content string }
ToolCallResult is the result of a CallSyncer.Call()
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package provider defines the ai.Provider interface and provides a mock provider for unittest.
|
Package provider defines the ai.Provider interface and provides a mock provider for unittest. |
anthropic
Package anthropic is the anthropic llm provider, see https://docs.anthropic.com
|
Package anthropic is the anthropic llm provider, see https://docs.anthropic.com |
azopenai
Package azopenai is used to provide the Azure OpenAI service
|
Package azopenai is used to provide the Azure OpenAI service |
cerebras
Package cerebras is the Cerebras llm provider
|
Package cerebras is the Cerebras llm provider |
cfazure
Package cfazure is used to provide the Azure OpenAI service
|
Package cfazure is used to provide the Azure OpenAI service |
cfopenai
Package cfopenai is used to provide the Azure OpenAI service
|
Package cfopenai is used to provide the Azure OpenAI service |
deepseek
Package deepseek is the DeepSeek llm provider
|
Package deepseek is the DeepSeek llm provider |
gemini
Package gemini is used to provide the gemini service
|
Package gemini is used to provide the gemini service |
githubmodels
Package githubmodels is the Github Models llm provider, see https://github.com/marketplace/models
|
Package githubmodels is the Github Models llm provider, see https://github.com/marketplace/models |
ollama
Package ollama is used to provide the Ollama service for YoMo Bridge.
|
Package ollama is used to provide the Ollama service for YoMo Bridge. |
openai
Package openai is the OpenAI llm provider
|
Package openai is the OpenAI llm provider |
vertexai
Package vertexai is used to provide the vertexai service
|
Package vertexai is used to provide the vertexai service |
vllm
Package vllm is the vllm llm provider
|
Package vllm is the vllm llm provider |
xai
Package xai is the x.ai provider
|
Package xai is the x.ai provider |
Package register provides a register for registering and unregistering functions
|
Package register provides a register for registering and unregistering functions |