임베딩 모델 통합

임베딩 모델 통합

LangChain JavaScript로 임베딩 모델과 통합하는 방법을 안내할게요.

출처: 문서

본문

개요

This overview covers **text-based embedding models**. LangChain does not currently support multimodal embeddings.

임베딩 모델은 문장, 문단, 트윗 같은 원시 텍스트를 그 의미를 담는 고정 길이 숫자 벡터로 변환해요. 이 벡터들은 기계가 정확한 단어가 아니라 의미에 기반해 텍스트를 비교하고 검색할 수 있게 해줘요.

실제로는 비슷한 생각을 가진 텍스트가 벡터 공간에서 서로 가까이 배치돼요. 예를 들어 "machine learning"이라는 구절만 매칭하는 대신, 임베딩은 다른 표현을 사용하더라도 관련 개념을 논의하는 문서를 표면화할 수 있어요.

작동 방식

  1. 벡터화: 모델이 각 입력 문자열을 고차원 벡터로 인코딩해요.
  2. 유사도 점수화: 벡터를 수학적 지표로 비교하여 기반 텍스트가 얼마나 밀접하게 관련됐는지 측정해요.

유사도 지표

임베딩을 비교하는 데 흔히 사용되는 몇 가지 지표가 있어요:

  • 코사인 유사도(Cosine similarity): 두 벡터 사이의 각도를 측정.
  • 유클리드 거리(Euclidean distance): 점들 사이의 직선 거리를 측정.
  • 내적(Dot product): 한 벡터가 다른 벡터에 얼마나 투영되는지 측정.

인터페이스

LangChain은 Embeddings 인터페이스를 통해 텍스트 임베딩 모델(예: OpenAI, Cohere, Hugging Face)을 위한 표준 인터페이스를 제공해요.

두 가지 주요 메서드가 있어요:

  • embedDocuments(documents: string[]) → number[][]: 문서 목록을 임베딩.
  • embedQuery(text: string) → number[]: 단일 쿼리를 임베딩.
The interface allows queries and documents to be embedded with different strategies, though most providers handle them the same way in practice.

설치 및 사용

Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/openai @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/openai @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/openai @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
OPENAI_API_KEY=your-api-key
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-large"
});
```
Install dependencies
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/openai @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/openai @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/openai @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AZURE_OPENAI_API_INSTANCE_NAME=<YOUR_INSTANCE_NAME>
AZURE_OPENAI_API_KEY=<YOUR_KEY>
AZURE_OPENAI_API_VERSION="2024-02-01"
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { AzureOpenAIEmbeddings } from "@langchain/openai";

const embeddings = new AzureOpenAIEmbeddings({
  azureOpenAIApiEmbeddingsDeploymentName: "text-embedding-ada-002"
});
```
Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/aws @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/aws @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/aws @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
BEDROCK_AWS_REGION=your-region
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { BedrockEmbeddings } from "@langchain/aws";

const embeddings = new BedrockEmbeddings({
  model: "amazon.titan-embed-text-v1"
});
```
Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/google-genai @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/google-genai @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/google-genai @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
GOOGLE_API_KEY=your-api-key
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { GoogleGenerativeAIEmbeddings } from "@langchain/google-genai";

const embeddings = new GoogleGenerativeAIEmbeddings({
  model: "text-embedding-004"
});
```
Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/google-vertexai @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/google-vertexai @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/google-vertexai @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
GOOGLE_APPLICATION_CREDENTIALS=credentials.json
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { VertexAIEmbeddings } from "@langchain/google-vertexai";

const embeddings = new VertexAIEmbeddings({
  model: "gemini-embedding-001"
});
```
Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/mistralai @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/mistralai @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/mistralai @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
MISTRAL_API_KEY=your-api-key
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { MistralAIEmbeddings } from "@langchain/mistralai";

const embeddings = new MistralAIEmbeddings({
  model: "mistral-embed"
});
```
Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/cohere @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/cohere @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/cohere @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
COHERE_API_KEY=your-api-key
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { CohereEmbeddings } from "@langchain/cohere";

const embeddings = new CohereEmbeddings({
  model: "embed-english-v3.0"
});
```
Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/ollama @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/ollama @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/ollama @langchain/core
  ```
</CodeGroup>

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { OllamaEmbeddings } from "@langchain/ollama";

const embeddings = new OllamaEmbeddings({
  model: "llama2",
  baseUrl: "http://localhost:11434", // Default value
});
```
Install dependencies:
<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/mongodb @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/mongodb @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/mongodb @langchain/core
  ```
</CodeGroup>

Add environment variables:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
VOYAGE_API_KEY=your-api-key
```

Instantiate the model:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { VoyageEmbeddings } from "@langchain/mongodb";

const embeddings = new VoyageEmbeddings({
  model: "voyage-4"
});
```

캐싱

임베딩은 다시 계산하지 않도록 저장하거나 임시 캐시할 수 있어요.

임베딩 캐싱은 CacheBackedEmbeddings로 수행할 수 있어요. 이 래퍼는 키-값 저장소에 임베딩을 저장하며, 텍스트가 해시되어 그 해시가 캐시의 키로 사용돼요.

CacheBackedEmbeddings를 초기화하는 주요 지원 방식은 fromBytesStore예요. 다음 매개변수를 받아요:

  • underlyingEmbeddings: 임베딩에 사용할 임베더(embedder).
  • documentEmbeddingStore: 문서 임베딩을 캐시하기 위한 BaseStore.
  • options.namespace: (선택, 기본 "") 문서 캐시에 사용할 네임스페이스. 충돌 방지에 도움을 줘요 (예: 임베딩 모델 이름으로 설정).
- Always set the `namespace` parameter to avoid collisions when using different embedding models. - `CacheBackedEmbeddings` does not cache query embeddings by default. To enable this, specify a `query_embedding_store`.
import { CacheBackedEmbeddings } from "@langchain/classic/embeddings/cache_backed";
import { InMemoryStore } from "@langchain/core/stores";

const underlyingEmbeddings = new OpenAIEmbeddings();

const inMemoryStore = new InMemoryStore();

const cacheBackedEmbeddings = CacheBackedEmbeddings.fromBytesStore(
  underlyingEmbeddings,
  inMemoryStore,
  {
    namespace: underlyingEmbeddings.model,
  }
);

// Example: caching a query embedding
const tic = Date.now();
const queryEmbedding = cacheBackedEmbeddings.embedQuery("Hello, world!");
console.log(`First call took: ${Date.now() - tic}ms`);

// Example: caching a document embedding
const tic = Date.now();
const documentEmbedding = cacheBackedEmbeddings.embedDocuments(["Hello, world!"]);
console.log(`Cached creation time: ${Date.now() - tic}ms`);

프로덕션에서는 일반적으로 데이터베이스나 클라우드 스토리지 같은 더 견고한 영구 저장소를 사용할 거예요. 옵션은 stores 통합을 참고하세요.

모든 통합

| Integration | Downloads | | :--- | :--- | | [`AzureOpenAIEmbeddings`](/oss/javascript/integrations/embeddings/azure_openai) | Downloads per month | | [`OpenAIEmbeddings`](/oss/javascript/integrations/embeddings/openai) | Downloads per month | | [`Bedrock`](/oss/javascript/integrations/embeddings/bedrock) | Downloads per month | | [`GoogleGenerativeAIEmbeddings`](/oss/javascript/integrations/embeddings/google_generative_ai) | Downloads per month | | [`VertexAIEmbeddings`](/oss/javascript/integrations/embeddings/google_vertex_ai) | Downloads per month | | [`OllamaEmbeddings`](/oss/javascript/integrations/embeddings/ollama) | Downloads per month | | [`MistralAIEmbeddings`](/oss/javascript/integrations/embeddings/mistralai) | Downloads per month | | [`PineconeEmbeddings`](/oss/javascript/integrations/embeddings/pinecone) | Downloads per month | | [`VoyageEmbeddings`](/oss/javascript/integrations/embeddings/voyageai) | Downloads per month | | [`CohereEmbeddings`](/oss/javascript/integrations/embeddings/cohere) | Downloads per month | | [`Baidu qianfan`](/oss/javascript/integrations/embeddings/baidu_qianfan) | Downloads per month | | [`Nomic`](/oss/javascript/integrations/embeddings/nomic) | Downloads per month | | [`CloudflareWorkersAIEmbeddings`](/oss/javascript/integrations/embeddings/cloudflare_ai) | Downloads per month | | [`FireworksEmbeddings`](/oss/javascript/integrations/embeddings/fireworks) | Downloads per month | | [`WatsonxEmbeddings`](/oss/javascript/integrations/embeddings/ibm) | Downloads per month | | [`TogetherAIEmbeddings`](/oss/javascript/integrations/embeddings/togetherai) | Downloads per month | | [`Mixedbread AI`](/oss/javascript/integrations/embeddings/mixedbread_ai) | Downloads per month | | [`SCXEmbeddings`](https://scx.ai/) | Downloads per month | | [`Minimax`](/oss/javascript/integrations/embeddings/minimax) | N/A | | [`OracleEmbeddings`](/oss/javascript/integrations/embeddings/oracleai) | N/A |

[Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers. [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/embeddings/index.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).

더 알아보기