0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LM Studio × LangChain で Embedding の 400 エラーが出たときの対処法

0
Posted at

はじめに

この記事では、LM Studio でローカルの埋め込みモデルを使用し、LangChain を使ってベクトルデータベースを構築しようとした際に発生した、以下のエラーについての解決策を備忘録としてまとめます。

Error code: 400 - {'error': "'input' field must be a string or an array of strings"}

解決策

from langchain_openai import OpenAIEmbeddings

embeddings_model = OpenAIEmbeddings(
    base_url="http://localhost:1234/v1",
    api_key="lm-studio",
    model=model_name,
    check_embedding_ctx_length=False  # ここを追加
)

検証

正常に動作したリクエスト

{
  "input": [
    "これは埋め込み生成のテスト用ドキュメントです。",
    "LangChain と LM Studio を組み合わせた検証を行っています。"
  ],
  "model": "text-embedding-embeddinggemma-300m-qat",
  "encoding_format": "base64"
}

この形式では、LM Studio 側でも Received request to embed multiple とログに出力され、正常に埋め込みが生成されていました。

エラーが発生したリクエスト

一方で、エラーが発生した際には、次のようなリクエストが送信されていました。

{
  "input": [
    [85701, 15682, 35722, 233, 62004, 16205, 120, 64121, 45059],
    [27317, 19368, 220, 19732, 47514, 19074, 95371, 58254]
  ],
  "model": "text-embedding-embeddinggemma-300m-qat",
  "encoding_format": "base64"
}

この場合、input フィールドは 文字列の配列ではなく、トークン ID(数値)の配列 になっていました。

原因

OpenAI の公式 API ドキュメントでは input の説明として次のように書かれていました。

Input
string or array

Input text to embed, encoded as a string or array of tokens.
To embed multiple inputs in a single request, pass an array of strings or array of token arrays.

つまり、OpenAI 本家の埋め込み API では、input として文字列だけでなく、文字列配列やトークン ID の配列(およびその配列)も受け付ける仕様になっています。

一方で、LM Studio の OpenAI 互換 API では、実際のリクエストログやエラーメッセージから判断すると、入力は文字列、もしくは文字列の配列を前提とした実装になっており、トークン ID の配列が渡された場合には正しく解釈できず、エラーが発生していました。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?