(English below)

Model Card cho ricepaper/vi-gemma-2-2b-function-calling

Mรด tแบฃ Mรด hรฌnh

ricepaper/vi-gemma-2-2b-function-calling lร  mรด hรฌnh ngรดn ngแปฏ lแป›n ฤ‘ฦฐแปฃc tinh chแป‰nh tแปซ google/gemma-2-2b-it cho khแบฃ nฤƒng hiแปƒu vร  thแปฑc thi single/multi function call (gแปi hร m) tแป‘i ฦฐu cho 2 ngรดn ngแปฏ chรญnh: tiแบฟng Viแป‡t vร  tiแบฟng Anh. Mรด hรฌnh ฤ‘ฦฐแปฃc huแบฅn luyแป‡n vแป›i tแบญp dแปฏ liแป‡u phong phรบ bao gแป“m cรกc ฤ‘oแบกn hแป™i thoแบกi chแปฉa function call theo ฤ‘แป‹nh dแบกng ChatML, kแบฟt hแปฃp vแป›i tแบญp dแปฏ liแป‡u ฤ‘a ngรดn ngแปฏ ฤ‘ฦฐแปฃc dแป‹ch sang tiแบฟng Viแป‡t.

Mแปฅc ฤ‘รญch Sแปญ dแปฅng

Mรด hรฌnh nร y phรน hแปฃp cho cรกc แปฉng dแปฅng yรชu cแบงu:

  • Xรขy dแปฑng chatbot cรณ khแบฃ nฤƒng tฦฐฦกng tรกc vแป›i ngฦฐแปi dรนng vร  thแปฑc thi cรกc tรกc vแปฅ cแปฅ thแปƒ thรดng qua function call.
  • Tแบกo cรกc hแป‡ thแป‘ng hแปi ฤ‘รกp tแปฑ ฤ‘แป™ng cรณ khแบฃ nฤƒng truy xuแบฅt thรดng tin tแปซ cรกc nguแป“n dแปฏ liแป‡u khรกc nhau.
  • Phรกt triแปƒn cรกc แปฉng dแปฅng xแปญ lรฝ ngรดn ngแปฏ tแปฑ nhiรชn nรขng cao nhฦฐ tรณm tแบฏt vฤƒn bแบฃn, dแป‹ch mรกy, tแบกo vฤƒn bแบฃn.
  • Xรขy dแปฑng agent: Tแบกo cรกc agent thรดng minh cรณ khแบฃ nฤƒng tฦฐฦกng tรกc vแป›i mรดi trฦฐแปng vร  thแปฑc hiแป‡n cรกc hร nh ฤ‘แป™ng dแปฑa trรชn ngรดn ngแปฏ.
  • Hแป‡ thแป‘ng multi-agent: Phรกt triแปƒn cรกc hแป‡ thแป‘ng ฤ‘a tรกc tแปญ, trong ฤ‘รณ cรกc agent cรณ thแปƒ giao tiแบฟp vร  hแปฃp tรกc vแป›i nhau ฤ‘แปƒ giแบฃi quyแบฟt cรกc vแบฅn ฤ‘แป phแปฉc tแบกp.

Cรกch sแปญ dแปฅng

1. Cร i ฤ‘แบทt cรกc thฦฐ viแป‡n cแบงn thiแบฟt:

! pip install transformers torch

2. KhแปŸi tแบกo tokenizer vร  model:

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json

# KhแปŸi tแบกo tokenizer vร  model
tokenizer = AutoTokenizer.from_pretrained("ricepaper/vi-gemma-2-2b-function-calling")
model = AutoModelForCausalLM.from_pretrained(
    "ricepaper/vi-gemma-2-2b-function-calling",
    device_map="auto",
    torch_dtype=torch.float16,
)

3. Xรขy dแปฑng hร m xแปญ lรฝ user query:

