[Python] Use LM Studio LLM model as embedding model with LangChain

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    In LM Studio, I have run a OpenAI server exposing a Gemma 3 model:

    [​IMG]

    Now consider the following code:

    from langchain_openai import OpenAIEmbeddings

    # Load embeddings
    embeddings = OpenAIEmbeddings(
    model="gemma-3-27b-it-qat",
    openai_api_base="http://127.0.0.1:3003/v1/",
    openai_api_key="not-needed",
    # We need this to avoid error `'input' field must be a string or an array of strings`
    # See: https://github.com/langchain-ai/langchain/issues/21318
    check_embedding_ctx_length=False,
    )

    # Test embeddings
    input_text = "The meaning of life is 42"
    vector = embeddings.embed_query("hello")
    print(vector[:3])


    It returns the following error:

    ---------------------------------------------------------------------------
    NotFoundError Traceback (most recent call last)
    Cell In[4], line 12
    10 # Test embeddings
    11 input_text = "The meaning of life is 42"
    ---> 12 vector = embeddings.embed_query("hello")
    13 print(vector[:3])

    File c:\Users\USER\miniforge3\envs\langchain\Lib\site-packages\langchain_openai\embeddings\base.py:617, in OpenAIEmbeddings.embed_query(self, text)
    608 def embed_query(self, text: str) -> list[float]:
    609 """Call out to OpenAI's embedding endpoint for embedding query text.
    610
    611 Args:
    (...) 615 Embedding for the text.
    616 """
    --> 617 return self.embed_documents([text])[0]

    File c:\Users\USER\miniforge3\envs\langchain\Lib\site-packages\langchain_openai\embeddings\base.py:565, in OpenAIEmbeddings.embed_documents(self, texts, chunk_size)
    563 embeddings: list[list[float]] = []
    564 for i in range(0, len(texts), chunk_size_):
    --> 565 response = self.client.create(
    566 input=texts[i : i + chunk_size_], **self._invocation_params
    567 )
    568 if not isinstance(response, dict):
    569 response = response.dict()

    File c:\Users\USER\miniforge3\envs\langchain\Lib\site-packages\openai\resources\embeddings.py:128, in Embeddings.create(self, input, model, dimensions, encoding_format, user, extra_headers, extra_query, extra_body, timeout)
    122 embedding.embedding = np.frombuffer( # type: ignore[no-untyped-call]
    123 base64.b64decode(data), dtype="float32"
    124 ).tolist()
    126 return obj
    --> 128 return self._post(
    129 "/embeddings",
    130 body=maybe_transform(params, embedding_create_params.EmbeddingCreateParams),
    131 options=make_request_options(
    132 extra_headers=extra_headers,
    133 extra_query=extra_query,
    134 extra_body=extra_body,
    135 timeout=timeout,
    136 post_parser=parser,
    137 ),
    138 cast_to=CreateEmbeddingResponse,
    139 )

    File c:\Users\USER\miniforge3\envs\langchain\Lib\site-packages\openai\_base_client.py:1276, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls)
    1262 def post(
    1263 self,
    1264 path: str,
    (...) 1271 stream_cls: type[_StreamT] | None = None,
    1272 ) -> ResponseT | _StreamT:
    1273 opts = FinalRequestOptions.construct(
    1274 method="post", url=path, json_data=body, files=to_httpx_files(files), **options
    1275 )
    -> 1276 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))

    File c:\Users\USER\miniforge3\envs\langchain\Lib\site-packages\openai\_base_client.py:949, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)
    946 else:
    947 retries_taken = 0
    --> 949 return self._request(
    950 cast_to=cast_to,
    951 options=options,
    952 stream=stream,
    953 stream_cls=stream_cls,
    954 retries_taken=retries_taken,
    955 )

    File c:\Users\USER\miniforge3\envs\langchain\Lib\site-packages\openai\_base_client.py:1057, in SyncAPIClient._request(self, cast_to, options, retries_taken, stream, stream_cls)
    1054 err.response.read()
    1056 log.debug("Re-raising status error")
    -> 1057 raise self._make_status_error_from_response(err.response) from None
    1059 return self._process_response(
    1060 cast_to=cast_to,
    1061 options=options,
    (...) 1065 retries_taken=retries_taken,
    1066 )

    NotFoundError: Error code: 404 - {'error': {'message': 'No models loaded. Please load a model in the developer page or use the `lms load` command.', 'type': 'invalid_request_error', 'param': 'model', 'code': 'model_not_found'}}


    However, if I load an embedding model, for instance:

    [​IMG]

    Then it works and the code returns:

    [-0.0032659093849360943, 0.017258411273360252, -0.015825312584638596]


    Notice that this works even if model="gemma-3-27b-it-qat" stays as it is (and the embedding model I loaded isn't that).

    I thought I should/could use the same LLM for the embedding.

    How can I use the same LLM for the embeddings?

    If I can't, what embedding model should I use?

    Continue reading...

Compartilhe esta Página