http

package
v0.0.0-...-b155b17 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 62 Imported by: 0

Documentation

Overview

Package http implements http server.

Index

Constants

View Source
const (
	// OpenaiMessageRoleSystem system message
	OpenaiMessageRoleSystem = "system"
	// OpenaiMessageRoleUser user message
	OpenaiMessageRoleUser = "user"
	// OpenaiMessageRoleAI ai message
	OpenaiMessageRoleAI = "assistant"
)
View Source
const VisionTokenPrice = 5000

VisionTokenPrice vision token price($/500000)

Variables

This section is empty.

Functions

func Call

func Call(name, args string) (string, error)

func Chat

func Chat(ctx *gin.Context)

Chat render chat page

func ChatHandler

func ChatHandler(ctx *gin.Context)

ChatHandler handle api request

func ChatModel

func ChatModel() string

ChatModel return chat model

func ConvertWebPToPNG

func ConvertWebPToPNG(webpData []byte) ([]byte, error)

ConvertWebPToPNG converts a WebP image to PNG format

func CopyHeader

func CopyHeader(to, from http.Header)

CopyHeader copy header from `from` to `to`

func CountVisionImagePrice

func CountVisionImagePrice(width int, height int, resolution VisionImageResolution) (int, error)

CountVisionImagePrice count vision image tokens

https://openai.com/pricing

func DownloadUserConfig

func DownloadUserConfig(ctx *gin.Context)

func DrawByDalleHandler

func DrawByDalleHandler(ctx *gin.Context)

func DrawByFlux

func DrawByFlux(ctx *gin.Context)

DrawByFlux draw image by flux-pro

func DrawByLcmHandler

func DrawByLcmHandler(ctx *gin.Context)

func DrawBySdxlturboHandlerByNvidia

func DrawBySdxlturboHandlerByNvidia(ctx *gin.Context)

func DrawBySdxlturboHandlerBySelfHosted

func DrawBySdxlturboHandlerBySelfHosted(ctx *gin.Context)

func FetchDynamicURLContent

func FetchDynamicURLContent(ctx context.Context,
	url string, opts ...FetchURLOption) (content []byte, err error)

FetchDynamicURLContent fetch dynamic url content, will render js by chromedp

func FetchURLContent

func FetchURLContent(gctx *gin.Context, url string) (content []byte, err error)

FetchURLContent fetch url content

func GetCurrentUser

func GetCurrentUser(ctx *gin.Context)

GetCurrentUser get current user

func GetUserInternalBill

func GetUserInternalBill(ctx context.Context,
	user *config.UserConfig, billType db.BillingType) (
	bill *db.Billing, err error)

GetUserInternalBill get user internal bill

func OneShotChatHandler

func OneShotChatHandler(gctx *gin.Context)

OneShotChatHandler handle one shot chat request

func OneapiProxyHandler

func OneapiProxyHandler(ctx *gin.Context)

OneapiProxyHandler proxy to oneapi url

func OneshotChat

func OneshotChat(ctx context.Context, user *config.UserConfig, model, systemPrompt, userPrompt string) (answer string, err error)

OneshotChat get ai response from gpt-3.5-turbo

Args:

  • systemPrompt: system prompt
  • userPrompt: user prompt

func PaymentHandler

func PaymentHandler(c *gin.Context)

func PaymentStaticHandler

func PaymentStaticHandler(c *gin.Context)

func RamjetProxyHandler

func RamjetProxyHandler(ctx *gin.Context)

RamjetProxyHandler proxy to ramjet url

func RegisterStatic

func RegisterStatic(g gin.IRouter)

RegisterStatic register static files

func SaveLlmConservationHandler

func SaveLlmConservationHandler(ctx *gin.Context)

SaveLlmConservationHandler save llm conservation

func SetupHTTPCli

func SetupHTTPCli() (err error)

SetupHTTPCli setup http client

func TTSHanler

func TTSHanler(ctx *gin.Context)

TTSHanler text to speech by azure, will return audio stream

func Tiktoken

