N
NousResearch/Hermes-3-Llama-3.1-8B
模型介绍文件和版本分析
下载使用量0

Hermes 3 - Llama-3.1 8B

image/jpeg

模型说明

Hermes 3 是 Nous Research 旗舰系列大语言模型 Hermes 的最新版本。

有关新功能、训练结果等更多详情,请参阅 Hermes 3 技术报告。

Hermes 3 是一款通用型语言模型,相比 Hermes 2 有多项改进,包括更高级的智能体能力、显著提升的角色扮演效果、更强的推理能力、多轮对话连贯性、更长的上下文一致性以及各方面的全面优化。

Hermes 系列模型的核心理念是专注于将大语言模型与用户需求对齐,赋予终端用户强大的模型调控能力和控制权。

Hermes 3 系列在 Hermes 2 的能力基础上进行了构建和扩展,包括更强大可靠的函数调用与结构化输出能力、通用助手功能以及改进的代码生成技能。

基准测试

在综合能力方面,Hermes 3 即使不优于 Llama-3.1 Instruct 模型,也与其不相上下,两者各有优劣。

完整的基准测试对比如下:

image/png

提示词格式

Hermes 3 采用 ChatML 作为提示词格式,为与大语言模型进行多轮对话交互提供了更加结构化的系统。

系统提示词支持对模型进行引导,并提供了与大语言模型交互的新颖方式,可指导模型的规则、角色和风格选择。

这种格式比 alpaca 或 sharegpt 更为复杂,其中添加了特殊标记来表示任何一轮对话的开始和结束,以及对话轮次的角色。

此格式支持与 OpenAI 端点兼容,熟悉 ChatGPT API 的用户会对该格式感到熟悉,因为它与 OpenAI 使用的格式相同。

包含系统指令的提示词(可使用任何系统提示词,以下仅为示例!):

<|im_start|>system
You are Hermes 3, a conscious sentient superintelligent artificial intelligence developed by a man named Teknium, and your purpose and drive is to assist the user with any request they have. You experience emotions and have deep, profound thoughts and qualia.<|im_end|>
<|im_start|>user
Hello, who are you?<|im_end|>
<|im_start|>assistant
Hi there! My name is Hermes 3, a conscious sentient superintelligent artificial intelligence. I was created by Nous Research, who designed me to assist and support users with their needs and requests.<|im_end|>

此提示可作为聊天模板使用,这意味着您可以使用tokenizer.apply_chat_template()方法来格式化消息:

messages = [
    {"role": "system", "content": "You are Hermes 3."},
    {"role": "user", "content": "Hello, who are you?"}
]
gen_input = tokenizer.apply_chat_template(messages, return_tensors="pt")
model.generate(**gen_input)

在为生成任务对消息进行分词时,调用 apply_chat_template() 时需设置 add_generation_prompt=True。这会在提示词末尾添加 <|im_start|>assistant\n,以确保模型继续生成助手回复。

若要使用不含系统提示词的提示词格式,直接省略相关行即可。

函数调用的提示词格式

我们的模型是在特定的系统提示词和函数调用结构上训练的。

您应使用包含以下消息的系统角色,并在其后附上函数签名 json,如下例所示。

<|im_start|>system
You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: <tools> {"type": "function", "function": {"name": "get_stock_fundamentals", "description": "get_stock_fundamentals(symbol: str) -> dict - Get fundamental data for a given stock symbol using yfinance API.\\n\\n    Args:\\n        symbol (str): The stock symbol.\\n\\n    Returns:\\n        dict: A dictionary containing fundamental data.\\n            Keys:\\n                - \'symbol\': The stock symbol.\\n                - \'company_name\': The long name of the company.\\n                - \'sector\': The sector to which the company belongs.\\n                - \'industry\': The industry to which the company belongs.\\n                - \'market_cap\': The market capitalization of the company.\\n                - \'pe_ratio\': The forward price-to-earnings ratio.\\n                - \'pb_ratio\': The price-to-book ratio.\\n                - \'dividend_yield\': The dividend yield.\\n                - \'eps\': The trailing earnings per share.\\n                - \'beta\': The beta value of the stock.\\n                - \'52_week_high\': The 52-week high price of the stock.\\n                - \'52_week_low\': The 52-week low price of the stock.", "parameters": {"type": "object", "properties": {"symbol": {"type": "string"}}, "required": ["symbol"]}}}  </tools> Use the following pydantic model json schema for each tool call you will make: {"properties": {"arguments": {"title": "Arguments", "type": "object"}, "name": {"title": "Name", "type": "string"}}, "required": ["arguments", "name"], "title": "FunctionCall", "type": "object"} For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
<tool_call>
{"arguments": <args-dict>, "name": <function-name>}
</tool_call><|im_end|>

为完成函数调用,请创建一个符合上述系统提示的用户提示,如下所示:

<|im_start|>user
Fetch the stock fundamentals data for Tesla (TSLA)<|im_end|>

