Gemma 3 27B:基于 LangChain 的函数调用实现多模态 AI

Gemma 3 27B:基于 LangChain 的函数调用实现多模态 AI

关键亮点

Gemma 3 27B 是 Google 发布的最先进的多模态 AI 模型,拥有 **270 亿参数 ** 并支持 超过 140 种语言

通过 LangChain 等工具实现的 函数调用 能够与外部系统无缝集成,扩展其在执行计算、数据分析和工作流自动化等专门任务方面的能力。

结合 Gemma 3 27B 的 多模态能力,函数调用显著增强了任务自动化,实现了文本、图像处理与外部工具之间的高级协作。开源特性鼓励了各行各业的广泛采用和创新。

Gemma 3 27B 代表了多模态 AI 的突破,在理解和生成文本与图像方面提供了无与伦比的能力。凭借 270 亿个参数和对超过 140 种语言的支持,它能够轻松处理跨行业的复杂任务。

什么是 Gemma 3 27B?

发布日期

2025 年 3 月 12 日

模型大小

270 亿参数

开源

是(由 Google 发布)

语言支持

超过 140 种语言

训练数据

14 万亿 tokens

优势

数学、编码、指令遵循

多模态能力

是(处理图像和文本,输出文本)

上下文窗口

128K tokens

Gemma 3 27B 多模态能力测试

提示: 告诉我从 gemma 2 27b 到 gemma 3 27b,elo 分数增加了多少倍?

ELO 分数

来自 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 分数

来自 Hugging Face

结合多模态模型与函数调用可以解决哪些问题?

问题 1

处理资源有限的大文件

虽然 LLM 可以直接处理图像或视频,但处理大文件(例如长视频或高分辨率图像)会消耗大量资源,并可能超出模型的上下文窗口。

函数调用的作用: 将复杂或资源密集型的任务分配给外部系统,让 LLM 专注于协调和整合结果。

问题 2

扩展特定功能的能力

LLM 主要侧重于理解和生成内容,但在某些场景中,用户可能需要更具体的功能(例如图表生成、图像编辑、视频剪辑),这些功能超出了 LLM 的内置能力。

函数调用的作用: 通过让 LLM 借助外部工具生成或执行专门的输出来扩展其能力。

问题 3

利用专业工具实现更深入的分析

虽然 LLM 可以分析文本、图像和视频等多模态输入,但在执行专业级任务(如医学图像分析或高精度视频编辑)时可能缺乏深度。

函数调用的作用: 将任务委托给专业工具或 API,从而获得比单独使用 LLM 更高的准确性和更深入的分析。

如何通过 Novita AI 使用 Llama 3.3 70B 函数调用

第一步:获取 API 密钥并安装!

进入“密钥管理”页面,您可以按照图片所示复制 API 密钥

获取 API 密钥

立即试用 Gemma 3 27B Demo!

第二步:使用 Langchain 实现函数调用

我们将创建一个可以执行加法和乘法运算的简单数学应用。

💡 虽然本指南为了方便使用了 LangChain,但实现函数调用并不需要任何特定的框架。关键在于设计正确的提示词,使模型能够理解并正确调用函数。这里使用 LangChain 仅仅是为了简化实现过程。

前提条件

首先,安装所需的包:

pip install langchain-openai python-dotenv

设置环境

在项目根目录创建一个 .env 文件,并添加您的 Novita AI API 密钥:

NOVITA_API_KEY=your_api_key_here

第三步:实现步骤

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 是一个拥有 **270 亿参数 ** 的多模态 AI 模型,能够处理 ** 文本和图像 **,并支持超过 140 种语言

Gemma 3 27B 与其前代相比如何?

Gemma 3 27B 显示出显著改进,包括 Elo 分数增加了 118 分,以及在编码、数学和多模态任务方面的性能提升。

Gemma 3 27B 是开源的吗?

是的,Gemma 3 27B 是开源的,鼓励社区驱动的创新。

Novita AI 是一个 AI 云平台,为开发者提供通过简单 API 部署 AI 模型的简便方法,同时也提供经济实惠且可靠的 GPU 云服务,用于构建和扩展规模。

推荐阅读