Gemma 3 27B:多模態 AI 結合 LangChain 實現 Function Calling

Gemma 3 27B:多模態 AI 結合 LangChain 實現 Function Calling

重點摘要

Gemma 3 27B 是由 Google 發佈的最新多模態 AI 模型,擁有 **270 億個參數 ,支援 ** 超過 140 種語言

透過 LangChain 等工具的 Function Calling 功能,可與外部系統無縫整合,擴展其執行專業任務的能力,例如計算、數據分析與工作流程自動化。

結合 Gemma 3 27B 的 多模態能力,Function Calling 能大幅提升任務自動化,實現文字、影像處理與外部工具之間的高階協作。開源特性促進了各產業的廣泛採用與創新。

Gemma 3 27B 代表了多模態 AI 的突破,在理解與生成文字及影像方面提供了無與倫比的能力。憑藉 270 億個參數與支援超過 140 種語言,它能輕鬆處理跨產業的複雜任務。

Gemma 3 27B 是什麼?

發佈日期

2025 年 3 月 12 日

模型規模

270 億個參數

開源

是(由 Google 發佈)

語言支援

超過 140 種語言

訓練資料

14 兆個 Token

強項

數學、程式碼、指令遵循

多模態能力

有(處理影像與文字,輸出文字)

上下文視窗

128K Token

Gemma 3 27B 多模態能力測試

提示: 告訴我從 Gemma 2 27B 到 Gemma 3 27B 的 Elo 分數增加了多少倍?

ELO SCORE

資料來源:Google

輸出: 準確識別了數字,但未依照提示計算倍數,僅計算了差值。

根據圖片,計算如下:

  • **Gemma 2 27B Elo 分數:**1220
  • **Gemma 3 27B Elo 分數:**1338

**增加量:**1338 - 1220 = 118

從 Gemma 2 27B 到 Gemma 3 27B,Elo 分數增加了 118 分。

Gemma 3 27B 基準測試

基準測試 Gemma 3 27B DeepSeek R1 LLaMA 3.3 70B
LMSys Elo 分數 1339 ~1360 ~1260
MMLU-Pro 67.5 84.0 66.4
LiveCodeBench 29.7 65.9 ~29
GPQA Diamond 42.4 71.5 50.5
MATH 69.0 97.3 77.0

elo scores

資料來源:Hugging Face

結合多模態模型與 Function Calling 可解決哪些問題?

問題一

資源有限時處理大型檔案

雖然 LLM 可以直接處理圖片或影片,但處理大型檔案(例如長影片或高解析度圖片)可能耗費大量資源,甚至超出模型的上下文視窗。

Function Calling 的角色: 將複雜或資源密集型任務分配給外部系統,讓 LLM 專注於協調與整合結果。

問題二

擴展特定功能的處理能力

LLM 主要專注於理解與生成內容,但在某些情境下,使用者可能需要更特定的功能(例如圖表生成、圖片編輯、影片裁切),這些超出了 LLM 的內建能力。

Function Calling 的角色: 讓 LLM 能透過外部工具生成或執行專業輸出,從而擴展其能力。

問題三

透過專業工具達成深入分析

雖然 LLM 能分析文字、圖片與影片等多模態輸入,但在執行專業級任務(如醫學影像分析或高精準度影片編輯)時深度可能不足。

Function Calling 的角色: 將任務委派給專業工具或 API,確保比單獨使用 LLM 時更高的準確性與更深入的分析。

如何透過 Novita AI 使用 Gemma 3 27B 的 Function Calling

步驟 1:取得 API 金鑰並安裝!

進入「金鑰管理」頁面,依圖示複製 API 金鑰

get api key

立即試用 Gemma 3 27B Demo!

步驟 2:使用 LangChain 實作 Function Calling

我們將建立一個簡單的數學應用程式,能執行加法與乘法運算。

💡 雖然本指南使用 LangChain 以方便示範,但實作 Function Calling 並不依賴任何特定框架。關鍵在於設計正確的提示,讓模型理解並正確呼叫函式。這裡使用 LangChain 只是為了簡化實作流程。

