OpenAI API Embeddings

2023-03-21 11:45 更新

獲取給定輸入的矢量表示,機(jī)器學(xué)習(xí)模型和算法可以輕松使用該表示。


Create embeddings

POST https://api.openai.com/v1/embeddings

創(chuàng)建表示輸入文本的嵌入向量。

Request body

字段 類(lèi)型 是否可選 說(shuō)明
model string 必須 要使用的模型的 ID。您可以使用 List models API 來(lái)查看所有可用模型。
input string or array 必須 輸入文本以獲取嵌入,編碼為字符串或標(biāo)記數(shù)組。要在單個(gè)請(qǐng)求中獲取多個(gè)輸入的嵌入,請(qǐng)傳遞一個(gè)字符串?dāng)?shù)組或令牌數(shù)組數(shù)組。每個(gè)輸入的長(zhǎng)度不得超過(guò) 8192 個(gè)標(biāo)記。
user string 可選 代表您的最終用戶(hù)的唯一標(biāo)識(shí)符,可以幫助 OpenAI 監(jiān)控和檢測(cè)濫用行為。

示例請(qǐng)求

 curl python  node.js 
curl https://api.openai.com/v1/embeddings \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-ada-002"
  }'
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Embedding.create(
  model="text-embedding-ada-002",
  input="The food was delicious and the waiter..."
)
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createEmbedding({
  model: "text-embedding-ada-002",
  input: "The food was delicious and the waiter...",
});

參數(shù)

{
  "model": "text-embedding-ada-002",
  "input": "The food was delicious and the waiter..."
}

響應(yīng)

{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [
        0.0023064255,
        -0.009327292,
        .... (1536 floats total for ada-002)
        -0.0028842222,
      ],
      "index": 0
    }
  ],
  "model": "text-embedding-ada-002",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)