def process_user_query(user_query, messages, available_tools):
    """
    Xแปญ lรฝ user query, tแบกo response, kiแปƒm tra vร  thแปฑc thi function call (nแบฟu cรณ).

    Args:
        user_query (str): Query tแปซ ngฦฐแปi dรนng.
        messages (list): List messages hiแป‡n tแบกi trong conversation.
        available_tools (dict): Dictionary chแปฉa cรกc function cรณ sแบตn.

    Returns:
        str: Response cuแป‘i cรนng sau khi xแปญ lรฝ function call (nแบฟu cรณ).
    """

    # Thรชm user query vร o messages
    messages.append({"role": "user", "content": user_query})

    # Tแบกo response tแปซ model
    input_ids = tokenizer.apply_chat_template(
        messages,
        add_generation_prompt=True,
        return_tensors="pt"
    ).to(model.device)
    outputs = model.generate(
        input_ids,
        max_new_tokens=300,
        # ... (Cรกc tham sแป‘ generate khรกc)
    )
    response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)

    try:
        # Chuyแปƒn ฤ‘แป•i chuแป—i JSON thร nh list Python
        response_list = json.loads(response)
        # Thรชm response vร o messages nแบฟu cรณ functioncall
        messages.append({"role": "assistant", "content": response})
    except json.JSONDecodeError:
        # Nแบฟu response khรดng phแบฃi JSON, coi nhฦฐ khรดng cรณ function call
        response_list = []

    # KhแปŸi tแบกo list function_responses ฤ‘แปƒ lฦฐu kแบฟt quแบฃ
    function_responses = []

    # Duyแป‡t qua tแปซng phแบงn tแปญ trong list
    for response_dict in response_list:
        if "name" in response_dict and "arguments" in response_dict:
            function_name = response_dict.get("name")
            function_args = response_dict.get("arguments")

            if function_name in available_tools:
                # Thแปฑc hiแป‡n function call
                print(f"Calling function {function_name} with arguments {function_args}\n")
                function_to_call = available_tools[function_name]
                function_response = function_to_call(**function_args)
                
                # Lฦฐu kแบฟt quแบฃ dฦฐแป›i dแบกng dictionary
                function_responses.append({
                    "name": function_name,
                    "response": function_response
                })
            else:
                print(f"Function {function_name} not found")

    # Thรชm list function_responses vร o messages
    if function_responses:
        messages.append({
            "role": "user",
            "content": f"FUNCTION RESPONSES:\n{json.dumps(function_responses, ensure_ascii=False)}"
        })
        print(messages[-1].get("content"))

        # Tแบกo response mแป›i sau khi xแปญ lรฝ function call
        input_ids = tokenizer.apply_chat_template(
            messages,
            add_generation_prompt=True,
            return_tensors="pt"
        ).to(model.device)
        outputs = model.generate(
            input_ids,
            max_new_tokens=300,
            # ... (Cรกc tham sแป‘ generate khรกc)
        )
        response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)

    return response

4. Tแบกo cรกc hร m hแป— trแปฃ vร  khai bรกo danh sรกch tools:

## Hร m mรด phแปng hแป— trแปฃ tรญnh boa cho mแป™t hรณa ฤ‘ฦกn
def calculate_tip(bill_amount: float, tip_percentage: float) -> str:
  """Tรญnh sแป‘ tiแปn boa cho mแป™t hรณa ฤ‘ฦกn vร  trแบฃ vแป mแป™t chuแป—i mรด tแบฃ kแบฟt quแบฃ.

  Args:
    bill_amount: Tแป•ng sแป‘ tiแปn cแปงa hรณa ฤ‘ฦกn.
    tip_percentage: Tแปท lแป‡ tiแปn boa.

  Returns:
    Mแป™t chuแป—i mรด tแบฃ sแป‘ tiแปn boa vร  tแป•ng sแป‘ tiแปn phแบฃi trแบฃ.
  """

  tip_amount = bill_amount * (tip_percentage / 100)
  total_amount = bill_amount + tip_amount
  return f"Sแป‘ tiแปn boa lร : {tip_amount:.2f}\nTแป•ng sแป‘ tiแปn phแบฃi trแบฃ lร : {total_amount:.2f}"

# Khai bรกo danh sรกch tools
tools = """
{
  "name": "calculate_tip",
  "description": "Tรญnh sแป‘ tiแปn boa cho mแป™t hรณa ฤ‘ฦกn",
  "parameters": {
    "type": "object",
    "properties": {
      "bill_amount": {
        "type": "number",
        "description": "Tแป•ng sแป‘ tiแปn cแปงa hรณa ฤ‘ฦกn"
      },
      "tip_percentage": {
        "type": "number",
        "description": "Tแปท lแป‡ tiแปn boa"
      }
    },
    "required": [
      "bill_amount",
      "tip_percentage"
    ]
  }
},
"""

