Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created December 25, 2025 08:50
Show Gist options
  • Select an option

  • Save kuc-arc-f/32cf3de25531b219f7012b8fddd358f3 to your computer and use it in GitHub Desktop.

Select an option

Save kuc-arc-f/32cf3de25531b219f7012b8fddd358f3 to your computer and use it in GitHub Desktop.
python , Weaviate example
import weaviate
from weaviate.connect import ConnectionParams
from weaviate.classes.config import Property, DataType
import weaviate.classes.config as Configure
COLLECT_NAME = "document"
client = weaviate.WeaviateClient(
connection_params=ConnectionParams.from_url(
"http://localhost:8080",
grpc_port=50051
)
)
client.connect()
# 接続確認
print(client.is_ready())
# 1. コレクション(テーブルのようなもの)の作成
# すでに存在する場合は取得、ない場合は作成
if not client.collections.exists(COLLECT_NAME):
print("not-exist-COLLECT")
else:
print("exist-COLLECT=" + COLLECT_NAME)
articles = client.collections.get(COLLECT_NAME)
articles.data.insert(
properties={
"content": "embedding を自分で作る-1",
"category": "none"
},
vector=[0.12, 0.34, 0.41] # embedding
)
# 処理が終わったら閉じる(または context manager を使用)
client.close()
import weaviate
from weaviate.connect import ConnectionParams
from weaviate.classes.config import Property, DataType
COLLECT_NAME = "document"
client = weaviate.WeaviateClient(
connection_params=ConnectionParams.from_url(
"http://localhost:8080",
grpc_port=50051
)
)
client.connect()
# 接続確認
print(client.is_ready())
client.collections.create(
name=COLLECT_NAME,
properties=[
Property(name="content", data_type=DataType.TEXT),
Property(name="category", data_type=DataType.TEXT),
]
)
# 処理が終わったら閉じる(または context manager を使用)
client.close()
import weaviate
from weaviate.connect import ConnectionParams
from weaviate.classes.config import Property, DataType
from weaviate.classes.query import MetadataQuery
import weaviate.classes.config as Configure
COLLECT_NAME = "document"
client = weaviate.WeaviateClient(
connection_params=ConnectionParams.from_url(
"http://localhost:8080",
grpc_port=50051
)
)
client.connect()
# 接続確認
print(client.is_ready())
try:
# 1. コレクション(テーブルのようなもの)の作成
# すでに存在する場合は取得、ない場合は作成
if not client.collections.exists(COLLECT_NAME):
print("not-exist-COLLECT")
else:
print("exist-COLLECT=" + COLLECT_NAME)
collection = client.collections.get(COLLECT_NAME)
query_vector = [0.12, 0.34, 0.36]
# 処理が終わったら閉じる(または context manager を使用)
# 2. 指定したベクトルで近傍検索を実行
response = collection.query.near_vector(
near_vector=query_vector,
limit=2, # 取得件数
return_metadata=MetadataQuery(distance=True) # 距離(近さ)も取得する場合
)
# 3. 結果の表示
for obj in response.objects:
print(f"content: {obj.properties.get('content')}")
print(f"距離: {obj.metadata.distance}") # 値が小さいほど似ている
print("-" * 20)
client.close()
finally:
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment