Skip Navigation
Show nav
Dev Center
  • Get Started
  • ドキュメント
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • ドキュメント
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku のアーキテクチャ
    • コンピューティング (dyno)
      • dyno の管理
      • dyno の概念
      • dyno の動作
      • dyno の参照資料
      • dyno のトラブルシューティング
    • スタック (オペレーティングシステムイメージ)
    • ネットワーキングと DNS
    • プラットフォームポリシー
    • プラットフォームの原則
  • 開発者ツール
    • コマンドライン
    • Heroku の VS Code 拡張機能
  • デプロイ
    • Git を使用したデプロイ
    • Docker によるデプロイ
    • デプロイ統合
  • 継続的デリバリーとインテグレーション
    • 継続的統合
  • 言語サポート
    • Node.js
      • Node.js アプリのトラブルシューティング
      • Heroku での Node.js の動作
      • Node.js の操作
    • Ruby
      • Rails のサポート
      • Bundler の使用
      • Ruby の操作
      • Heroku での Ruby の動作
      • Ruby アプリのトラブルシューティング
    • Python
      • Python の操作
      • Python でのバックグラウンドジョブ
      • Heroku での Python の動作
      • Django の使用
    • Java
      • Heroku での Java の動作
      • Java の操作
      • Maven の使用
      • Spring Boot の使用
      • Java アプリのトラブルシューティング
    • PHP
      • PHP の操作
      • Heroku での PHP の動作
    • Go
      • Go の依存関係管理
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • データベースとデータ管理
    • Heroku Postgres
      • Postgres の基礎
      • Postgres スターターガイド
      • Postgres のパフォーマンス
      • Postgres のデータ転送と保持
      • Postgres の可用性
      • Postgres の特別なトピック
      • Heroku Postgres への移行
    • Heroku Key-Value Store
    • Apache Kafka on Heroku
    • その他のデータストア
  • AI
    • Vector Database
    • Working with AI
    • Heroku Inference
      • AI Models
      • Inference Essentials
      • Heroku Inference Quick Start Guides
      • Inference API
    • Model Context Protocol
  • モニタリングとメトリクス
    • ログ記録
  • アプリのパフォーマンス
  • アドオン
    • すべてのアドオン
  • 共同作業
  • セキュリティ
    • アプリのセキュリティ
    • ID と認証
      • シングルサインオン (SSO)
    • Private Space
      • インフラストラクチャネットワーキング
    • コンプライアンス
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Team
    • Heroku Connect (Salesforce 同期)
      • Heroku Connect の管理
      • Heroku Connect のリファレンス
      • Heroku Connect のトラブルシューティング
  • パターンとベストプラクティス
  • Heroku の拡張
    • Platform API
    • アプリの Webhook
    • Heroku Labs
    • アドオンのビルド
      • アドオン開発のタスク
      • アドオン API
      • アドオンのガイドラインと要件
    • CLI プラグインのビルド
    • 開発ビルドパック
    • Dev Center
  • アカウントと請求
  • トラブルシューティングとサポート
  • Salesforce とのインテグレーション
  • AI
  • Heroku Inference
  • Heroku Inference Quick Start Guides
  • v1-embeddings API の Python クイックスタートガイド

v1-embeddings API の Python クイックスタートガイド

日本語 — Switch to English

この記事の英語版に更新があります。ご覧の翻訳には含まれていない変更点があるかもしれません。

最終更新日 2025年01月24日(金)

Table of Contents

  • 前提条件
  • Python のサンプルコード

Heroku Managed Inference and Agent アドオンは現在パイロット段階です。パイロットの一環として提供される製品は本番環境での使用を目的としたものではなく、ベータサービスとみなされています。また、https://www.salesforce.com/company/legal/agreements.jsp​ のベータサービス条件が適用されます。

Cohere Embed Multilingual​ (cohere-embed-multilingual​) モデルは、提供されたテキスト入力に対するベクトル埋め込み (数値のリスト) を生成します。これらの埋め込みは、検索、分類、クラスタリングなどのさまざまなアプリケーションで使用できます。このガイドでは、Python を使用してv1-embeddings​ API にアクセスする方法について説明します。

前提条件

リクエストを行う前に、選択したモデルへのアクセスをプロビジョニングします。

  1. まだインストールされていない場合は、Heroku CLI​ をインストールします。次に、Heroku AI プラグインをインストールします。

    heroku plugins:install @heroku/plugin-ai
    
  2. 埋め込みモデルをアプリにアタッチします。

    # If you don't have an app yet, you can create one with:
    heroku create $APP_NAME # specify the name you want for your app (or skip this step to use an existing app you have)
    
    # Create and attach one of our chat models to your app, $APP_NAME:
    
    heroku ai:models:create -a $APP_NAME cohere-multilingual --as EMBEDDING
    
  3. 必要な requests​ パッケージをインストールします。

    pip install requests
    

Python のサンプルコード

import requests
import json
import os

# Global variables for API endpoint, authorization key, and model ID from Heroku config variables
ENV_VARS = {
    "EMBEDDING_URL": None,
    "EMBEDDING_KEY": None,
    "EMBEDDING_MODEL_ID": None
}

# Assert the existence of required environment variables, with helpful messages if they're missing.
for env_var in ENV_VARS.keys():
    value = os.environ.get(env_var)
    assert value is not None, (
        f"Environment variable '{env_var}' is missing. Set it using:\n"
        f"export {env_var}=$(heroku config:get -a $APP_NAME {env_var})"
    )
    ENV_VARS[env_var] = value


def parse_embedding_output(response):
    """
    Parses and prints the API response for the embedding request.

    Parameters:
        - response (requests.Response): The response object from the API call.
    """
    if response.status_code == 200:
        result = response.json()
        print("Embeddings:", result["data"])
    else:
        print(f"Request failed: {response.status_code}, {response.text}")

def generate_embeddings(payload):
    """
    Generates embeddings using the Stability AI Embeddings model.

    Parameters:
        - payload (dict): dictionary containing parameters for the embedding generation

    Returns:
        - Prints the generated embeddings.
    """
    # Set headers using the global API key
    HEADERS = {
        "Authorization": f"Bearer {ENV_VARS['EMBEDDING_KEY']}",
        "Content-Type": "application/json"
    }
    endpoint_url = ENV_VARS['EMBEDDING_URL'] + "/v1/embeddings"
    response = requests.post(endpoint_url, headers=HEADERS, data=json.dumps(payload))

    parse_embedding_output(response=response)


# Example payload
payload = {
    "model": ENV_VARS["EMBEDDING_MODEL_ID"],
    "input": ["Hello, I am a blob of text.", "How's the weather in Portland?"],
    "input_type": "search_document",
    "truncate": "END",
    "encoding_format": "float"
}

# Generate embeddings with the given payload
generate_embeddings(payload)

関連カテゴリー

  • Heroku Inference Quick Start Guides
v1-images-generations API の Ruby クイックスタートガイド v1-embeddings API の Ruby クイックスタートガイド

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure
  • .NET

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices