philschmid RSS feed 09月30日
Gemma 3 27B:强大的多模态语言模型
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

Gemma 3 27B是一款强大的开源多模态视觉语言模型,支持超过140种语言,并拥有高达128k token的上下文窗口。它在数学、推理和聊天能力方面均有显著提升,特别支持结构化输出和函数调用。该模型能够处理代理工作流,并表现出极强的指令遵循能力,为与外部工具和API的交互提供了新途径。通过精心设计的指令,Gemma 3 27B可以实现函数调用,从而将自然语言指令转化为实际操作,实现更广泛的应用场景。

🌟 **强大的多模态能力**:Gemma 3 27B不仅是一个语言模型,更是一个多模态视觉语言模型,能够理解和处理文本与图像信息,扩展了AI的应用范围。

📚 **广泛的语言支持与超长上下文**:该模型支持超过140种语言,并提供高达128k token的超长上下文窗口,使其在处理多语言和长篇内容时表现出色,能够更好地理解和生成连贯的长文本。

💡 **增强的推理与指令遵循**:Gemma 3 27B在数学计算、逻辑推理和指令遵循方面均有显著提升,使其能够更准确地理解复杂指令并执行任务,特别是在需要精确操作的场景下。

🛠️ **函数调用与代理工作流**:该模型支持函数调用,允许AI与外部工具和API进行结构化交互。这意味着Gemma 3 27B可以根据指令调用特定函数来执行实际操作,例如货币转换,从而实现更高级的代理工作流。

Google Gemma 3 27B It is an open, multilingual, multimodal Vision-Language model. It handles context windows up to 128k tokens, understands over 140 languages, and offers improved math, reasoning, and chat capabilities, including structured outputs and function calling.

Gemma 3 can be used for agentic workflows and has very strong instruction following capabilities. While there are no dedicated tool/function calling special tokens, you can prompt it to do function calling through careful instruction. Gemma 3 27B is available via AI Studio and the Gen AI API. Get your API key from AI Studio.

Function calling is the capability to connect LLMs to external tools and to interact with your code and APIs in a structured way. Instead of generating text responses, LLMs understand when to call specific functions and provide the necessary parameters to execute real-world actions.

Function Calling follows these steps:

    Your application sends a prompt to the LLM along with function definitionsThe LLM analyzes the prompt and decides whether to respond directly or use defined functionsIf using functions, the LLM generates structured arguments for the function callYour application receives the function call details and executes the actual functionThe function results are sent back to the LLMThe LLM provides a final response incorporating the function results

Raw Text Example of how function calling can be implement with Gemma 3

Below is an textual example of how to use function calling with Gemma 3 27B It. In this example the first user message includes the general instruction how and when to use function calling and example flow. The prompt with the Gemma 3 template would look like this:

    Send user message with instructions and function definitons and first user message.
