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?

すべてlocalで、LM StudioとChroma DBを動かす

Last updated at Posted at 2025-08-10

なぜ

新しいパソコンを買ったので、流行りの生成AIをローカルで作ってみた。

やったこと

chatgptに聞きながら、最小構成でのコードを動かす。

実際のコード

from openai import OpenAI
from langchain_community.vectorstores import Chroma
from langchain.docstore.document import Document

# LM Studio APIクライアント
client = OpenAI(base_url="http://localhost:1234/v1", api_key="not-needed")
model_name = "text-embedding-nomic-embed-text-1.5"

# 埋め込みラッパークラス
class LMStudioEmbedding:
    def embed_documents(self, texts):
        return [client.embeddings.create(model=model_name, input=t).data[0].embedding for t in texts]

    def embed_query(self, text):
        return self.embed_documents([text])[0]

embedding_fn = LMStudioEmbedding()

# データ
docs = [
    Document(page_content="横浜は港町で、みなとみらいには観覧車がある。"),
    Document(page_content="AWSのIAMロールは、人が一時的に引き受ける(Assume)仕組み。"),
    Document(page_content="CIDRは可変長サブネットマスクでアドレスを表す方式。"),
]

# Chromaに投入
vectordb = Chroma.from_documents(docs, embedding=embedding_fn, collection_name="demo-lms")

print("✅ Vector store built.")

# 類似検索
hits = vectordb.similarity_search("IAMって何?", k=2)
for i, h in enumerate(hits, 1):
    print(f"[{i}] {h.page_content}")

実行結果

(venv) PS C:\workspace\llm>  c:; cd 'c:\workspace\llm'; & 'c:\workspace\llm\venv\Scripts\python.exe' 'c:\Users\hoge\.vscode\extensions\ms-python.debugpy-2025.10.0-win32-arm64\bundled\libs\debugpy\launcher' '52954' '--' 'C:\workspace\llm\src\second_test.py' 
✅ Vector store built.
[1] 横浜は港町で、みなとみらいには観覧車がある。
[2] AWSのIAMロールは、人が一時的に引き受ける(Assume)仕組み。
(venv) PS C:\workspace\llm> 

みなとみらいのほうがAWSより上に来るのは謎だが、結果は出た。

前準備

  1. 必要パッケージの用意
    pip install -U openai langchain-community chromadb
  2. LM Studio 側の準備
    1. LM Studioのインストール
      下記のサイトから自分のOSに合わせたインストーラをダウンロードしてインストール
      https://lmstudio.ai/
    2. 適当なembeddingモデルをダウンロード
      今回は下記をダウンロード
      text-embedding-nomic-embed-text-1.5
    3. ローカルサーバで起動
      下記のような形でサーバとして起動。
      image.png
    4. サーバとの通信の確認
      下記を実行。
      curl http://127.0.0.1:1234/v1/embeddings ^
      -H "Content-Type: application/json" ^
      -d "{ \"model\": \"text-embedding-nomic-embed-text-1.5\", \"input\": \"Some text to embed\"}"
    
    なんかたくさん数字が入ったJSONが返ってくればOK。下記のような画面。結果は長いので抜粋。
    image.png

ハマりポイント

  1. model_name
    7行目でmodel_nameを指定しているが何を指定するかでしばらく悩んだ。結果的には LM StudioのAPI identifierの文字を指定する。どうもモデルには何種類かあるらしく、Domainembeddingとなっているものを選ばないとならないらしい。
    image.png

  2. Chroma.from_documents()のembedding引数
    Chroma.from_xx()関数が何種類かあるみたいなのだが、chatgptが教えてくれる関数をたたいても、エラーばかり出て進めなかった。最終的にラッパークラスを作ってそれを渡したらうまく動くようになった。

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?