임베딩 생성

임베딩 생성 (Generate Embeddings)

임베딩 API는 입력 문자열을 숫자 벡터로 바꿔줘요. 두 벡터를 비교하면 두 원문이 얼마나 관련 있는지 측정할 수 있죠. 검색, 분류, 추천, 그리고 검색 증강 생성(RAG)까지 폭넓게 쓰여요. 장기 검색이 목적이라면 임베딩을 벡터 데이터베이스에 저장하고 유사도로 질의하면 돼요.

출처: 공식문서 - Generate embeddings

임베딩 하나 생성하기

client.embeddings.create에 모델과 입력 문자열을 넘기면 돼요.

from together import Together

client = Together()

response = client.embeddings.create(
    model="intfloat/multilingual-e5-large-instruct",
    input="Our solar system orbits the Milky Way galaxy at about 515,000 mph",
)
curl -X POST https://api.together.ai/v1/embeddings \
     -H "Authorization: Bearer ***" \
     -H "Content-Type: application/json" \
     -d '{
         "input": "Our solar system orbits the Milky Way galaxy at about 515,000 mph.",
         "model": "intfloat/multilingual-e5-large-instruct"
        }'

응답에는 data 아래에 임베딩과 함께 메타데이터가 담겨요.

{
  "model": "intfloat/multilingual-e5-large-instruct",
  "object": "list",
  "data": [
    {
      "index": 0,
      "object": "embedding",
      "embedding": [0.2633975, 0.13856208, 0.04331574]
    }
  ]
}

여러 임베딩 생성하기

input에 문자열 배열을 넘기면 한 번의 호출로 여러 텍스트를 임베딩할 수 있어요.

from together import Together

client = Together()

response = client.embeddings.create(
    model="intfloat/multilingual-e5-large-instruct",
    input=[
        "Our solar system orbits the Milky Way galaxy at about 515,000 mph",
        "Jupiter's Great Red Spot is a storm that has been raging for at least 350 years.",
    ],
)
curl -X POST https://api.together.ai/v1/embeddings \
     -H "Authorization: Bearer ***" \
     -H "Content-Type: application/json" \
     -d '{
         "model": "intfloat/multilingual-e5-large-instruct",
         "input": [
            "Our solar system orbits the Milky Way galaxy at about 515,000 mph",
            "Jupiter'\''s Great Red Spot is a storm that has been raging for at least 350 years."
         ]
        }'

response.data에는 입력당 객체 하나가 들어 있고, 각각이 일치하는 index를 가져요.

{
  "model": "intfloat/multilingual-e5-large-instruct",
  "object": "list",
  "data": [
    {
      "index": 0,
      "object": "embedding",
      "embedding": [0.2633975, 0.13856208, 0.04331574]
    },
    {
      "index": 1,
      "object": "embedding",
      "embedding": [-0.14496337, 0.21044481, -0.16187587]
    }
  ]
}

다음 단계

더 알아보기 (Learn more)