MistralAIEmbeddings 통합

MistralAIEmbeddings 통합

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

출처: 문서

본문

이 문서는 LangChain으로 MistralAIEmbeddings 임베딩 모델을 시작하는 데 도움을 줘요. MistralAIEmbeddings 기능과 구성 옵션에 대한 자세한 문서는 API 레퍼런스를 참고하세요.

개요

통합 세부 정보

클래스 패키지 Local Py 지원 Downloads Version
MistralAIEmbeddings @langchain/mistralai NPM - Downloads NPM - Version

설정

MistralAI 임베딩 모델에 접근하려면 MistralAI 계정을 만들고 API 키를 받은 뒤 @langchain/mistralai 통합 패키지를 설치해야 해요.

자격 증명

console.mistral.ai에서 MistralAI에 가입하고 API 키를 생성하세요. 완료되면 MISTRAL_API_KEY 환경 변수를 설정하세요:

export MISTRAL_API_KEY="your-api-key"

모델 호출의 자동 추적(tracing)을 원한다면 아래 주석을 해제해 LangSmith API 키를 설정할 수도 있어요:

# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

설치

LangChain MistralAIEmbeddings 통합은 @langchain/mistralai 패키지에 있어요:

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

인스턴스 생성

이제 모델 객체를 생성하고 채팅 완성을 생성할 수 있어요:

import { MistralAIEmbeddings } from "@langchain/mistralai";

const embeddings = new MistralAIEmbeddings({
  model: "mistral-embed", // Default value
});

인덱싱 및 검색

임베딩 모델은 데이터 인덱싱과 나중 검색 모두의 일부로, 검색 증강 생성(RAG) 흐름에서 자주 사용돼요. 더 자세한 지침은 Learn의 RAG 튜토리얼을 참고하세요.

아래에서 위에서 초기화한 embeddings 객체로 데이터를 인덱싱하고 검색하는 방법을 확인하세요. 이 예제에서는 데모 MemoryVectorStore를 사용해 샘플 문서를 인덱싱하고 검색할 거예요.

// Create a vector store with a sample text
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";

const text = "LangChain is the framework for building context-aware reasoning applications";

const vectorstore = await MemoryVectorStore.fromDocuments(
  [{ pageContent: text, metadata: {} }],
  embeddings,
);

// Use the vector store as a retriever that returns a single document
const retriever = vectorstore.asRetriever(1);

// Retrieve the most similar text
const retrievedDocuments = await retriever.invoke("What is LangChain?");

retrievedDocuments[0].pageContent;
LangChain is the framework for building context-aware reasoning applications

직접 사용

내부적으로 vectorstore와 retriever 구현은 fromDocuments에 사용된 텍스트와 retriever의 invoke 연산에 대해 각각 embeddings.embedDocument(...)embeddings.embedQuery(...)를 호출해 임베딩을 만들어요.

이 메서드들을 직접 호출해 자신의 사용 사례에 대한 임베딩을 얻을 수 있어요.

단일 텍스트 임베딩

embedQuery로 검색을 위한 쿼리를 임베딩할 수 있어요. 이는 쿼리에 특화된 벡터 표현을 생성해요:

const singleVector = await embeddings.embedQuery(text);

console.log(singleVector.slice(0, 100));
[
          -0.04443359375,         0.01885986328125,      0.018035888671875,
    -0.00864410400390625,        0.049652099609375,     -0.001190185546875,
       0.028900146484375,       -0.035675048828125,   -0.00702667236328125,
  0.00016105175018310547,          -0.027587890625,      0.029388427734375,
      -0.053253173828125,   -0.0003020763397216797,     -0.046112060546875,
      0.0258026123046875,   -0.0010776519775390625,       0.02703857421875,
       0.040985107421875,       -0.004547119140625,     -0.020172119140625,
       -0.02606201171875,     -0.01457977294921875,          0.01220703125,
     -0.0078582763671875,         -0.0084228515625,      -0.02056884765625,
         -0.071044921875,         -0.0404052734375,    0.00923919677734375,
     0.01407623291015625,      -0.0210113525390625,  0.0006284713745117188,
    -0.01465606689453125,       0.0186309814453125,     -0.015838623046875,
   0.0007920265197753906,        -0.04437255859375,      0.008758544921875,
        -0.0172119140625,         0.01312255859375,   -0.01358795166015625,
     -0.0212860107421875, -0.000035822391510009766,    -0.0226898193359375,
    -0.01390838623046875,       -0.007659912109375,     -0.016021728515625,
       0.025909423828125,       -0.034515380859375,       -0.0372314453125,
       0.020355224609375,        -0.02606201171875,    -0.0158843994140625,
      -0.037994384765625,      0.00450897216796875,        0.0142822265625,
      -0.012725830078125,         -0.0770263671875,       0.02630615234375,
      -0.048614501953125,        0.006072998046875,    0.00417327880859375,
   -0.005138397216796875,         0.02557373046875,        0.0311279296875,
       0.026519775390625,      -0.0103607177734375,    -0.0108489990234375,
      -0.029510498046875,        0.022186279296875,     0.0256500244140625,
     -0.0186309814453125,          0.0443115234375,    -0.0304107666015625,
       -0.03131103515625,     0.007427215576171875,     0.0234527587890625,
      0.0224761962890625,      0.00463104248046875, -0.0037021636962890625,
      0.0302581787109375,          0.0733642578125,    -0.0121612548828125,
     -0.0172576904296875,        0.019317626953125,         0.029052734375,
     -0.0024871826171875,       0.0174713134765625,      0.026092529296875,
        0.04425048828125,   -0.0004563331604003906,     0.0146026611328125,
    -0.00748443603515625,         0.06146240234375,          0.02294921875,
         -0.016845703125,   -0.0014057159423828125,   -0.01435089111328125,
        0.06097412109375
]

