aicontext

package
v2.9.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextResults

func ContextResults() []string

Types

type Context

type Context struct {
	Ctx      *context.Context
	Provider *ProviderSpec

	// Req is the original request from the user. It's body is in ReqBody.
	Req       *httpprot.Request
	ReqBody   []byte
	ReqInfo   *protocol.GeneralRequest
	OpenAIReq map[string]any
	RespType  ResponseType

	// ParseMetricFn is a function that parses the response body to a metric.
	// If it is sent, it will be called to parse the response body to a metric.
	// Otherwise, default ParseMetricFn will be used.
	ParseMetricFn func(fc *FinishContext) *metricshub.Metric
	// contains filtered or unexported fields
}

func New

func New(ctx *context.Context, provider *ProviderSpec) (*Context, error)

func (*Context) AddCallBack

func (c *Context) AddCallBack(cb func(fc *FinishContext))

AddCallBack adds a callback function to the context. The callback will be called when the reponse is sent to the user.

func (*Context) Callbacks

func (c *Context) Callbacks() []func(fc *FinishContext)

CallBacks returns all callback functions registered in the context.

func (*Context) GetResponse

func (c *Context) GetResponse() *Response

GetResponse returns the response of the context.

func (*Context) IsStopped

func (c *Context) IsStopped() bool

IsStopped checks if the context execution is stopped.

func (*Context) Result

func (c *Context) Result() ResultError

Result returns the result of the context execution.

func (*Context) SetResponse

func (c *Context) SetResponse(resp *Response)

SetResponse sets the response of the context. If you need to close the response body, you should add a callback function to the context using AddCallBack method.

func (*Context) Stop

func (c *Context) Stop(result ResultError)

Stop stops the context execution and sets the result to be returned.

type FinishContext

type FinishContext struct {
	StatusCode int
	Header     http.Header
	// RespBody is the final response body that sent to the user.
	RespBody []byte
	Duration int64
}

type ProviderSpec

type ProviderSpec struct {
	Name         string            `json:"name"`
	ProviderType string            `json:"providerType"`
	BaseURL      string            `json:"baseURL"`
	APIKey       string            `json:"apiKey"`
	Headers      map[string]string `json:"headers,omitempty"`
	// Optional parameters for specific providers, such as Azure.
	Endpoint     string `json:"endpoint,omitempty"`     // It is used for Azure OpenAI.
	DeploymentID string `json:"deploymentID,omitempty"` // It is used for Azure OpenAI.
	APIVersion   string `json:"apiVersion,omitempty"`   // It is used for Azure OpenAI.
}

ProviderSpec defines the specification for an AI provider.

type Response

type Response struct {
	StatusCode int
	// ContentLength records the length of the associated content. The
	// value -1 indicates that the length is unknown. Unless Request.Method
	// is "HEAD", values >= 0 indicate that the given number of bytes may
	// be read from Body.
	ContentLength int64
	Header        http.Header
	// BodyReader is the response body reader. Only one of BodyReader or BodyBytes should be set.
	BodyReader io.Reader
	// BodyBytes is the response body bytes. Only one of BodyReader or BodyBytes should be set.
	BodyBytes []byte
}

type ResponseType

type ResponseType string

ResponseType defines the type of response for AI requests.

const (
	// ResponseTypeCompletions is used for standard completion requests.
	ResponseTypeCompletions ResponseType = "/v1/completions"
	// ResponseTypeChatCompletions is used for chat completion requests.
	ResponseTypeChatCompletions ResponseType = "/v1/chat/completions"
	// ResponseTypeEmbeddings is used for embedding requests.
	ResponseTypeEmbeddings ResponseType = "/v1/embeddings"
	// ResponseTypeModels is used for model listing requests.
	ResponseTypeModels ResponseType = "/v1/models"
	// ResponseTypeImageGenerations is used for image generation requests.
	ResponseTypeImageGenerations ResponseType = "/v1/images/generations"
)

type ResultError

type ResultError string
const (
	// ResultOk indicates a successful request with no errors.
	// In easegress, it is represented as an empty string.
	ResultOk               ResultError = ""
	ResultInternalError    ResultError = "internalError"
	ResultClientError      ResultError = "clientError"
	ResultServerError      ResultError = "serverError"
	ResultFailureCodeError ResultError = "failureCodeError"
	ResultProviderError    ResultError = "providerError"
	ResultMiddlewareError  ResultError = "middlewareError"
)

Jump to

Keyboard shortcuts

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