one-api 接口 /v1/embeddings 仅支持string,不支持数组

q3qa4bjr  于 2个月前  发布在  其他
关注(0)|答案(6)|浏览(87)
let chunks: FileItemChunk[] = []

switch (fileExtension) {
  case "csv":
    chunks = await processCSV(blob)
    break
  case "json":
    chunks = await processJSON(blob)
    break
  case "md":
    chunks = await processMarkdown(blob)
    break
  case "pdf":
    chunks = await processPdf(blob)
    break
  case "txt":
    chunks = await processTxt(blob)
    break
  default:
    return new NextResponse("Unsupported file type", {
      status: 400
    })
}

let embeddings: any = []

const openai = new OpenAI({
  apiKey: profile.openai_api_key || "",
  organization: profile.openai_organization_id,
  baseURL: "https://one-api.com/v1"
})

const response = await openai.embeddings.create({
  model: "text-embedding-3-small",
  input: chunks.map(chunk => chunk.content)
})

其中input: chunks.map(chunk => chunk.content) ,chunk 是文件处理后字符串数组。
baseURL 为官方接口 https://api.openai.com/v1 时文件能成功上传并响应
baseURL 为one-api接口 https://one-api.com/v1 时,文件上传返回以下错误

Failed to upload. JSON object requested, multiple (or no) rows returned

似乎接口只支持字符串,不支持数组。
https://platform.openai.com/docs/api-reference/embeddings/create

fnatzsnv

fnatzsnv1#

从提供的错误日志来看,这是一个访问智谱AI向量库时发生的报错。具体来说,错误发生在zhipu.ConvertEmbeddingRequest函数中,将请求数据转换为嵌入向量时发生了类型转换错误。

错误信息如下:


[ERR] 2024/04/09 - 12:06:33 | 2024040912063389521557768025579 | panic detected: interface conversion: interface {} is []interface {}, not string

这个错误表明在尝试将一个空接口(interface{})转换为字符串时发生了问题。实际上,该空接口是一个包含多个接口的切片([]interface{})。为了解决这个问题,您需要检查zhipu.ConvertEmbeddingRequest函数中的相关代码,并确保在进行类型转换之前正确处理了可能的空值或空切片。

kmynzznz

kmynzznz3#

这个问题解决了嘛?我好像同样遇到这个问题 labring/FastGPT#1198 (comment)

lxkprmvk

lxkprmvk4#

我也遇到同样的问题。

ht4b089n

ht4b089n5#

@songquanpeng 应该不是 OneAPI 的问题。和渠道有关,比如我用 ChatAnywhere 是正常的,但智谱只能字符串,不能字符列表:
参考智谱文档: https://open.bigmodel.cn/dev/api#text_embedding

up9lanfz

up9lanfz6#

@songquanpeng 应该不是 OneAPI 的问题。和渠道有关,比如我用 ChatAnywhere 是正常的,但智谱只能字符串,不能字符列表:
Ref 智谱文档: https://open.bigmodel.cn/dev/api#text_embedding

输入的日志如下:

[DEBUG] 2024/04/24 - 14:28:53 | 2024042414285385214797631405077 | request body: {"input": [[2028, 374, 264, 1296, 4733]], "model": "text-embedding-3-large", "encoding_format": "base64"}

解析代码

func (r GeneralOpenAIRequest) ParseInput() []string {
	if r.Input == nil {
		return nil
	}
	var input []string
	switch r.Input.(type) {
	case string:
		input = []string{r.Input.(string)}
	case []any:
		input = make([]string, 0, len(r.Input.([]any)))
		for _, item := range r.Input.([]any) {
			if str, ok := item.(string); ok {
				input = append(input, str)
			}
		}
	}
	return input

相关问题