埋め込み(embedding)は、テキストの意味を数値ベクトルとして表現する方法です。これにより、テキスト同士の「距離」や「類似度」を計算できます。セマンティック検索、クラスタリング、レコメンド、異常検知、分類など、多くの AI アプリケーションの基盤になります。YouRouter はシンプルで統一された API で主要な埋め込みモデルにアクセスできます。
from openai import OpenAIclient = OpenAI( api_key="your-api-key-here", base_url="https://api.yourouter.ai/v1")response = client.embeddings.create( input="The quick brown fox jumps over the lazy dog", model="text-embedding-ada-002",)embedding = response.data[0].embeddingprint(f"Embedding dimensions: {len(embedding)}")
response = client.embeddings.create( input=[ "First sentence to embed.", "Another sentence for batch processing.", ], model="text-embedding-ada-002",)for i, data in enumerate(response.data): print(f"Embedding for input {i+1}: {len(data.embedding)} dimensions")
import numpy as npfrom scipy.spatial.distance import cosinedocuments = [ "The sky is blue and beautiful.", "The sun is the star at the center of the Solar System.", "Artificial intelligence will reshape our world.",]doc_embeddings = [ client.embeddings.create(input=doc, model="text-embedding-ada-002").data[0].embedding for doc in documents]query = "What is the future of AI?"query_embedding = client.embeddings.create(input=query, model="text-embedding-ada-002").data[0].embeddingsimilarities = [1 - cosine(query_embedding, doc_emb) for doc_emb in doc_embeddings]most_similar_index = int(np.argmax(similarities))print(f"Query: '{query}'")print(f"Most similar document: '{documents[most_similar_index]}'")