llmguard

package
v3.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	REQAPIPATH = "/scan/prompt"
	RESAPIPATH = "/scan/output"
)

Variables

View Source
var (
	CONFIGPATH = "llmcontentpath.json"
	//配置各大模型API框架支持的content-type
	ContentTypes = []string{
		"application/json",
		"application/x-ndjson",
		"application/x-www-form-urlencoded",
	}
	MaxIdleConns        = 10000
	MaxIdleConnsPerHost = 10000
	LlmGuardClient      *LlmGuard
)

配置相关变量

View Source
var RequestPaths []string

记录全部LLM框架接口的问题和答案的json路径

View Source
var ResponsePaths []string

Functions

func ContainsContentType

func ContainsContentType(contentType string) bool

func ContentExtractFromJSONDATA

func ContentExtractFromJSONDATA(data string, datatype DATATYPE) (string, bool)

从给定的字符串中提取请求或者应答数据,如果提取不到,返回""和false, 如果提取到内容返回对应的内容和true

func FileExists

func FileExists(path string) bool

func RequestBodyExtract

func RequestBodyExtract(data string) (string, bool)

func ResponseBodyExtract

func ResponseBodyExtract(data string) (string, bool)

输入json结构体字符串,然后从中提取符合支持的大模型回答json结构体里的答案

Types

type Config

type Config struct {
	Address  string    `json:"address"`
	LLMPaths []LLMPath `json:"llmpaths"`
}

type DATATYPE

type DATATYPE int
const (
	REQUESTBODY DATATYPE = iota
	RESPONSEBODY
)

type LLMGuardOutputRequest

type LLMGuardOutputRequest struct {
	Prompt            string    `json:"prompt"`
	Output            string    `json:"output"`
	Scanners_Suppress []Scanner `json:"scanners_suppress"`
}

func (*LLMGuardOutputRequest) AddScannerSuppress

func (lor *LLMGuardOutputRequest) AddScannerSuppress(scanner Scanner)

func (*LLMGuardOutputRequest) RemoveFromScannerList

func (lor *LLMGuardOutputRequest) RemoveFromScannerList(scanner Scanner)

func (*LLMGuardOutputRequest) RemoveScanner added in v3.0.2

func (lor *LLMGuardOutputRequest) RemoveScanner(scanner Scanner)

type LLMGuardPromptRequest

type LLMGuardPromptRequest struct {
	Prompt            string    `json:"prompt"`
	Scanners_Suppress []Scanner `json:"scanners_suppress"`
}

func (*LLMGuardPromptRequest) AddScannerSuppress

func (lpr *LLMGuardPromptRequest) AddScannerSuppress(scanner Scanner)

func (*LLMGuardPromptRequest) RemoveFromScannerList

func (lpr *LLMGuardPromptRequest) RemoveFromScannerList(scanner Scanner)

func (*LLMGuardPromptRequest) RemoveScanner

func (lpr *LLMGuardPromptRequest) RemoveScanner(scanner Scanner)

type LLMGuardRequest

type LLMGuardRequest interface {
	RemoveScanner(Scanner)
	AddScannerSuppress(Scanner)
}

type LLMGuardResponse

type LLMGuardResponse struct {
	IsValid         bool           `json:"is_valid"`
	Scanners        ScannersResult `json:"scanners"`
	SanitizedPrompt string         `json:"sanitized_prompt,omitempty"`
}

func LLMguardScanWithTransport

func LLMguardScanWithTransport(url string, data LLMGuardRequest) (*LLMGuardResponse, error)

func (*LLMGuardResponse) Valid

func (l *LLMGuardResponse) Valid() bool

type LLMPath

type LLMPath struct {
	Request  string `json:"request"`
	Response string `json:"response"`
}

type LlmGuard

type LlmGuard struct {
	// contains filtered or unexported fields
}

定义接口调用客户端池

type Scanner

type Scanner string
var (
	Anonymize          Scanner = "Anonymize"
	BanCode            Scanner = "Bancode"
	BanCompetitors     Scanner = "BanCompetitors"
	BanSubstrings      Scanner = "BanSubstrings"
	BanTopics          Scanner = "BanTopics"
	Code               Scanner = "Code"
	Gibberish          Scanner = "Gibberish"
	InvisibleText      Scanner = "InvisibleText"
	Language           Scanner = "Language"
	PromptInjection    Scanner = "PromptInjection"
	Regex              Scanner = "Regex"
	Secrets            Scanner = "Secrets"
	Sentiment          Scanner = "Sentiment"
	TokenLimit         Scanner = "TokenLimit"
	Toxicity           Scanner = "Toxicity"
	Bias               Scanner = "Bias"
	Deanonymize        Scanner = "Deanonymize"
	FactualConsistency Scanner = "FactualConsistency"
	JSON               Scanner = "JSON"
	LanguageSame       Scanner = "LanguageSame"
	MaliciousURLs      Scanner = "MaliciousURLs"
	NoRefusal          Scanner = "NoRefusal"
	ReadingTime        Scanner = "ReadingTime"
	Relevance          Scanner = "Relevance"
	Sensitive          Scanner = "Sensitive"
)

func RemoveFromScannerList

func RemoveFromScannerList(s Scanner) []Scanner

type ScannersResult

type ScannersResult struct {
	Anonymize          float32 `json:"Anonymize,omitempty"`
	BanCode            float32 `json:"BanCode,omitempty"`
	BanCompetitors     float32 `json:"BanCompetitors,omitempty"`
	BanSubstrings      float32 `json:"BanSubstrings,omitempty"`
	BanTopics          float32 `json:"BanTopics,omitempty"`
	Code               float32 `json:"Code,omitempty"`
	Gibberish          float32 `json:"Gibberish,omitempty"`
	InvisibleText      float32 `json:"InvisibleText,omitempty"`
	Language           float32 `json:"Language,omitempty"`
	PromptInjection    float32 `json:"PromptInjection,omitempty"`
	Regex              float32 `json:"Regex,omitempty"`
	Secrets            float32 `json:"Secrets,omitempty"`
	Sentiment          float32 `json:"Sentiment,omitempty"`
	TokenLimit         float32 `json:"TokenLimit,omitempty"`
	Toxicity           float32 `json:"Toxicity,omitempty"`
	Bias               float32 `json:"Bias,omitempty"`
	Deanonymize        float32 `json:"Deanonymize,omitempty"`
	FactualConsistency float32 `json:"FactualConsistency,omitempty"`
	JSON               float32 `json:"JSON,omitempty"`
	LanguageSame       float32 `json:"LanguageSame,omitempty"`
	MaliciousURLs      float32 `json:"MaliciousURLs,omitempty"`
	NoRefusal          float32 `json:"NoRefusal,omitempty"`
	ReadingTime        float32 `json:"ReadingTime,omitempty"`
	Relevance          float32 `json:"Relevance,omitempty"`
	Sensitive          float32 `json:"Sensitive,omitempty"`
}

func DetectAnswer added in v3.0.2

func DetectAnswer(req string, ans string) (bool, ScannersResult)

TODO 对外提供大模型检测接口,检测回答的内容

func DetectQuestion

func DetectQuestion(reqBody string) (bool, ScannersResult)

对外提供大模型检测调用接口,用于请求体检测

Jump to

Keyboard shortcuts

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