여러 텍스트 임베딩

embedDocuments로 인덱싱을 위한 여러 텍스트를 임베딩할 수 있어요. 이 메서드에 사용되는 내부는 쿼리 임베딩과 다를 수 있지만(다를 필요는 없음) 있어요:

const text2 = "LangGraph is a library for building stateful, multi-actor applications with LLMs";

const vectors = await embeddings.embedDocuments([text, text2]);

console.log(vectors[0].slice(0, 100));
console.log(vectors[1].slice(0, 100));
[
          -0.04443359375,         0.01885986328125,    0.0180511474609375,
     -0.0086517333984375,        0.049652099609375,  -0.00121307373046875,
      0.0289154052734375,        -0.03570556640625, -0.007015228271484375,
   0.0001499652862548828,      -0.0276641845703125,    0.0294036865234375,
          -0.05322265625,   -0.0002808570861816406,     -0.04608154296875,
        0.02581787109375,   -0.0011072158813476562,        0.027099609375,
       0.040985107421875,       -0.004547119140625,   -0.0201873779296875,
     -0.0260772705078125,      -0.0146026611328125,    0.0121917724609375,
      -0.007843017578125,      -0.0084381103515625,   -0.0205535888671875,
       -0.07110595703125,        -0.04046630859375,   0.00931549072265625,
        0.01409912109375,           -0.02099609375, 0.0006232261657714844,
      -0.014678955078125,       0.0186614990234375,   -0.0158233642578125,
    0.000812530517578125,        -0.04437255859375,   0.00873565673828125,
        -0.0172119140625,        0.013092041015625,      -0.0135498046875,
     -0.0212860107421875, -0.000006735324859619141,   -0.0226898193359375,
    -0.01389312744140625,      -0.0076751708984375,   -0.0160064697265625,
      0.0259246826171875,         -0.0345458984375,    -0.037200927734375,
       0.020355224609375,         -0.0260009765625,   -0.0159149169921875,
       -0.03802490234375,     0.004489898681640625,    0.0143280029296875,
    -0.01274871826171875,        -0.07708740234375,    0.0263214111328125,
       -0.04864501953125,      0.00608062744140625,  0.004192352294921875,
   -0.005115509033203125,       0.0255889892578125,       0.0311279296875,
      0.0265045166015625,      -0.0103607177734375,  -0.01084136962890625,
     -0.0294952392578125,        0.022186279296875,    0.0256500244140625,
        -0.0186767578125,        0.044342041015625,    -0.030426025390625,
       -0.03131103515625,     0.007396697998046875,    0.0234527587890625,
            0.0224609375,     0.004634857177734375, -0.003643035888671875,
      0.0302886962890625,         0.07342529296875,  -0.01221466064453125,
      -0.017303466796875,       0.0193023681640625,        0.029052734375,
  -0.0024890899658203125,       0.0174407958984375,        0.026123046875,
       0.044219970703125,   -0.0004944801330566406,   0.01462554931640625,
   -0.007450103759765625,         0.06146240234375,     0.022979736328125,
         -0.016845703125,    -0.001445770263671875,   -0.0143890380859375,
        0.06097412109375
]
[
       -0.02032470703125,       0.02606201171875,      0.051605224609375,
        -0.0281982421875,      0.055755615234375,   0.001987457275390625,
          0.031982421875,    -0.0131378173828125,       -0.0252685546875,
    0.001010894775390625,     -0.024017333984375,      0.053375244140625,
      -0.042816162109375,      0.005584716796875,      -0.04132080078125,
        0.03021240234375,       0.01324462890625,      0.016876220703125,
       0.041961669921875,  -0.004299163818359375,    -0.0273895263671875,
      -0.039642333984375,     -0.021575927734375,     0.0309295654296875,
     -0.0099945068359375,    -0.0163726806640625,   -0.00968170166015625,
       -0.07733154296875,     -0.030364990234375,  -0.003864288330078125,
       0.016387939453125,       -0.0389404296875,    -0.0026702880859375,
     -0.0176544189453125,     0.0264434814453125,      -0.01226806640625,
  -0.0022220611572265625,     -0.039703369140625,   -0.00907135009765625,
     -0.0260467529296875,       0.03155517578125, -0.0004324913024902344,
      -0.019500732421875,    -0.0120697021484375,        -0.008544921875,
       -0.01654052734375,       0.00067138671875,    -0.0134735107421875,
        0.01080322265625,     -0.034759521484375,         -0.06201171875,
       0.012359619140625,  -0.006237030029296875,    -0.0168914794921875,
     -0.0183563232421875,     0.0236053466796875, -0.0021419525146484375,
     -0.0164947509765625,     -0.052581787109375,      0.022125244140625,
      -0.045745849609375, -0.0009088516235351562,     0.0097808837890625,
  -0.0009326934814453125,      0.041656494140625,        0.0269775390625,
          0.016845703125, -0.0022335052490234375,    -0.0182342529296875,
     -0.0245208740234375,  0.0036602020263671875,    -0.0188751220703125,
     -0.0023956298828125,     0.0238800048828125,     -0.034942626953125,
      -0.033782958984375,     0.0046234130859375,        0.0318603515625,
      0.0251007080078125, -0.0023288726806640625,    -0.0225677490234375,
   0.0004394054412841797,         0.064208984375,    -0.0254669189453125,
     -0.0234222412109375,  0.0009264945983886719,    0.01464080810546875,
    0.006626129150390625,  -0.007450103759765625,       0.02642822265625,
         0.0260009765625,    0.00536346435546875,    0.01479339599609375,
  -0.0032253265380859375,           0.0498046875,      0.048248291015625,
    -0.01519012451171875,    0.00605010986328125,      0.019744873046875,
      0.0296478271484375
]