# Tแบกo dictionary รกnh xแบก tรชn hร m vแป›i hร m tฦฐฦกng แปฉng
available_tools = {
    "calculate_tip": calculate_tip,
}

5. Tแบกo lแป‹ch sแปญ trรฒ chuyแป‡n vร  sแปญ dแปฅng:

# Tแบกo lแป‹ch sแปญ trรฒ chuyแป‡n mแป›i
messages = [
    {"role": "user", "content": f"""Bแบกn lร  mแป™t trแปฃ lรฝ hแปฏu รญch vแป›i quyแปn truy cแบญp vร o cรกc chแปฉc nฤƒng sau. Sแปญ dแปฅng chรบng nแบฟu cแบงn thiแบฟt {tools}"""},
    {"role": "assistant", "content": "Xin chร o, tรดi cรณ thแปƒ giรบp gรฌ cho bแบกn?"},
]
# Sแปญ dแปฅng
res = process_user_query("Tรดi cแบงn trแปฃ giรบp tรญnh tiแปn boa cho hรณa ฤ‘ฦกn cแปงa mรฌnh. Tแป•ng sแป‘ tiแปn lร  50 USD vร  tรดi muแป‘n ฤ‘แปƒ lแบกi 15% tiแปn boa?", messages, available_tools)
messages.append({"role": "assistant", "content": res})
print("\n"+res)
# Calling function calculate_tip with arguments {'bill_amount': 50, 'tip_percentage': 15}

# FUNCTION RESPONSES:
# [{"name": "calculate_tip", "response": "Sแป‘ tiแปn boa lร : 7.50\nTแป•ng sแป‘ tiแปn phแบฃi trแบฃ lร : 57.50"}]

# Sแป‘ tiแปn boa cho hรณa ฤ‘ฦกn cแปงa bแบกn lร  7,50 USD. Tแป•ng sแป‘ tiแปn phแบฃi trแบฃ lร  57,50 USD.

messages
# [{'role': 'user',
#   'content': 'Bแบกn lร  mแป™t trแปฃ lรฝ hแปฏu รญch vแป›i quyแปn truy cแบญp vร o cรกc chแปฉc nฤƒng sau. Sแปญ dแปฅng chรบng nแบฟu cแบงn thiแบฟt \n{\n  "name": "calculate_tip",\n  "description": "Tรญnh sแป‘ tiแปn boa cho mแป™t hรณa ฤ‘ฦกn",\n  "parameters": {\n    "type": "object",\n    "properties": {\n      "bill_amount": {\n        "type": "number",\n        "description": "Tแป•ng sแป‘ tiแปn cแปงa hรณa ฤ‘ฦกn"\n      },\n      "tip_percentage": {\n        "type": "number",\n        "description": "Tแปท lแป‡ tiแปn boa"\n      }\n    },\n    "required": [\n      "bill_amount",\n      "tip_percentage"\n    ]\n  }\n},\n'},
#  {'role': 'assistant', 'content': 'Xin chร o, tรดi cรณ thแปƒ giรบp gรฌ cho bแบกn?'},
#  {'role': 'user',
#   'content': 'Tรดi cแบงn trแปฃ giรบp tรญnh tiแปn boa cho hรณa ฤ‘ฦกn cแปงa mรฌnh. Tแป•ng sแป‘ tiแปn lร  50 USD vร  tรดi muแป‘n ฤ‘แปƒ lแบกi 15% tiแปn boa?'},
#  {'role': 'assistant',
#   'content': '[{"name": "calculate_tip", "arguments": {"bill_amount": 50, "tip_percentage": 15}}]'},
#  {'role': 'user',
#   'content': 'FUNCTION RESPONSES:\n[{"name": "calculate_tip", "response": "Sแป‘ tiแปn boa lร : 7.50\\nTแป•ng sแป‘ tiแปn phแบฃi trแบฃ lร : 57.50"}]'},
#  {'role': 'assistant',
#   'content': 'Sแป‘ tiแปn boa cho hรณa ฤ‘ฦกn cแปงa bแบกn lร  7,50 USD. Tแป•ng sแป‘ tiแปn phแบฃi trแบฃ lร  57,50 USD.'}]

