SenseNova: SenseTimeによるAIチャットと画像生成
SenseTimeのSenseNovaチュートリアルへようこそ!SenseNovaは、中国を代表するAI企業であるSenseTimeが開発した大規模マルチモーダルAIモデルのファミリーです。SenseNovaプラットフォームはOpenAI互換APIを提供し、既存のワークフローへの統合が容易です。

このノートブックでは、2つの強力なSenseNovaモデルを探索します:
- SenseNova 6.7 Flash-Lite: テキスト会話をサポートし、モデルの思考プロセスを示す独自のreasoningフィールドを含む、高速で効率的なチャットモデル。
- SenseNova U1 Fast: テキストプロンプトから高品質な画像を生成する画像生成モデル。
カバーする重要な概念:
- OpenAI SDK互換性: OpenAI Python SDKをSenseNovaのAPIで使用する。
- チャット補完: 基本的および多ターンのテキスト会話。
- Reasoningフィールド: モデルの内部思考プロセスを探索する。
- 画像生成: テキストプロンプトから画像を作成して表示する。
1. セットアップ
まず、必要なPythonライブラリをインストールし、API認証情報を設定します。
1.1 ライブラリのインストール
SenseNovaのAPIと互換性のあるopenaiPython SDKと、追加のHTTP呼び出し用のrequestsを使用します。
# Uncomment the following line to install the required packages
# %pip install openai requests1.2 APIキーの設定とクライアントの初期化
SenseNovaのAPIは完全にOpenAI互換です。base_urlをhttps://token.sensenova.cn/v1に向け、APIキーを提供するだけです。APIキーはSenseNovaプラットフォームから取得できます。
セキュリティのため、APIキーは環境変数または.envファイルに保存することをお勧めします。このデモでは、環境変数SENSENOVA_API_KEYを使用します。
import os
from openai import OpenAI
# Set the SENSENOVA_API_KEY environment variable before running
SENSENOVA_API_KEY = os.environ.get("SENSENOVA_API_KEY")
SENSENOVA_BASE_URL = "https://token.sensenova.cn/v1"
# Initialize the OpenAI client with SenseNova's base URL
client = OpenAI(
api_key=SENSENOVA_API_KEY,
base_url=SENSENOVA_BASE_URL,
)
print("SenseNova client initialized successfully!")
print(f"Base URL: {SENSENOVA_BASE_URL}")SenseNova client initialized successfully! Base URL: https://token.sensenova.cn/v1
2. SenseNova 6.7 Flash-Liteでのチャット補完
SenseNova 6.7 Flash-Liteは、高速で効率的なチャットモデルです。標準的なOpenAIチャット補完形式を使用するため、すでに慣れ親しんだSDK呼び出しをそのまま利用できます。
注目すべき機能の1つは、応答のreasoningフィールドです。これにより、モデルの内部chain-of-thought思考プロセスが明らかになります。
2.1 基本的なテキストチャット
簡単なシングルターンのチャット補完から始めましょう。
# Basic text chat completion
response = client.chat.completions.create(
model="sensenova-6.7-flash-lite",
messages=[
{"role": "user", "content": "What is SenseNova by SenseTime? Answer in 2-3 sentences."}
],
)
print("=== Response ===")
print(response.choices[0].message.content)=== Response === SenseNova is a large-scale artificial intelligence model developed by SenseTime that integrates multi-modal capabilities such as natural language processing and computer vision. It serves as a foundational technology designed to power diverse AI applications across various industries. Through its advanced generative features, SenseNova aims to enhance productivity and drive innovation in the AI landscape.
2.2 Reasoningフィールドの探索
SenseNova 6.7 Flash-Liteの独自の機能は、応答メッセージに含まれるreasoningフィールドです。このフィールドには、モデルが回答に至るまでの内部chain-of-thoughtが含まれています。これは、デバッグ、透明性、モデルの意思決定プロセスを理解する上で貴重です。
# Let's look at the reasoning field
response = client.chat.completions.create(
model="sensenova-6.7-flash-lite",
messages=[
{"role": "user", "content": "Is 97 a prime number? Explain briefly."}
],
)
message = response.choices[0].message
print("=== Final Answer ===")
print(message.content)
print()
print("=== Reasoning (Chain of Thought) ===")
if message.reasoning:
# Print the first 800 characters of the reasoning for readability
print(message.reasoning[:800])
if len(message.reasoning) > 800:
print("... (truncated)")
else:
print("No reasoning field found.")=== Final Answer ===
Yes, 97 is a prime number. It is only divisible by 1 and itself, and it is the largest two-digit prime number.
=== Reasoning (Chain of Thought) ===
Thinking Process:
1. **Analyze the Request:**
* Question: "Is 97 a prime number?"
* Constraint: "Explain briefly."
* Identity: I am SenseNova 6.7 Flash-Lite, developed by SenseTime.
2. **Determine the Fact:**
* Is 97 prime?
* A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
* Check divisors for 97:
* Not divisible by 2 (odd).
* Not divisible by 3 (sum of digits 9+7=16, not divisible by 3).
* Not divisible by 5 (doesn't end in 0 or 5).
* Check 7: 7 * 10 = 70, 97-70=27, not divisible by 7.
* Check 11: 11 * 9 = 99, too high.
* Square root of 97 is slightly less than 10 (since 10^2 = 100). So I only need to check primes up to 7.
* Con
... (truncated)
2.3 多ターン会話
SenseNova APIは、完全なメッセージ履歴を渡すことで多ターン会話をサポートします。以前のターンの情報を参照する会話を示します。
# Multi-turn conversation
response = client.chat.completions.create(
model="sensenova-6.7-flash-lite",
messages=[
{"role": "user", "content": "My favorite programming language is Python. Remember that!"},
{"role": "assistant", "content": "Got it! Python is your favorite programming language. I will remember that for our conversation."},
{"role": "user", "content": "What is my favorite programming language and why might it be popular for AI?"},
],
)
print("=== Multi-Turn Response ===")
print(response.choices[0].message.content)=== Multi-Turn Response === Based on what you shared earlier, your favorite programming language is **Python**. Python is widely considered the standard language for AI and machine learning for several key reasons: 1. **Rich Ecosystem:** It has powerful, specialized libraries and frameworks like **TensorFlow**, **PyTorch**, **scikit-learn**, and **Keras** that simplify building complex models. 2. **Simplicity and Readability:** Its clean syntax allows developers to focus on solving AI problems rather than getting bogged down in complex code structures, making it great for prototyping. 3. **Data Science Tools:** It offers excellent support for data manipulation and analysis with libraries like **pandas** and **numpy**, which are essential for preparing data for AI. 4. **Community Support:** A massive global community means there are endless tutorials, documentation, and forums available for troubleshooting. 5. **Interoperability:** Python can easily integrate with high-performance languages like C++ for backend operations, combining ease of use with speed. It's a great choice for both learning and professional AI development!
3. SenseNova U1 Fastによる画像生成
SenseNova U1 Fastは、テキストプロンプトから高品質な画像を生成する画像生成モデルです。これもOpenAI互換のエンドポイントを使用するため、OpenAI SDKのclient.images.generate()メソッドを使用できます。
利用可能な画像サイズ: 1664x2496, 2496x1664, 1760x2368, 2368x1760, 1824x2272, 2272x1824, 2048x2048, 2752x1536, 1536x2752, 3072x1376, 1344x3136
3.1 画像の生成
クリエイティブなプロンプトを使用して画像を生成しましょう。APIは、画像を表示またはダウンロードするために使用できる署名付きURLを返します。
import json
# Generate an image using SenseNova U1 Fast
image_response = client.images.generate(
model="sensenova-u1-fast",
prompt="A cute cat wearing a tiny astronaut helmet floating in space with stars in the background, digital art style",
size="2048x2048",
n=1,
)
# Get the image URL
image_url = image_response.data[0].url
print("=== Image Generated Successfully ===")
print(f"Image URL: {image_url[:100]}...")
print(f"\nFull response metadata:")
print(json.dumps(image_response.model_dump(), indent=2, default=str)[:500])=== Image Generated Successfully ===
Image URL: https://aoss.cn-sh-01.sensecoreapi-oss.cn/nova-clotho-images-sh02/image/019e1acc-d571-7456-86c0-38d8...
Full response metadata:
{
"created": 1781246973,
"background": null,
"data": [
{
"b64_json": null,
"revised_prompt": null,
"url": "https://aoss.cn-sh-01.sensecoreapi-oss.cn/nova-clotho-images-sh02/image/019e1acc-d571-7456-86c0-38d8f5d35ec2/2026/6/12/8b14b312-1c93-44a4-a951-cfa800d1a202_0.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=7EA4DE1929F0442E9402B33E0B6EEF70%2F20260612%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20260612T064936Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz
3.2 生成された画像の表示
APIは、生成された画像の署名付きURLを返します。ノートブック内で直接ダウンロードして表示しましょう。
import requests
from IPython.display import Image, display
# Download and display the generated image
image_data = requests.get(image_url).content
# Save the image locally
with open("sensenova_generated_image.png", "wb") as f:
f.write(image_data)
print("Image saved as 'sensenova_generated_image.png'")
print("Displaying the generated image:")
# Display in notebook
display(Image(filename="sensenova_generated_image.png", width=512))/Users/team/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 warnings.warn(
Image saved as 'sensenova_generated_image.png' Displaying the generated image:
4. まとめ
おめでとうございます!🎉 このノートブックでは、SenseNova 6.7 Flash-Liteを使用したテキストチャット、reasoningフィールドの探索、SenseNova U1 Fastを使用した画像生成までを学びました。
SenseNovaのOpenAI互換APIにより、既存のコードベースやツールへの統合がシームレスになります。ぜひ、より創造的なプロンプトやアプリケーションを試してみてください。Happy building!