前置需求

首先,安裝必要的套件:

pip install langchain-openai python-dotenv

設定環境

在專案根目錄建立 .env 檔案,並加入你的 Novita AI API 金鑰:

NOVITA_API_KEY=your_api_key_here

步驟 3:實作步驟

1. 定義工具

首先,使用 LangChain 的 @tool 裝飾器建立兩個簡單的數學工具:

from langchain_core.tools import tool

@tool
def multiply(x: float, y: float) -> float:
    """Multiply two numbers together."""
    return x * y

@tool
def add(x: int, y: int) -> int:
    """Add two numbers."""
    return x + y

tools = [multiply, add]

2. 建立工具執行函式

接著,實作一個執行工具的函式:

from typing import Any, Dict, Optional, TypedDict
from langchain_core.runnables import RunnableConfig

class ToolCallRequest(TypedDict):
    name: str
    arguments: Dict[str, Any]

def invoke_tool(
    tool_call_request: ToolCallRequest, 
    config: Optional[RunnableConfig] = None
):
    """Execute the specified tool with given arguments."""
    tool_name_to_tool = {tool.name: tool for tool in tools}
    name = tool_call_request["name"]
    requested_tool = tool_name_to_tool[name]
    return requested_tool.invoke(tool_call_request["arguments"], config=config)

3. 設定 LangChain 管線

建立一個使用 Novita AI 的 LLM 來選擇與準備工具呼叫的鏈:

from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import render_text_description

def create_chain():
    """Create a chain that uses the specified LLM model to select and prepare tool calls."""
    model = ChatOpenAI(
        model="google/gemma-3-27b-it",
        api_key=os.getenv("NOVITA_API_KEY"),
        base_url="https://api.novita.ai/v3/openai",
    )
    
    rendered_tools = render_text_description(tools)
    system_prompt = f"""\
    You are an assistant that has access to the following set of tools. 
    Here are the names and descriptions for each tool:

    {rendered_tools}

    Given the user input, return the name and input of the tool to use. 
    Return your response as a JSON blob with 'name' and 'arguments' keys.

    The `arguments` should be a dictionary, with keys corresponding 
    to the argument names and the values corresponding to the requested values.
    """

    prompt = ChatPromptTemplate.from_messages(
        [("system", system_prompt), ("user", "{input}")]
    )

    return prompt | model | JsonOutputParser()

4. 建立主要處理函式

實作處理數學查詢的主要函式:

def process_math_query(query: str):
    """Process a mathematical query by using an LLM to select the appropriate tool and execute it."""
    chain = create_chain()
    message = chain.invoke({"input": query})
    result = invoke_tool(message, config=None)
    return message, result

5. 使用範例

以下是使用實作的方式:

if __name__ == "__main__":
    message, result = process_math_query(
        "meta-llama/llama-3.3-70b-instruct", 
        "what's 3 plus 1132"
    )
    print(result)  # Output: 1135

Gemma 3 27B 以數學、程式碼與指令遵循任務的業界頂尖表現,重新定義了多模態 AI。其 多模態整合 與龐大 訓練資料,使其成為開發者與研究人員的多功能工具。

常見問題

Gemma 3 27B 是什麼?

Gemma 3 27B 是一個擁有多模態能力的 AI 模型,含有 **270 億個參數 ,能處理 文字與影像 **,並支援超過 140 種語言

Gemma 3 27B 與前代相比表現如何?

Gemma 3 27B 有顯著提升,包括 Elo 分數增加 118 分,並在程式碼、數學與多模態任務上表現更佳。

Gemma 3 27B 是開源的嗎?

是的,Gemma 3 27B 是開源的,鼓勵社群驅動的創新。

Novita AI 是一個 AI 雲端平台,為開發者提供簡單 API 部署 AI 模型的便利方式,同時也提供經濟且可靠的 GPU 雲端服務,用於建置與擴展 AI 應用。

推薦閱讀