[Python] llama3 responding only function call?

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make Llama3 Instruct able to use function call from tools , it does work but now it is answering only function call! if I ask something like who are you ? or what is apple device ? it answer back function call , I believe it is something in chat template ? or still something is missing in my code ?

    from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
    import os
    import torch
    from huggingface_hub import login

    def get_current_temperature(location: str, unit: str) -> float:
    """
    Get the current temperature at a location.

    Args:
    location: The location to get the temperature for, in the format "City, Country"
    unit: The unit to return the temperature in. (choices: ["celsius", "fahrenheit"])
    Returns:
    The current temperature at the specified location in the specified units, as a float.
    """
    return 22. # A real function should probably actually get the temperature!

    def get_current_wind_speed(location: str) -> float:
    """
    Get the current wind speed in km/h at a given location.

    Args:
    location: The location to get the temperature for, in the format "City, Country"
    Returns:
    The current wind speed at the given location in km/h, as a float.
    """
    return 6. # A real function should probably actually get the wind speed!

    tools = [get_current_temperature, get_current_wind_speed]


    # Suppress MPS log message (optional)
    os.environ["TORCH_MPS_DEVICE"] = "1"

    checkpoint = "models/Llama-3.2-1B-Instruct"

    messages = [
    {"role": "user", "content": "Hey, who are you ?"}
    ]

    tokenizer = AutoTokenizer.from_pretrained(checkpoint)
    model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="cpu")

    inputs = tokenizer.apply_chat_template(messages, tools=tools, add_generation_prompt=True, return_dict=True, return_tensors="pt")
    inputs = {k: v.to(model.device) for k, v in inputs.items()}
    out = model.generate(**inputs, max_new_tokens=128)
    print(tokenizer.decode(out[0][len(inputs["input_ids"][0]):],skip_special_tokens=True))

    Continue reading...

Compartilhe esta Página