現代の AI モデルは、単なるテキスト生成を超えて進化しています。関数呼び出し ** や ** 構造化出力 ** といった機能により、開発者はよりスマートで信頼性の高いアプリケーションを構築できるようになりました。Novita AI では、コンソール上でモデルがこれらの機能をサポートしているかどうかを ** すぐに確認 できるため、統合をより迅速かつ効率的に行えます。
関数呼び出しとは?
関数呼び出しは、特に大規模言語モデル (LLM) における AI の機能であり、モデルがテキスト応答の生成を超えて、外部の関数、ツール、API と連携して特定のタスクを実行できるようにします。モデルは単にテキストを生成するだけでなく、ユーザーのプロンプトに基づいて特定の関数を呼び出す必要があるタイミングを識別し、呼び出す正しい関数を決定し、その関数に必要なパラメータを指定します。関数の実際の実行は、モデル自体ではなく、外部のシステムやアプリケーションによって処理されます。
https://www.youtube.com/watch?v=aqdWSYWC\_LI
関数呼び出しの仕組み
関数呼び出しにより、AI モデルは会話の中で外部ツール、API、内部関数と連携できるようになります。モデルは単にテキストを生成するだけでなく、**関数が必要なタイミングを認識 ** し、**構造化された引数を持つ正しい関数呼び出しを生成 ** し、それを実行して結果を取得 します。これにより、自然言語理解と実際のアクションとの間のギャップが埋められ、モデルはリアルタイムデータの取得、ワークフローの管理、システムアクションの自動トリガーなどのタスクを実行できます。
関数呼び出しの利点は?
- 実際のアクションとの連携: モデルは情報を提供するだけでなく、アクションをトリガーできます。
- 自動化: データ取得、予約、フォーム送信などのプロセスを効率化します。
- 構造化された理解: テキスト生成だけに依存するのではなく、モデルは出力を事前定義された形式に整理し、エラーを減らします。
- ユースケースの拡大: より信頼性が高く効率的な動的アプリ、アシスタント、システムを構築できます。
- 開発効率の向上: モデルが関数呼び出しをサポートしていることを事前に把握することで、大規模な修正なしで統合を迅速に計画できます。
構造化出力とは?
構造化出力とは、大規模言語モデル (LLM) が自由形式のテキストではなく、事前定義された特定の形式に従った応答を生成する機能を指します。これらの出力は通常、JSON、XML、埋め込まれたテンプレート、表データなどの機械可読形式であり、ソフトウェアシステムやワークフローへの解析、検証、統合が容易です。
構造化出力の生成方法
- プロンプティング手法: 注意深く作成されたプロンプトを通じてモデルを誘導し、目的の形式で出力を生成させます。
- 関数呼び出し API: より堅牢な方法として、関数のスキーマに厳密に従った引数をモデルが返す関数呼び出しインターフェースを使用します。これは多くの場合、
strict: trueのようなパラメータを設定することで強制されます。 - 有限状態機械 (FSM) ガイダンス: 内部的には、生成される各トークンが出力形式のルールに従うように制約され、構造のエラーを削減します。
ユースケースと利点
- 非構造化テキストから、イベントの詳細、ユーザー情報、クエリパラメータなどの構造化データを抽出する。
- モデルによって生成された正確なパラメータを使用して、外部 API や関数をトリガーする。
- 一貫性と検証されたデータ交換に依存する複雑なマルチステップワークフローを構築する。
- モデル出力のあいまいさを減らすことで、チャットボット、データ抽出、自動化システムなどのアプリケーションの信頼性を向上させる。
関数呼び出しと構造化出力
| 側面 | 🛠️ 関数呼び出し | 🗂️ 構造化出力 |
|---|---|---|
| 目的 | 外部関数をトリガーする | 構造化情報を返す |
| 方向性 | アクション指向 | 情報指向 |
| 出力 | 関数名 + 引数 | 事前定義された構造化データ (例: JSON) |
| 例 | 天気予報を取得するために天気 API を呼び出す | JSON 形式で天気レポートを返す |
| 使用シナリオ | モデルにアクションを実行させる必要がある場合 | アクションなしでクリーンで解析可能なデータが必要な場合 |
Novita AI で関数呼び出しと構造化出力を確認する方法
1: Novita AI にログイン
Novita AI のホームページにアクセスしたら、右上の 「Log In」 または 「Get Started」 ボタンをクリックします。Google、GitHub、Hugging Face、または ** メールアドレス** で簡単にログインできます。


コンソールで「Model API」をクリック!
ログインすると、Novita コンソールのダッシュボードに移動します。上部の 「Model API」 をクリックします。このセクションでは、利用可能なモデルの完全なリストと、各モデルの機能(** 関数呼び出し ** や ** 構造化出力** のサポート状況を含む)の詳細情報にアクセスできます。

モデルを選択して確認!
目的のモデルを見つけてクリックすると、右側にパネルが表示されます。「サポートされている機能」 の下に、** 関数呼び出し ** と ** 構造化出力** がサポートされているかどうかがすぐに表示されます。