훅 (Hooks)

Mistral AI는 세 가지 이벤트에 대한 커스텀 훅을 지원해요: beforeRequest, requestError, response. 각 훅 유형의 함수 시그니처 예시는 아래에서 확인할 수 있어요:

const beforeRequestHook = (req: Request): Request | void | Promise<Request | void> => {
    // Code to run before a request is processed by Mistral
};

const requestErrorHook = (err: unknown, req: Request): void | Promise<void> => {
    // Code to run when an error occurs as Mistral is processing a request
};

const responseHook = (res: Response, req: Request): void | Promise<void> => {
    // Code to run before Mistral sends a successful response
};

이 훅들을 임베딩 모델에 추가하려면 인자로 전달하면 자동으로 추가돼요:

import { MistralAIEmbeddings } from "@langchain/mistralai"

const modelWithHooks = new MistralAIEmbeddings({
    model: "mistral-embed",
    beforeRequestHooks: [ beforeRequestHook ],
    requestErrorHooks: [ requestErrorHook ],
    responseHooks: [ responseHook ],
    // other params...
});

또는 인스턴스 생성 후 수동으로 할당하고 추가할 수도 있어요:

import { MistralAIEmbeddings } from "@langchain/mistralai"

const model = new MistralAIEmbeddings({
    model: "mistral-embed",
    // other params...
});

model.beforeRequestHooks = [ ...model.beforeRequestHooks, beforeRequestHook ];
model.requestErrorHooks = [ ...model.requestErrorHooks, requestErrorHook ];
model.responseHooks = [ ...model.responseHooks, responseHook ];

model.addAllHooksToHttpClient();

addAllHooksToHttpClient 메서드는 훅 중복을 피하기 위해 업데이트된 전체 훅 목록을 할당하기 전에 현재 추가된 모든 훅을 지워요.

훅은 하나씩 제거하거나 한 번에 모두 지울 수 있어요.

model.removeHookFromHttpClient(beforeRequestHook);

model.removeAllHooksFromHttpClient();

API 레퍼런스

모든 MistralAIEmbeddings 기능과 구성에 대한 자세한 문서는 API 레퍼런스를 참고하세요.


[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/mistralai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).

더 알아보기