func Tiktoken() *tiktoken.Tiktoken

Tiktoken return tiktoken, could be nil if not found

func UploadFiles

func UploadFiles(ctx *gin.Context)

UploadFiles upload files

func UploadUserConfig

func UploadUserConfig(ctx *gin.Context)

Types

type AzureCreateImageResponse

type AzureCreateImageResponse struct {
	Created int64 `json:"created"`
	Data    []struct {
		RevisedPrompt string `json:"revised_prompt"`
		Url           string `json:"url"`
	} `json:"data"`
}

AzureCreateImageResponse return from azure image api

type DrawImageByFluxProResponse

type DrawImageByFluxProResponse struct {
	CompletedAt time.Time                       `json:"completed_at"`
	CreatedAt   time.Time                       `json:"created_at"`
	DataRemoved bool                            `json:"data_removed"`
	Error       string                          `json:"error"`
	ID          string                          `json:"id"`
	Input       DrawImageByFluxReplicateRequest `json:"input"`
	Logs        interface{}                     `json:"logs"`
	Metrics     FluxMetrics                     `json:"metrics"`
	Output      []string                        `json:"output"`
	StartedAt   time.Time                       `json:"started_at"`
	Status      string                          `json:"status"`
	URLs        FluxURLs                        `json:"urls"`
	Version     string                          `json:"version"`
}

DrawImageByFluxProResponse is response of DrawImageByFluxProRequest

https://replicate.com/black-forest-labs/flux-pro?prediction=kg1krwsdf9rg80ch1sgsrgq7h8&output=json

type DrawImageByFluxReplicateRequest

type DrawImageByFluxReplicateRequest struct {
	Input FluxInput `json:"input"`
}

DrawImageByFluxReplicateRequest draw image by fluxpro

https://replicate.com/black-forest-labs/flux-pro?prediction=kg1krwsdf9rg80ch1sgsrgq7h8&output=json

type DrawImageByFluxSegmind

type DrawImageByFluxSegmind struct {
	// Prompt is the text prompt for generating the image
	Prompt string `json:"prompt" binding:"required"`

	// Steps is the number of inference steps for image generation
	// min: 1, max: 100
	Steps int `json:"steps" binding:"required,min=1,max=100"`

	// Seed is the seed for random number generation
	Seed int `json:"seed"`

	// SamplerName is the sampler for the image generation process
	SamplerName string `json:"sampler_name" binding:"required"`

	// Scheduler is the scheduler for the image generation process
	Scheduler string `json:"scheduler" binding:"required"`

	// Samples is the number of samples to generate
	Samples int `json:"samples" binding:"required"`

	// Width is the image width, can be between 512 and 2048 in multiples of 8
	Width int `json:"width" binding:"required,min=512,max=2048"`

	// Height is the image height, can be between 512 and 2048 in multiples of 8
	Height int `json:"height" binding:"required,min=512,max=2048"`

	// Denoise is the denoise level for the generated image
	Denoise float64 `json:"denoise" binding:"required"`
}

DrawImageByFluxSegmind is request to draw image by flux schnell

https://www.segmind.com/models/flux-schnell/api

type DrawImageByImageRequest

type DrawImageByImageRequest struct {
	Prompt      string `json:"prompt" binding:"required,min=1"`
	Model       string `json:"model" binding:"required,min=1"`
	ImageBase64 string `json:"image_base64" binding:"required,min=1"`
}

DrawImageByImageRequest draw image by image and prompt

type DrawImageByLcmRequest

type DrawImageByLcmRequest struct {
	// Data consist of 6 strings:
	//  1. prompt,
	//  2. base64 encoded image with fixed prefix "data:image/png;base64,"
	//  3. steps
	//  4. cfg
	//  5. sketch strength
	//  6. seed
	Data    [6]any `json:"data"`
	FnIndex int    `json:"fn_index"`
}

DrawImageByLcmRequest draw image by image and prompt with lcm

type DrawImageByLcmResponse

type DrawImageByLcmResponse struct {
	// Data base64 encoded image with fixed prefix "data:image/png;base64,"
	Data            []string `json:"data"`
	IsGenerating    bool     `json:"is_generating"`
	Duration        float64  `json:"duration"`
	AverageDuration float64  `json:"average_duration"`
}

DrawImageByLcmResponse draw image by image and prompt with lcm

type DrawImageBySdxlturboRequest

type DrawImageBySdxlturboRequest struct {
	Model string `json:"model" binding:"required,min=1"`
	// Text prompt
	Text           string `json:"text" binding:"required,min=1"`
	NegativePrompt string `json:"negative_prompt"`
	ImageB64       string `json:"image"`
	// N how many images to generate
	N int `json:"n"`
}

type DrawImageBySdxlturboResponse

type DrawImageBySdxlturboResponse struct {
	B64Images []string `json:"images"`
}

type DrawImageByTextRequest

type DrawImageByTextRequest struct {
	Prompt string `json:"prompt" binding:"required,min=1"`
	Model  string `json:"model" binding:"required,min=1"`
}

DrawImageByTextRequest draw image by text and prompt

type ExternalBillingUserResponse

type ExternalBillingUserResponse struct {
	Data struct {
		Status      ExternalBillingUserStatus `json:"status"`
		RemainQuota db.Price                  `json:"remain_quota"`
	} `json:"data"`
}

ExternalBillingUserResponse return from external billing api

type ExternalBillingUserStatus

type ExternalBillingUserStatus int

ExternalBillingUserStatus user status

const (
	// ExternalBillingUserStatusActive active
	ExternalBillingUserStatusActive ExternalBillingUserStatus = 1
)

type FetchURLOption

type FetchURLOption func(*fetchURLOption) error

func WithDuration

func WithDuration(duration time.Duration) FetchURLOption

type FluxInput

type FluxInput struct {
	Steps           int    `json:"steps" binding:"required,min=1"`
	Prompt          string `json:"prompt" binding:"required,min=5"`
	Guidance        int    `json:"guidance" binding:"required,min=2,max=5"`
	Interval        int    `json:"interval" binding:"required,min=1,max=4"`
	AspectRatio     string `json:"aspect_ratio" binding:"required,oneof=1:1 16:9 2:3 3:2 4:5 5:4 9:16"`
	SafetyTolerance int    `json:"safety_tolerance" binding:"required,min=1,max=5"`
	Seed            int    `json:"seed"`
}

FluxInput is input of DrawImageByFluxProRequest

type FluxMetrics

type FluxMetrics struct {
	ImageCount  int     `json:"image_count"`
	PredictTime float64 `json:"predict_time"`
	TotalTime   float64 `json:"total_time"`
}

FluxMetrics is metrics of DrawImageByFluxProResponse

type FluxURLs

type FluxURLs struct {
	Get    string `json:"get"`
	Cancel string `json:"cancel"`
}

FluxURLs is urls of DrawImageByFluxProResponse

type FrontendReq

type FrontendReq struct {
	Model            string               `json:"model"`
	MaxTokens        uint                 `json:"max_tokens"`
	Messages         []FrontendReqMessage `json:"messages,omitempty"`
	PresencePenalty  float64              `json:"presence_penalty"`
	FrequencyPenalty float64              `json:"frequency_penalty"`
	Stream           bool                 `json:"stream"`
	Temperature      float64              `json:"temperature"`
	TopP             float64              `json:"top_p"`
	N                int                  `json:"n"`

	// LaiskyExtra some special config for laisky
	LaiskyExtra *struct {
		ChatSwitch struct {
			// DisableHttpsCrawler disable https crawler
			DisableHttpsCrawler bool `json:"disable_https_crawler"`
			// EnableGoogleSearch enable google search
			EnableGoogleSearch bool `json:"enable_google_search"`
		} `json:"chat_switch"`
	} `json:"laisky_extra,omitempty"`
}

FrontendReq request from frontend

func (*FrontendReq) PromptTokens

func (r *FrontendReq) PromptTokens() (n int)

PromptTokens count prompt tokens

