MistralAI 통합

MistralAI 통합

LangChain JavaScript로 MistralAI LLM과 통합하는 방법을 알아봅니다.

Mistral AI는 강력한 오픈소스 모델 호스팅을 제공하는 플랫폼이에요. LangChain으로 MistralAI 완성 모델(LLM)을 시작하는 데 도움을 주는 가이드입니다. MistralAI 기능과 구성 옵션에 대한 자세한 문서는 API reference를 참조하세요.

출처: 문서

본문

현재 이 페이지는 Mistral 모델을 텍스트 완성 모델로 사용하는 방법을 다루는 문서예요. Mistral에서 제공하는 많은 인기 모델은 챗 완성 모델입니다.

아마 이 페이지를 찾고 계실 수도 있어요.

Mistral의 모델을 로컬에서 실행하고 싶으신가요? Ollama 통합을 확인해 보세요.

개요

통합 세부 정보

클래스(Class) 패키지(Package) 로컬(Local) 직렬화(Serializable) PY 지원 다운로드(Downloads) 버전(Version)
MistralAI @langchain/mistralai NPM - Downloads NPM - Version

설정

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

자격 증명(Credentials)

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

export MISTRAL_API_KEY="your-api-key"

모델 호출에 대한 자동 추적을 받으려면 아래 주석을 해제해 LangSmith API 키를 설정할 수도 있습니다:

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

설치

LangChain MistralAI 통합은 @langchain/mistralai 패키지에 있습니다:

npm install @langchain/mistralai @langchain/core
yarn add @langchain/mistralai @langchain/core
pnpm add @langchain/mistralai @langchain/core

인스턴스화(Instantiation)

이제 모델을 인스턴스화하고 텍스트 완성을 생성할 수 있습니다:

import { MistralAI } from "@langchain/mistralai"

const llm = new MistralAI({
  model: "codestral-latest",
  temperature: 0,
  maxTokens: undefined,
  maxRetries: 2,
  // other params...
})

호출(Invocation)

const inputText = "MistralAI is an AI company that "

const completion = await llm.invoke(inputText)
completion
 has developed Mistral 7B, a large language model (LLM) that is open-source and available for commercial use. Mistral 7B is a 7 billion parameter model that is trained on a diverse and high-quality dataset, and it has been fine-tuned to perform well on a variety of tasks, including text generation, question answering, and code interpretation.

MistralAI has made Mistral 7B available under a permissive license, allowing anyone to use the model for commercial purposes without having to pay any fees. This has made Mistral 7B a popular choice for businesses and organizations that want to leverage the power of large language models without incurring high costs.

Mistral 7B has been trained on a diverse and high-quality dataset, which has enabled it to perform well on a variety of tasks. It has been fine-tuned to generate coherent and contextually relevant text, and it has been shown to be capable of answering complex questions and interpreting code.

Mistral 7B is also a highly efficient model, capable of processing text at a fast pace. This makes it well-suited for applications that require real-time responses, such as chatbots and virtual assistants.

Overall, Mistral 7B is a powerful and versatile large language model that is open-source and available for commercial use. Its ability to perform well on a variety of tasks, its efficiency, and its permissive license make it a popular choice for businesses and organizations that want to leverage the power of large language models.

훅(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 { MistralAI } from "@langchain/mistralai"

const modelWithHooks = new MistralAI({
    model: "codestral-latest",
    temperature: 0,
    maxRetries: 2,
    beforeRequestHooks: [ beforeRequestHook ],
    requestErrorHooks: [ requestErrorHook ],
    responseHooks: [ responseHook ],
    // other params...
});

또는 인스턴스화 후 수동으로 할당·추가할 수도 있습니다:

import { MistralAI } from "@langchain/mistralai"

const model = new MistralAI({
    model: "codestral-latest",
    temperature: 0,
    maxRetries: 2,
    // 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 reference

모든 MistralAI 기능과 구성에 대한 자세한 문서는 API reference를 참조하세요.

더 알아보기 (Learn more)