106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.ilapage.cn/OPC/chorus/internal/core/model"
|
|
)
|
|
|
|
type FailureClass string
|
|
|
|
const (
|
|
FailureRateLimited FailureClass = "rate_limited"
|
|
FailureServer FailureClass = "server_error"
|
|
FailureTimeout FailureClass = "timeout"
|
|
FailureConnection FailureClass = "connection_error"
|
|
FailureBadRequest FailureClass = "bad_request"
|
|
FailureUnauthorized FailureClass = "unauthorized"
|
|
FailurePolicyRejected FailureClass = "policy_rejected"
|
|
FailureOther FailureClass = "other"
|
|
)
|
|
|
|
type ErrorCode string
|
|
|
|
const (
|
|
CodeRateLimited ErrorCode = "upstream_rate_limited"
|
|
CodeServerError ErrorCode = "upstream_server_error"
|
|
CodeTimeout ErrorCode = "upstream_timeout"
|
|
CodeConnection ErrorCode = "upstream_connection_error"
|
|
CodeBadRequest ErrorCode = "upstream_bad_request"
|
|
CodeUnauthorized ErrorCode = "upstream_unauthorized"
|
|
CodePolicyRejected ErrorCode = "upstream_policy_rejected"
|
|
CodeUnknown ErrorCode = "upstream_unknown"
|
|
)
|
|
|
|
type Error struct {
|
|
Code ErrorCode
|
|
Class FailureClass
|
|
Message string
|
|
}
|
|
|
|
// AuthType is deliberately small. Provider configuration must not supply
|
|
// arbitrary request headers because credentials are a security boundary.
|
|
type AuthType string
|
|
|
|
const (
|
|
AuthNone AuthType = "none"
|
|
AuthBearer AuthType = "bearer"
|
|
AuthGoogleAPIKey AuthType = "x-goog-api-key"
|
|
)
|
|
|
|
func (a AuthType) Valid() bool {
|
|
return a == AuthNone || a == AuthBearer || a == AuthGoogleAPIKey
|
|
}
|
|
|
|
func (e *Error) Error() string {
|
|
return fmt.Sprintf("provider request failed: %s", e.Code)
|
|
}
|
|
|
|
type Input struct {
|
|
StorageKey string
|
|
MIMEType string
|
|
Role model.InputRole
|
|
Position uint32
|
|
Content []byte
|
|
}
|
|
|
|
type Request struct {
|
|
Kind model.GenerationKind
|
|
APIType model.APIType
|
|
ModelID string
|
|
RenderedPrompt string
|
|
Inputs []Input
|
|
}
|
|
|
|
type Output struct {
|
|
Kind model.GenerationKind
|
|
Text string
|
|
Content []byte
|
|
ContentType string
|
|
}
|
|
|
|
type Client interface {
|
|
Generate(ctx context.Context, request Request) ([]Output, error)
|
|
}
|
|
|
|
type HTTPRequest struct {
|
|
Method string
|
|
URL string
|
|
Header http.Header
|
|
Body []byte
|
|
MaxBytes int64
|
|
}
|
|
|
|
type HTTPResponse struct {
|
|
StatusCode int
|
|
ContentType string
|
|
Body []byte
|
|
}
|
|
|
|
type HTTPClient interface {
|
|
Do(ctx context.Context, request HTTPRequest) (HTTPResponse, error)
|
|
Fetch(ctx context.Context, rawURL string, maxBytes int64) (HTTPResponse, error)
|
|
}
|