type FrontendReqMessage

type FrontendReqMessage struct {
	Role    OpenaiMessageRole `json:"role"`
	Content string            `json:"content"`
	// Files send files with message
	Files []frontendReqMessageFiles `json:"files"`
}

FrontendReqMessage request message from frontend

type LLMConservationReq

type LLMConservationReq struct {
	Model     string               `json:"model" binding:"required,min=1"`
	MaxTokens uint                 `json:"max_tokens" binding:"required,min=1"`
	Messages  []FrontendReqMessage `json:"messages" binding:"required,min=1"`
	Response  string               `json:"response" binding:"required,min=1"`
}

type NvidiaArtifact

type NvidiaArtifact struct {
	Base64       string `json:"base64"`
	FinishReason string `json:"finish_reason"`
	Seed         int    `json:"seed"`
}

NvidiaArtifact draw image artifact

type NvidiaDrawImageBySdxlturboRequest

type NvidiaDrawImageBySdxlturboRequest struct {
	TextPrompts []NvidiaTextPrompt `json:"text_prompts"`
	Seed        int                `json:"seed"`
	Sampler     string             `json:"sampler"`
	Steps       int                `json:"steps"`
}

NvidiaDrawImageBySdxlturboRequest draw image by image and prompt with sdxlturbo

https://build.nvidia.com/explore/discover?snippet_tab=Python#sdxl-turbo

func NewNvidiaDrawImageBySdxlturboRequest

func NewNvidiaDrawImageBySdxlturboRequest(prompt string) NvidiaDrawImageBySdxlturboRequest

NewNvidiaDrawImageBySdxlturboRequest create new request

type NvidiaDrawImageBySdxlturboResponse

type NvidiaDrawImageBySdxlturboResponse struct {
	Artifacts []NvidiaArtifact `json:"artifacts"`
}

NvidiaDrawImageBySdxlturboResponse draw image by image and prompt with sdxlturbo

type NvidiaTextPrompt

type NvidiaTextPrompt struct {
	Text string `json:"text"`
}

NvidiaTextPrompt text prompt

type OneShotChatRequest

type OneShotChatRequest struct {
	SystemPrompt string `json:"system_prompt"`
	UserPrompt   string `json:"user_prompt" binding:"required,min=1"`
}

OneShotChatRequest request to one-shot chat api

type OpenaiChatReq

type OpenaiChatReq[T string | []OpenaiVisionMessageContent] struct {
	Model            string                `json:"model"`
	MaxTokens        uint                  `json:"max_tokens"`
	Messages         []OpenaiReqMessage[T] `json:"messages,omitempty"`
	PresencePenalty  float64               `json:"presence_penalty"`
	FrequencyPenalty float64               `json:"frequency_penalty"`
	Stream           bool                  `json:"stream"`
	Temperature      float64               `json:"temperature"`
	TopP             float64               `json:"top_p"`
	N                int                   `json:"n"`
	Tools            []OpenaiChatReqTool   `json:"tools,omitempty"`
}

OpenaiChatReq request to openai chat api

type OpenaiChatReqTool

type OpenaiChatReqTool struct {
	Type       string                      `json:"type"`
	Function   OpenaiChatReqToolFunction   `json:"function"`
	Parameters OpenaiChatReqToolParameters `json:"parameters"`
}

OpenaiChatReqTool define tools

{
	"type": "function",
	"function": {
	  "name": "get_current_weather",
	  "description": "Get the current weather in a given location",
	  "parameters": {
		"type": "object",
		"properties": {
		  "location": {
			"type": "string",
			"description": "The city and state, e.g. San Francisco, CA"
		  },
		  "unit": {
			"type": "string",
			"enum": [
			  "celsius",
			  "fahrenheit"
			]
		  }
		},
		"required": [
		  "location"
		]
	  }
	}
}

func ToolsRequest

func ToolsRequest() []OpenaiChatReqTool

type OpenaiChatReqToolFunction

type OpenaiChatReqToolFunction struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

type OpenaiChatReqToolLocation

