当前位置: 首页 > article >正文

llama3.1 8b instruct的function calling的template解析

最近在研究function calling,但是没有llama3.1 8b instruction调用function calling的教程,于是我顺手写了一个调用的例子,仅供大家学习参考,安装依赖:

pip install transformers
pip install langchain

调用代码示例:

import json

import torch
from langchain_core.prompts import PromptTemplate
from transformers import AutoModelForCausalLM, AutoTokenizer

checkpoint = "<your path to >/llama3.1-8b-instruct"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
sys_message="""Cutting Knowledge Date: December 2023
Today Date: 23 July 2024

When you receive a tool call response, use the output to format an answer to the orginal user question.

You are a helpful assistant with tool calling capabilities.
"""

user_message="""
Given the following functions, please respond with a JSON for a function call with its proper arguments that best answers the given prompt.

Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}. Do not use variables.

{{functions}}

Question: {{query}}
"""
prompt_template= PromptTemplate(template=user_message, input_variables=["functions", "query"], template_format="jinja2")
query = "what is the weather like in San Fransisco?"
functions = [{
    "type": "function",
    "function": {
    "name": "get_current_conditions",
    "description": "Get the current weather conditions for a specific location",
    "parameters": {
        "type": "object",
        "properties": {
        "location": {
            "type": "string",
            "description": "The city and state, e.g., San Francisco, CA"
        },
        "unit": {
            "type": "string",
            "enum": ["Celsius", "Fahrenheit"],
            "description": "The temperature unit to use. Infer this from the user's location."
        }
        },
        "required": ["location", "unit"]
    }
    }
}]
messages = [
    {"role": "system", "content": sys_message},
    {"role":"user", "content": prompt_template.format(query=query, functions=functions)}]
# inputs = tokenizer.apply_chat_template(messages, tokenize=False)
inputs = tokenizer.apply_chat_template(messages, return_dict=True, return_tensors="pt")
print(inputs)
model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto")
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]):]))

参考文献

[1]. JSON based tool calling
[2].开源模型 Function Call 方案梳理


http://www.kler.cn/a/324980.html

相关文章:

  • 【Redis】使用redis实现登录校验功能
  • 5.4.2-1 编写Java程序在HDFS上创建文件
  • FastAPI 中间件详解:实现高性能 Web 应用的完整指南和实际案例
  • 4A架构之间的关系和集成
  • 系统思考—跳出症状看全局
  • RK3568平台开发系列讲解(platform虚拟总线驱动篇)实验:点亮一个LED
  • C++第五讲(1):STL--string--各个函数的使用方法
  • 在线支付系统
  • 无人机之模拟图传篇
  • 交互式低延迟音频解码器
  • QT开发:基于Qt实现的交通信号灯模拟器:实现一个带有倒计时功能的图形界面应用
  • 计算机毕业设计之:宠物互助平台的微信小程序系统(源码+文档+讲解)
  • Java | Leetcode Java题解之第429题N叉树的层序遍历
  • Go 语言框架接入阿里云的报警通知与日志实时追踪与监控
  • 从0学习React(2)
  • 海云安董事长谢朝海博士出席2024年中国国际服务贸易交易会“大模型应用创新论坛”
  • Rust调用tree-sitter支持自定义语言解析
  • JavaScript中的输出方式
  • Android页面跳转与返回机制详解
  • 用友畅捷通-TPlus FileUploadHandler.ashx 任意文件上传
  • [报错解决] 运行MATCHA时需要在线下载Arial.TTF字体,但是无法连接huggingface
  • 阿里云AlibabaCloudLinux php 安装 mysqli 扩展
  • 基于Dockerfile打包算法镜像
  • Prometheus+Grafana+elasticsearch_exporter监控elasticsearch的简单配置过程
  • fmql之Linux阻塞和非阻塞IO
  • 性能调优知识点(mysql)三