Lฦฐu รฝ

  • Mรด hรฌnh cรณ thแปƒ yรชu cแบงu scale chแบฅt lฦฐแปฃng vร  cแบฅu hรฌnh phแบงn cแปฉng phรน hแปฃp ฤ‘แปƒ hoแบกt ฤ‘แป™ng hiแป‡u quแบฃ.
  • Kแบฟt quแบฃ cแปงa function call phแปฅ thuแป™c vร o chแบฅt lฦฐแปฃng cแปงa hร m hแป— trแปฃ ฤ‘ฦฐแปฃc cung cแบฅp.
  • Ngฦฐแปi dรนng cรณ thแปƒ thay ฤ‘แป•i cรกc tham sแป‘ generate cแปงa mรด hรฌnh ฤ‘แปƒ ฤ‘iแปu chแป‰nh ฤ‘แป™ dร i vร  nแป™i dung cแปงa response.

English model card version:

Model Card for ricepaper/vi-gemma-2-2b-function-calling

Model Description

ricepaper/vi-gemma-2-2b-function-calling is a large language model fine-tuned from google/gemma-2-2b-it for understanding and executing single/multi function calls, optimized for 2 main languages: Vietnamese and English. The model is trained on a rich dataset of conversations containing function calls in ChatML format, combined with multilingual data translated into Vietnamese.

Intended Uses

This model is suitable for applications requiring:

  • Building chatbots that can interact with users and perform specific tasks through function calls.
  • Creating automated question answering systems capable of retrieving information from various data sources.
  • Developing advanced natural language processing applications such as text summarization, machine translation, and text generation.
  • Building agents: Creating intelligent agents capable of interacting with the environment and performing actions based on language.
  • Multi-agent systems: Developing multi-agent systems where agents can communicate and collaborate to solve complex problems.

How to Use

1. Install necessary libraries:

! pip install transformers torch

2. Initialize the tokenizer and model:

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json

# Initialize the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("ricepaper/vi-gemma-2-2b-function-calling")
model = AutoModelForCausalLM.from_pretrained(
    "ricepaper/vi-gemma-2-2b-function-calling",
    device_map="auto",
    torch_dtype=torch.float16,
)

3. Build a function to process user queries:

def process_user_query(user_query, messages, available_tools):
    """
    Processes user queries, generates responses, checks for, and executes function calls (if any).

    Args:
        user_query (str): The query from the user.
        messages (list): The list of current messages in the conversation.
        available_tools (dict): A dictionary containing available functions.

    Returns:
        str: The final response after processing function calls (if any).
    """

    # Add the user query to the messages
    messages.append({"role": "user", "content": user_query})

    # Generate a response from the model
    input_ids = tokenizer.apply_chat_template(
        messages,
        add_generation_prompt=True,
        return_tensors="pt"
    ).to(model.device)
    outputs = model.generate(
        input_ids,
        max_new_tokens=300,
        # ... (Other generate parameters)
    )
    response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)

    try:
        # Convert the JSON string to a Python list
        response_list = json.loads(response)
        # Add the response to messages if there's a function call
        messages.append({"role": "assistant", "content": response})
    except json.JSONDecodeError:
        # If the response is not JSON, assume no function call
        response_list = []

    # Initialize a list to store function responses
    function_responses = []

    # Iterate through each element in the list
    for response_dict in response_list:
        if "name" in response_dict and "arguments" in response_dict:
            function_name = response_dict.get("name")
            function_args = response_dict.get("arguments")

            if function_name in available_tools:
                # Execute the function call
                print(f"Calling function {function_name} with arguments {function_args}\n")
                function_to_call = available_tools[function_name]
                function_response = function_to_call(**function_args)
                
                # Store the result as a dictionary
                function_responses.append({
                    "name": function_name,
                    "response": function_response
                })
            else:
                print(f"Function {function_name} not found")

    # Add the list of function responses to the messages
    if function_responses:
        messages.append({
            "role": "user",
            "content": f"FUNCTION RESPONSES:\n{json.dumps(function_responses, ensure_ascii=False)}"
        })
        print(messages[-1].get("content"))

        # Generate a new response after processing function calls
        input_ids = tokenizer.apply_chat_template(
            messages,
            add_generation_prompt=True,
            return_tensors="pt"
        ).to(model.device)
        outputs = model.generate(
            input_ids,
            max_new_tokens=300,
            # ... (Other generate parameters)
        )
        response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)

    return response