type OpenaiChatReqToolLocation struct {
	Type        string `json:"type"`
	Description string `json:"description"`
}

type OpenaiChatReqToolParameters

type OpenaiChatReqToolParameters struct {
	Type       string                      `json:"type"`
	Properties OpenaiChatReqToolProperties `json:"properties"`
	Required   []string                    `json:"required"`
}

type OpenaiChatReqToolProperties

type OpenaiChatReqToolProperties struct {
	Location OpenaiChatReqToolLocation `json:"location"`
	Unit     OpenaiChatReqToolUnit     `json:"unit"`
}

type OpenaiChatReqToolUnit

type OpenaiChatReqToolUnit struct {
	Type string   `json:"type"`
	Enum []string `json:"enum"`
}

type OpenaiCompletionReq

type OpenaiCompletionReq struct {
	Model            string  `json:"model"`
	MaxTokens        uint    `json:"max_tokens"`
	PresencePenalty  float64 `json:"presence_penalty"`
	FrequencyPenalty float64 `json:"frequency_penalty"`
	Stream           bool    `json:"stream"`
	Temperature      float64 `json:"temperature"`
	TopP             float64 `json:"top_p"`
	N                int     `json:"n"`
	Prompt           string  `json:"prompt,omitempty"`
}

OpenaiCompletionReq request to openai chat api

type OpenaiCompletionResp

type OpenaiCompletionResp struct {
	ID     string `json:"id"`
	Object string `json:"object"`
	Model  string `json:"model"`
	Usage  struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage"`
	Choices []struct {
		Message struct {
			Role    string `json:"role"`
			Content string `json:"content"`
		}
		FinishReason string `json:"finish_reason"`
		Index        int    `json:"index"`
	} `json:"choices"`
}

nolint: lll OpenaiCompletionResp return from openai chat api

https://platform.openai.com/docs/guides/chat/response-format

{
	"id": "chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve",
	"object": "chat.completion",
	"created": 1677649420,
	"model": "gpt-3.5-turbo",
	"usage": {"prompt_tokens": 56, "completion_tokens": 31, "total_tokens": 87},
	"choices": [
	  {
	   "message": {
		 "role": "assistant",
		 "content": "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers."},
	   "finish_reason": "stop",
	   "index": 0
	  }
	 ]
   }

type OpenaiCompletionStreamResp

type OpenaiCompletionStreamResp struct {
	ID      string                             `json:"id"`
	Object  string                             `json:"object"`
	Created int64                              `json:"created"`
	Model   string                             `json:"model"`
	Choices []OpenaiCompletionStreamRespChoice `json:"choices"`
}

OpenaiCompletionStreamResp stream chunk return from openai chat api

{
    "id":"chatcmpl-6tCPrEY0j5l157mOIddZi4I0tIFhv",
    "object":"chat.completion.chunk",
    "created":1678613787,
    "model":"gpt-3.5-turbo-0301",
    "choices":[{"delta":{"role":"assistant"}, "index":0, "finish_reason":null}]
}

type OpenaiCompletionStreamRespChoice

type OpenaiCompletionStreamRespChoice struct {
	Delta        OpenaiCompletionStreamRespDelta `json:"delta"`
	Index        int                             `json:"index"`
	FinishReason string                          `json:"finish_reason"`
}

type OpenaiCompletionStreamRespDelta

type OpenaiCompletionStreamRespDelta struct {
	Role      OpenaiMessageRole                    `json:"role"`
	Content   string                               `json:"content"`
	ToolCalls []OpenaiCompletionStreamRespToolCall `json:"tool_calls,omitempty"`
}

type OpenaiCompletionStreamRespToolCall

type OpenaiCompletionStreamRespToolCall struct {
	ID       string `json:"id"`
	Type     string `json:"type"`
	Function struct {
		Name      string `json:"name"`
		Arguments string `json:"arguments"`
	} `json:"function"`
}

OpenaiCompletionStreamRespToolCall tool call