<bos><start_of_turn>userAt each turn, if you decide to invoke any of the function(s), it should be wrapped with ```tool_code```. The python methods described below are imported and available, you can only use defined methods. The generated code should be readable and efficient. The response to a method will be wrapped in ```tool_output``` use it to call more tools or generate a helpful, friendly response. When using a ```tool_call``` think step by step why and how it should be used.The following Python methods are available:\`\`\`pythondef convert(amount: float, currency: str, new_currency: str) -> float:    """Convert the currency with the latest exchange rate    Args:      amount: The amount of currency to convert      currency: The currency to convert from      new_currency: The currency to convert to    """\`\`\`User: What is $200,000 in EUR?<end_of_turn><start_of_turn>model

Note: The ``` should not be escaped when you use it, but my blog cannot render ``` inside a code block. See code.

    Handle Model response when a tool/function is used.
Okay, I need to convert $200,000 to EUR. I will use the `convert` function for this.\`\`\`tool_codeconvert(amount=200000.0, currency="USD", new_currency="EUR")\`\`\`
    Execute local function and create tool output string
\`\`\`tool_output180000.0\`\`\`
    Send the tool output as new request to the model
<bos><start_of_turn>userAt each turn, if you decide to invoke any of the function(s), it should be wrapped with ```tool_code```. The python methods described below are imported and available, you can only use defined methods. The generated code should be readable and efficient. The response to a method will be wrapped in ```tool_output``` use it to call more tools or generate a helpful, friendly response. When using a ```tool_call``` think step by step why and how it should be used.The following Python methods are available:\`\`\`pythondef convert(amount: float, currency: str, new_currency: str) -> float:    """Convert the currency with the latest exchange rate    Args:      amount: The amount of currency to convert      currency: The currency to convert from      new_currency: The currency to convert to    """\`\`\`User: What is $200,000 in EUR?<end_of_turn><start_of_turn>modelOkay, I need to convert $200,000 to EUR. I will use the `convert` function for this.\`\`\`tool_codeconvert(amount=200000.0, currency="USD", new_currency="EUR")\`\`\`<end_of_turn><start_of_turn>user\`\`\`tool_output180000.0\`\`\`<end_of_turn><start_of_turn>model
    Final Response: $200,000 is approximately €180,000

Function Calling Example with Gemma 3 27B and Python.

Now, let's test this using the GenAI API. If you want to do this locally, e.g. ollama. You can just use the prompts and simulate the function execution. So first install the google-genai SDK.

%pip install google-genai

Then we create our client, define Gemma as model id and create a helper method extract_tool_call. This method parses the model responses and checks if there is a ```tool_code```. If there is one it uses the eval method to run it, extract the result and create a ```tool_output```.

Note: We use eval only for demonstration purposes if you plan to use this in production you should add more security and safety as it will execute model generated code in your environment.

import osfrom google import genaiimport re # create clientapi_key = os.getenv("GEMINI_API_KEY","xxx")client = genai.Client(api_key=api_key) # speicfy the model idmodel_id = "gemma-3-27b-it" # extract the tool call from the responsedef extract_tool_call(text):    import io    from contextlib import redirect_stdout     pattern = r"```tool_code\s*(.*?)\s*```"    match = re.search(pattern, text, re.DOTALL)    if match:        code = match.group(1).strip()        # Capture stdout in a string buffer        f = io.StringIO()        with redirect_stdout(f):            result = eval(code)        output = f.getvalue()        r = result if output == '' else output        return f'```tool_output\n{r}\n```'''    return None 

Next, we define a simple example for a function we want to use. Here is is a convert method that simulates the conversion calcuation of currencies. Since we use eval the method we want to use for function calling needs to be available in the environment.

We define our first user prompt including our instructions and function signature with a docstring and args and a template string for our user message.

def convert(amount: float, currency: str, new_currency: str) -> float:  # demo implementation  return amount * 0.9  instruction_prompt_with_function_calling = '''At each turn, if you decide to invoke any of the function(s), it should be wrapped with ```tool_code```. The python methods described below are imported and available, you can only use defined methods. The generated code should be readable and efficient. The response to a method will be wrapped in ```tool_output``` use it to call more tools or generate a helpful, friendly response. When using a ```tool_call``` think step by step why and how it should be used. The following Python methods are available: \`\`\`pythondef convert(amount: float, currency: str, new_currency: str) -> float:    """Convert the currency with the latest exchange rate     Args:      amount: The amount of currency to convert      currency: The currency to convert from      new_currency: The currency to convert to    """ def get_exchange_rate(currency: str, new_currency: str) -> float:    """Get the latest exchange rate for the currency pair     Args:      currency: The currency to convert from      new_currency: The currency to convert to    """\`\`\` User: \{user_message\}'''

Note: The ``` should not be escaped when you use it, but my blog cannot render ``` inside a code block. See code.

The genai SDK supports stateful chat session, which makes it quite easy to test our example as we can easily append the different messages. First, we start with a simple greeting to see what Gemma does.

chat = client.chats.create(model=model_id) response = chat.send_message(instruction_prompt_with_function_calling.format(user_message="hello"))print(response.text)# Hello! How can I help you today? Do you want to convert some currency, or get an exchange rate?

Nice! it greeted us back and didn't use any function call. Okay now lets ask it to convert some currency.

response = chat.send_message("What is $200,000 in EUR?")print(response.text)Okay, I need to convert $200,000 to EUR. I will use the `convert` function for this.# ```tool_code# convert(amount=200000.0, currency="USD", new_currency="EUR")# ```

Great! it generated out ```tool_code```, which we can now use and extract and to cool our method.

call_response = extract_tool_call(response.text)print(call_response)# ```tool_output# 180000.0# ```

After we have the response from our tool call we send a final message to generate a user friendly output.

response = chat.send_message(call_response)print(response.text)# $200,000 is equivalent to €180,000. Is there anything else I can help you with?

Conclusion

Function calling enables us to build powerful AI assistants that can access real-time data, perform actions, handle complex interactions, and provide natural language interfaces to APIs and tools, making it an increasingly important capability for practical AI applications that interact with the real world.

This is a simplified example on how you could implement function calling with Gemma 3. I didn't run detailed evaluation or benchmarks. Gemma 3 is an open model with strong reasoning and instruction following capabilities. This allows you to further optimize the prompt used to match your use case and data or further fine-tune on data with similar format to customize its agentic capabilities.


Thanks for reading! If you have any questions or feedback, please let me know on Twitter or LinkedIn.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Gemma 3 27B 多模态模型 语言模型 函数调用 AI Gemma 3 27B Multimodal Model Language Model Function Calling AI
相关文章