fakeembed

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package fakeembed 是测试用的「假嵌入服务」:一个本机的 OpenAI 兼容 /embeddings 端点, 用确定性的「概念词典 + 字符 n-gram 哈希」造向量,使近义词在向量上相近、而词法完全不命中—— 这样才能验证「语义检索」真的比词法多找到了东西。它只被测试引用,不会进入任何生产代码路径。

Index

Constants

This section is empty.

Variables

View Source
var DefaultConcepts = [][]string{
	{"汽车", "轿车", "机动车", "车辆", "car", "automobile", "vehicle"},
	{"保养", "维修", "养护", "检修", "maintenance", "repair"},
	{"数据库", "存储", "库表", "database", "storage"},
	{"备份", "转储", "归档", "backup", "dump"},
	{"登录", "鉴权", "认证", "login", "authentication"},
	{"令牌", "凭证", "口令", "token", "credential"},
	{"发布", "上线", "部署", "release", "deploy"},
	{"回滚", "撤销", "还原", "rollback", "revert"},
	{"会议", "评审", "讨论", "meeting", "review"},
	{"预算", "费用", "成本", "budget", "cost"},
	{"猫", "小猫", "猫咪", "cat", "kitten"},
	{"狗", "小狗", "犬", "dog", "puppy"},
}

DefaultConcepts 是内置的概念词典:同一组里的词在向量上几乎一样(互为近义词)。

Functions

This section is empty.

Types

type Options

type Options struct {
	Concepts [][]string // 空 = DefaultConcepts
}

Options 创建假服务的参数。

type Server

type Server struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Server 是一个假嵌入服务。

func New

func New(o Options) *Server

New 启动一个假服务(调用方负责 Close)。基础地址是 URL + "/v1"。

func (*Server) Auths

func (s *Server) Auths() []string

Auths 返回每个请求的 Authorization 头(测试检查「密钥没有出现在别处」时用)。

func (*Server) BaseURL

func (s *Server) BaseURL() string

BaseURL 是填进配置的服务地址(到 /v1 为止)。

func (*Server) Batches

func (s *Server) Batches() [][]string

Batches 返回每个请求带来的文本列表。

func (*Server) Embed

func (s *Server) Embed(text string, dims int) []float32

Embed 是确定性的假嵌入:概念命中占据前 nConcept 维,字符 1-gram / 2-gram 带符号哈希占据其余维(权重很小)。 要求 dims > 概念数 + 8。

func (*Server) FailWith

func (s *Server) FailWith(status, n int)

FailWith 让接下来 n 个请求都用 status 应答(n < 0 = 一直失败;status 为 0 = 恢复正常)。

func (*Server) Inputs

func (s *Server) Inputs() []string

Inputs 按收到的顺序返回所有送来嵌入的文本(每条一项)。

func (*Server) OverrideDimensions

func (s *Server) OverrideDimensions(d int)

OverrideDimensions 让服务返回指定维度(模拟提供商悄悄换了维度);0 = 按请求。

func (*Server) RejectContaining

func (s *Server) RejectContaining(sub string)

RejectContaining 让「任何输入含 sub」的请求都以 400 应答(模拟内容安全拦截);空串 = 取消。

func (*Server) Requests

func (s *Server) Requests() int

Requests 返回收到的请求总数。

func (*Server) Reset

func (s *Server) Reset()

Reset 清空记录。

func (*Server) SetDelay

func (s *Server) SetDelay(d time.Duration)

SetDelay 让每个请求先睡一会儿再应答。

Jump to

Keyboard shortcuts

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