{
	"id": "call_abc123",
	"type": "function",
	"function": {
	  "name": "get_current_weather",
	  "arguments": "{\n\"location\": \"Boston, MA\"\n}"
	}
}

type OpenaiCreateImageRequest

type OpenaiCreateImageRequest struct {
	Model          string `json:"model,omitempty"`
	Prompt         string `json:"prompt"`
	N              int    `json:"n"`
	Size           string `json:"size"`
	Quality        string `json:"quality,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	Style          string `json:"style,omitempty"`
}

OpenaiCreateImageRequest request to openai image api

func NewOpenaiCreateImageRequest

func NewOpenaiCreateImageRequest(prompt string) *OpenaiCreateImageRequest

NewOpenaiCreateImageRequest create new request

type OpenaiCreateImageResponse

type OpenaiCreateImageResponse struct {
	Created int64 `json:"created"`
	Data    []struct {
		Url     string `json:"url"`
		B64Json string `json:"b64_json"`
	} `json:"data"`
}

OpenaiCreateImageResponse return from openai image api

type OpenaiMessageRole

type OpenaiMessageRole string

OpenaiMessageRole message role

func (OpenaiMessageRole) String

func (r OpenaiMessageRole) String() string

String return string

type OpenaiReqMessage

type OpenaiReqMessage[T string | []OpenaiVisionMessageContent] struct {
	Role    OpenaiMessageRole `json:"role"`
	Content T                 `json:"content"`
}

OpenaiReqMessage request message to openai chat api

chat completion message and vision message have different content

type OpenaiVisionMessageContent

type OpenaiVisionMessageContent struct {
	Type     OpenaiVisionMessageContentType      `json:"type"`
	Text     string                              `json:"text,omitempty"`
	ImageUrl *OpenaiVisionMessageContentImageUrl `json:"image_url,omitempty"`
}

OpenaiVisionMessageContent vision message content

type OpenaiVisionMessageContentImageUrl

type OpenaiVisionMessageContentImageUrl struct {
	URL    string                `json:"url"`
	Detail VisionImageResolution `json:"detail,omitempty"`
}

OpenaiVisionMessageContentImageUrl image url

type OpenaiVisionMessageContentType

type OpenaiVisionMessageContentType string

OpenaiVisionMessageContentType vision message content type

const (
	// OpenaiVisionMessageContentTypeText text
	OpenaiVisionMessageContentTypeText OpenaiVisionMessageContentType = "text"
	// OpenaiVisionMessageContentTypeImageUrl image url
	OpenaiVisionMessageContentTypeImageUrl OpenaiVisionMessageContentType = "image_url"
)

type TranscriptRequest

type TranscriptRequest struct {
	File  multipart.File `form:"file" binding:"required"`
	Model string         `form:"model" binding:"required"`
}

TranscriptRequest is the request struct for speech to text

type TranscriptionResponse

type TranscriptionResponse struct {
	Task     string                 `json:"task"`
	Language string                 `json:"language"`
	Duration float64                `json:"duration"`
	Text     string                 `json:"text"`
	Segments []transcriptionSegment `json:"segments"`
	XGroq    xGroq                  `json:"x_groq"`
}

TranscriptionResponse is the request struct for speech to text

func Transcript

func Transcript(ctx context.Context, user *config.UserConfig, req *TranscriptRequest) (respData *TranscriptionResponse, err error)

Transcript transcribe audio to text

type UserQueryType

type UserQueryType string

UserQueryType user query type

const (
	// UserQueryTypeSearch search by embeddings chunks
	UserQueryTypeSearch UserQueryType = "search"
	// UserQueryTypeScan scan by map-reduce
	UserQueryTypeScan UserQueryType = "scan"
)

type VisionImageResolution

type VisionImageResolution string

VisionImageResolution image resolution

const (
	// VisionImageResolutionLow low resolution
	VisionImageResolutionLow VisionImageResolution = "low"
	// VisionImageResolutionHigh high resolution
	VisionImageResolutionHigh VisionImageResolution = "high"
)

Jump to

Keyboard shortcuts

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