4. Create helper functions and declare the tools list:

## Function simulating tip calculation for a bill
def calculate_tip(bill_amount: float, tip_percentage: float) -> str:
  """Calculates the tip amount for a bill and returns a string describing the result.

  Args:
    bill_amount: The total amount of the bill.
    tip_percentage: The tip percentage.

  Returns:
    A string describing the tip amount and the total amount to be paid.
  """

  tip_amount = bill_amount * (tip_percentage / 100)
  total_amount = bill_amount + tip_amount
  return f"The tip amount is: {tip_amount:.2f}\nThe total amount to be paid is: {total_amount:.2f}"

# Declare the tools list
tools = """
{
  "name": "calculate_tip",
  "description": "Calculate the tip amount for a bill",
  "parameters": {
    "type": "object",
    "properties": {
      "bill_amount": {
        "type": "number",
        "description": "The total bill amount"
      },
      "tip_percentage": {
        "type": "number",
        "description": "The tip percentage"
      }
    },
    "required": [
      "bill_amount",
      "tip_percentage"
    ]
  }
},
"""

# Create a dictionary mapping function names to their corresponding functions
available_tools = {
    "calculate_tip": calculate_tip,
}

5. Create a new conversation history and use the model:

# Create a new conversation history
messages = [
    {"role": "user", "content": f"""You are a helpful assistant with access to the following functions. Use them if necessary {tools}"""},
    {"role": "assistant", "content": "Hello, how can I assist you?"},
]
# Use the model
res = process_user_query("I need help calculating the tip for my bill. The total is $50 and I would like to leave a 15% tip.", messages, available_tools)
messages.append({"role": "assistant", "content": res})
print("\n"+res)
# Calling function calculate_tip with arguments {'bill_amount': 50, 'tip_percentage': 15}

# FUNCTION RESPONSES:
# [{"name": "calculate_tip", "response": "The tip amount is: 7.50\nThe total amount to be paid is: 57.50"}]

# The tip amount for your bill is $7.50. The total amount to be paid is $57.50.

messages
# [{'role': 'user',
#   'content': 'You are a helpful assistant with access to the following functions. Use them if necessary \n{\n  "name": "calculate_tip",\n  "description": "Calculate the tip amount for a bill",\n  "parameters": {\n    "type": "object",\n    "properties": {\n      "bill_amount": {\n        "type": "number",\n        "description": "The total bill amount"\n      },\n      "tip_percentage": {\n        "type": "number",\n        "description": "The tip percentage"\n      }\n    },\n    "required": [\n      "bill_amount",\n      "tip_percentage"\n    ]\n  }\n},\n'},
#  {'role': 'assistant', 'content': 'Hello, how can I assist you?'},
#  {'role': 'user',
#   'content': 'I need help calculating the tip for my bill. The total is $50 and I would like to leave a 15% tip.'},
#  {'role': 'assistant',
#   'content': '[{"name": "calculate_tip", "arguments": {"bill_amount": 50, "tip_percentage": 15}}]'},
#  {'role': 'user',
#   'content': 'FUNCTION RESPONSES:\n[{"name": "calculate_tip", "response": "The tip amount is: 7.50\\nThe total amount to be paid is: 57.50"}]'},
#  {'role': 'assistant',
#   'content': 'The tip amount for your bill is $7.50. The total amount to be paid is $57.50.'}]

Uploaded model

This gemma model was trained 2x faster with Unsloth and Huggingface's TRL library.

Downloads last month
7
Safetensors
Model size
2.61B params
Tensor type
FP16
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for ricepaper/vi-gemma-2-2b-function-calling

Finetuned
(131)
this model

Collection including ricepaper/vi-gemma-2-2b-function-calling