Views
No views yet
llama codebase.transformers >= 4.43.0 onward, you can run conversational inference using the Transformers pipeline abstraction or by leveraging the Auto classes with the generate() function.pip install --upgrade transformers.1import transformers
2import torch
3model_id = "EpistemeAI/Fireball-Llama-3.1-8B-Intruct-v1dpo-16bit"
4pipeline = transformers.pipeline(
5 "text-generation",
6 model=model_id,
7 model_kwargs={"torch_dtype": torch.bfloat16},
8 device_map="auto",
9)
10messages = [
11 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
12 {"role": "user", "content": "Who are you?"},
13]
14outputs = pipeline(
15 messages,
16 max_new_tokens=256,
17)
18print(outputs[0]["generated_text"][-1])torch.compile(), assisted generations, quantised and more at huggingface-llama-recipes1# First, define a tool
2def get_current_temperature(location: str) -> float:
3 """
4 Get the current temperature at a location.
5
6 Args:
7 location: The location to get the temperature for, in the format "City, Country"
8 Returns:
9 The current temperature at the specified location in the specified units, as a float.
10 """
11 return 22. # A real function should probably actually get the temperature!
12# Next, create a chat and apply the chat template
13messages = [
14 {"role": "system", "content": "You are a bot that responds to weather queries."},
15 {"role": "user", "content": "Hey, what's the temperature in Paris right now?"}
16]
17inputs = tokenizer.apply_chat_template(messages, tools=[get_current_temperature], add_generation_prompt=True)1tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France"}}
2messages.append({"role": "assistant", "tool_calls": [{"type": "function", "function": tool_call}]})tool role, like so:messages.append({"role": "tool", "name": "get_current_temperature", "content": "22.0"})generate() again to let the model use the tool result in the chat. Note that this was a very brief introduction to tool calling - for more information,
see the LLaMA prompt format docs and the Transformers tool use documentation.