> For the complete documentation index, see [llms.txt](https://docs.unitlab.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.unitlab.ai/api-sdk-cli/ko/sdks/upsert-and-search-vectors.md).

# 벡터 업서트와 검색

Python SDK로 라이브러리 파일과 비디오 프레임의 임베딩 벡터를 업서트하고, 코사인 유사도로 가장 비슷한 파일이나 프레임을 검색하는 방법을 설명합니다.

## 벡터 쓰기 <a href="#write-vectors" id="write-vectors"></a>

```python
space.upsert("ASSET_ID", vector)                         # one file
space.upsert("VIDEO_ASSET_ID", vector, frame_index=120)  # one video frame

report = space.upsert_many(
    {"asset_id": asset_id, "vector": vec} for asset_id, vec in my_vectors
)
print(report["created"], "created,", report["updated"], "updated")
```

이미 벡터가 있는 파일이나 프레임에 벡터를 쓰면 기존 벡터를 대체합니다. `upsert_many()` 호출당 최대 1,000개 항목을 보내고, 더 큰 집합은 나눠서 보내세요.

## 검색 <a href="#search" id="search"></a>

```python
hits = space.search(query_vector, limit=20, project_id=project.id, level="asset")
for hit in hits["results"]:
    print(hit["score"], hit["file_name"], hit["asset_id"], hit["frame_index"])
```

| 인자           | 기본값     | 의미                                          |
| ------------ | ------- | ------------------------------------------- |
| `vector`     | 필수      | 공간과 같은 길이                                   |
| `limit`      | 20      | 결과 1\~100개                                  |
| `project_id` | 없음      | 이 프로젝트의 파일만 검색                              |
| `level`      | `asset` | `asset`은 파일당 결과 1개, `frame`은 비디오 프레임당 결과 1개 |

`score`는 코사인 유사도이며, 1이면 동일합니다.

## 관련 문서 <a href="#related" id="related"></a>

[임베딩 공간](https://docs.unitlab.ai/api-sdk-cli/ko/sdks/embedding-spaces.md) · [패턴: 임베딩 검색](https://docs.unitlab.ai/api-sdk-cli/ko/sdks/pattern-custom-embedding-pipeline.md)