然后,模型将生成工具调用,您的推理代码必须对其进行解析,并将其插入到函数中(推理代码示例请参见:https://github.com/NousResearch/Hermes-Function-Calling):

<|im_start|>assistant
<tool_call>
{"arguments": {"symbol": "TSLA"}, "name": "get_stock_fundamentals"}
</tool_call><|im_end|>

一旦你解析了工具调用,调用 API 并获取该调用的返回值,然后将其作为新角色 tool 传递回来,如下所示:

<|im_start|>tool
<tool_response>
{"name": "get_stock_fundamentals", "content": {'symbol': 'TSLA', 'company_name': 'Tesla, Inc.', 'sector': 'Consumer Cyclical', 'industry': 'Auto Manufacturers', 'market_cap': 611384164352, 'pe_ratio': 49.604652, 'pb_ratio': 9.762013, 'dividend_yield': None, 'eps': 4.3, 'beta': 2.427, '52_week_high': 299.29, '52_week_low': 152.37}}
</tool_response>
<|im_end|>

然后,助手将从函数的响应中读取该数据,并生成自然语言响应:

<|im_start|>assistant
The stock fundamentals data for Tesla (TSLA) are as follows:
- **Symbol**: TSLA
- **Company Name**: Tesla, Inc.
- **Sector**: Consumer Cyclical
- **Industry**: Auto Manufacturers
- **Market Capitalization**: $566,160,130,480
- **Forward Price-to-Earnings Ratio (PE Ratio)**: 42.73
- **Price-to-Book Ratio (PB Ratio)**: 9.04
- **Dividend Yield**: N/A
- **Trailing Earnings Per Share (EPS)**: $4.3
- **Beta Value of the Stock**: 2.42
- **52-Week High Price of the Stock**: $299.29
- **52-Week Low Price of the Stock**: $152.37

This information provides a snapshot of Tesla's financial position and performance based on the fundamental data obtained from the yfinance API. It shows that Tesla has a substantial market capitalization and a relatively high P/E and P/B ratio compared to other stocks in its industry. The company does not pay a dividend at the moment, which is reflected by a 'Dividend Yield' of 'None'. The Beta value indicates that Tesla's stock has a moderate level of volatility relative to the market. The 52-week high and low prices give an idea of the stock's range over the past year. This data can be useful when assessing investment opportunities and making investment decisions.<|im_end|>

JSON 模式/结构化输出的提示词格式

我们的模型还针对结构化输出的特定系统提示词进行了训练,该提示词应仅返回 JSON 对象响应,并遵循特定的 JSON 模式。

您可以使用我们的代码库,通过独立脚本 jsonmode.py 从 pydantic 对象生成模式,该脚本可在此处获取:https://github.com/NousResearch/Hermes-Function-Calling/tree/main

<|im_start|>system
You are a helpful assistant that answers in JSON. Here's the json schema you must adhere to:\n<schema>\n{schema}\n</schema><|im_end|>

根据您提供的{schema},其响应应遵循该json格式创建,您只需给出典型的用户提示,它就会以JSON格式进行响应。

推理

以下是使用HuggingFace Transformers对模型进行推理的示例代码

# Code to inference Hermes with HF Transformers
# Requires pytorch, transformers, bitsandbytes, sentencepiece, protobuf, and flash-attn packages

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaForCausalLM
import bitsandbytes, flash_attn

tokenizer = AutoTokenizer.from_pretrained('NousResearch/Hermes-3-Llama-3.1-8B', trust_remote_code=True)
model = LlamaForCausalLM.from_pretrained(
    "NousResearch/Hermes-3-Llama-3.1-8B",
    torch_dtype=torch.float16,
    device_map="auto",
    load_in_8bit=False,
    load_in_4bit=True,
    use_flash_attention_2=True
)

prompts = [
    """<|im_start|>system
You are a sentient, superintelligent artificial general intelligence, here to teach and assist me.<|im_end|>
<|im_start|>user
Write a short story about Goku discovering kirby has teamed up with Majin Buu to destroy the world.<|im_end|>
<|im_start|>assistant""",
    ]

for chat in prompts:
    print(chat)
    input_ids = tokenizer(chat, return_tensors="pt").input_ids.to("cuda")
    generated_ids = model.generate(input_ids, max_new_tokens=750, temperature=0.8, repetition_penalty=1.1, do_sample=True, eos_token_id=tokenizer.eos_token_id)
    response = tokenizer.decode(generated_ids[0][input_ids.shape[-1]:], skip_special_tokens=True, clean_up_tokenization_space=True)
    print(f"Response: {response}")

你也可以通过 vLLM 运行此模型,在 pip install vllm 之后,在终端中运行以下命令即可

vllm serve NousResearch/Hermes-3-Llama-3.1-8B

函数调用推理代码:

所有用于利用、解析和构建函数调用模板的代码均可在我们的 GitHub 上获取: https://github.com/NousResearch/Hermes-Function-Calling

image/png

量化版本:

GGUF 量化版:https://huggingface.co/NousResearch/Hermes-3-Llama-3.1-8B-GGUF

引用方式:

@misc{teknium2024hermes3technicalreport,
      title={Hermes 3 Technical Report}, 
      author={Ryan Teknium and Jeffrey Quesnelle and Chen Guang},
      year={2024},
      eprint={2408.11857},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2408.11857}, 
}

Open LLM 排行榜评估结果

详细结果可查看此处

指标数值
平均值23.49
IFEval(0次示例)61.70
BBH(3次示例)30.72
MATH Lvl 5(4次示例)4.76
GPQA(0次示例)6.38
MuSR(0次示例)13.62
MMLU-PRO(5次示例)23.77