Novita AI で関数呼び出しを使用する方法
1. クライアントを初期化する
まず、Novita API キーを使用してクライアントを初期化します。
from openai import OpenAI
import json
client = OpenAI(
base_url="https://api.novita.ai/v3/openai",
# Get the Novita AI API Key from: https://novita.ai/settings/key-management.
api_key="<YOUR Novita AI API Key>",
)
model = "meta-llama/llama-4-maverick-17b-128e-instruct-fp8"
- 呼び出す関数を定義する
次に、モデルが呼び出すことができる Python 関数を定義します。この例では、天気情報を取得する関数です。
# Example function to simulate fetching weather data.
def get_weather(location):
"""Retrieves the current weather for a given location."""
print("Calling get_weather function with location: ", location)
# In a real application, you would call an external weather API here.
# This is a simplified example returning hardcoded data.
return json.dumps({"location": location, "temperature": "60 degrees Fahrenheit"})
2. ツールとユーザーメッセージを含む API リクエストを構築する
次に、Novita エンドポイントへの API リクエストを作成します。このリクエストには、モデルが使用できる関数を定義する tools パラメータと、ユーザーのメッセージが含まれます。
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of an location, the user shoud supply a location first",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["location"]
},
}
},
]
messages = [
{
"role": "user",
"content": "What is the weather in San Francisco?"
}
]
# Let's send the request and print the response.
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
)
# Please check if the response contains tool calls if in production.
tool_call = response.choices[0].message.tool_calls[0]
print(tool_call.model_dump())
3. 出力
{'id': '0', 'function': {'arguments': '{"location": "San Francisco, CA"}', 'name': 'get_weather'}, 'type': 'function'}
4. 関数呼び出しの結果で応答し、最終的な回答を取得する
次のステップは、関数呼び出しを処理し、get_weather 関数を実行し、その結果をモデルに送り返してユーザーに対する最終的な応答を生成することです。
# Ensure tool_call is defined from the previous step
if tool_call:
# Extend conversation history with the assistant's tool call message
messages.append(response.choices[0].message)
function_name = tool_call.function.name
if function_name == "get_weather":
function_args = json.loads(tool_call.function.arguments)
# Execute the function and get the response
function_response = get_weather(
location=function_args.get("location"))
# Append the function response to the messages
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"content": function_response,
}
)
# Get the final response from the model, now with the function result
answer_response = client.chat.completions.create(
model=model,
messages=messages,
# Note: Do not include tools parameter here.
)
print(answer_response.choices[0].message)
5. 出力
{'id': '0', 'function': {'arguments': '{"location": "San Francisco, CA"}', 'name': 'get_weather'}, 'type': 'function'}
Novita AI で構造化出力を使用する方法
1. クライアントを初期化する
まず、Novita API キーを使用してクライアントを初期化します。
from openai import OpenAI
client = OpenAI(
base_url="https://api.novita.ai/v3/openai",
# Get the Novita AI API Key from: https://novita.ai/settings/key-management.
api_key="<YOUR Novita AI API Key>",
)
# Go to the [Models](https://novita.ai/models) page to see the models that support `Structured Outputs`.
model = "mistralai/mistral-7b-instruct"
2. JSON スキーマを定義する
この例では、ユーザーの入力から経費情報を抽出するためのスキーマを作成します。
# Define system prompt for expense tracking.
system_prompt = """You are an expense tracking assistant.
Extract expense information from the user's input and format it according to the provided schema."""
# Define JSON schema for structured response.
response_format = {
"type": "json_schema",
"json_schema": {
"name": "expense_tracking_schema",
"schema": {
"type": "object",
"properties": {
"expenses": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string",
"description": "Description of the expense"
},
"amount": {
"type": "number",
"description": "Amount spent in dollars"
},
"date": {
"type": "string",
"description": "When the expense occurred"
},
"category": {
"type": "string",
"description": "Category of expense (e.g., food, office, travel)"
}
},
"required": [
"description",
"amount"
]
}
},
"total": {
"type": "number",
"description": "Total amount of all expenses"
}
},
"required": [
"expenses",
"total"
],
},
},
}
3. チャット補完 API をリクエストする
次に、Novita エンドポイントにチャット補完リクエストを送信します。
このリクエストには、前の手順で定義した JSON スキーマを指定する response_format パラメータが含まれます。
chat_completion = client.chat.completions.create(
model=model,
messages=[
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": """I spent $120 on dinner at an Italian restaurant last Friday with my colleagues.
Also bought office supplies for $45 on Monday.""",
},
],
max_tokens=1024,
temperature=0.8,
stream=False,
response_format=response_format,
)
response_content = chat_completion.choices[0].message.content
# Parse and prettify the JSON
try:
json_response = json.loads(response_content)
prettified_json = json.dumps(json_response, indent=2)
print(prettified_json)
except json.JSONDecodeError:
print("Could not parse response as JSON. Raw response:")
print(response_content)
4. 出力:
{
"expenses": [
{
"date": "2023-03-17",
"description": "Dinner at Italian restaurant",
"amount": 120,
"category": "Food & Dining"
},
{
"date": "2023-03-13",
"description": "Office supplies",
"amount": 45,
"category": "Office Supplies"
}
],
"total": 165
}
関数呼び出しと構造化出力により、AI モデルが実行できることが大幅に拡張されます。ワークフローの自動化、リアルタイムデータの取得、構造化情報の抽出など、Novita AI を使用すると、ニーズに合ったモデルを簡単に見つけることができます。今すぐ Novita コンソール でモデルを探索し、よりスマートなアプリケーションの構築を始めましょう!
よくある質問
AI モデルにおける関数呼び出しとは何ですか?
ユーザーのプロンプトに基づいて構造化された関数呼び出しを生成し、外部ツールや API をトリガーできるようにする機能です。
構造化出力とは何ですか?
構造化出力とは、JSON のような厳密で機械可読な形式でのモデルの応答を指し、簡単な統合に最適です。
関数呼び出しと構造化出力の違いは何ですか?
関数呼び出しはアクション駆動型 (例: 天気の取得) であるのに対し、構造化出力はクリーンでフォーマットされたデータを返すことに焦点を当てています。
Novita AI は、AI への野心を強化するオールインワンのクラウドプラットフォームです。統合 API、サーバーレス、GPU インスタンス — コスト効率の高いツールを提供します。インフラストラクチャを排除し、無料で始めて、AI のビジョンを現実にしましょう。
