NousResearch/Hermes-Function-Calling
Jupyter Notebook
Captured source
source ↗NousResearch/Hermes-Function-Calling
Language: Jupyter Notebook
License: MIT
Stars: 1386
Forks: 198
Open issues: 29
Created: 2024-02-24T09:00:28Z
Pushed: 2025-12-22T14:09:27Z
Default branch: main
Fork: no
Archived: no
README:
Hermes-Function-Calling
This repository contains code for the Hermes Pro Large Language Model to perform function calling based on the provided schema. It allows users to query the model and retrieve information related to stock prices, company fundamentals, financial statements, and more.
Installation
To install the required packages, run the following command:
pip install -r requirements.txt
Usage
Function calling
To run the function call inference with a query, use the following command:
python functioncall.py --query "I need the current stock price of Tesla (TSLA)"
Json mode
To run the json mode inference with a query, use the following command:
python jsonmode.py --query "Please return a json object to represent Goku from the anime Dragon Ball Z?"
Command Line Arguments
--model_path: Path to the model folder (default: "NousResearch/Hermes-2-Pro-Llama-3-8B").--chat_template: Chat template for prompt formatting (default: "chatml").--num_fewshot: Option to include few-shot examples (default: None).--load_in_4bit: Option to load in 4bit with bitsandbytes (default: "False").--query: Query to be used for function call inference (default: "I need the current stock price of Tesla (TSLA)").--max_depth: Maximum number of recursive iterations (default: 5).
Adding Custom Functions
To add your own functions for the model to use, you can modify the functions.py script. This script contains various functions that retrieve stock-related information using the yfinance library.
Here's an example of how to add a new function:
@tool
def get_new_function(symbol: str) -> dict:
"""
Description of the new function.
Args:
symbol (str): The stock symbol.
Returns:
dict: Dictionary containing the desired information.
"""
try:
# Implement the logic to retrieve the desired information
# using the yfinance library or any other relevant libraries
# Example:
stock = yf.Ticker(symbol)
new_info = stock.new_method()
return new_info
except Exception as e:
print(f"Error fetching new information for {symbol}: {e}")
return {}After defining your new function, make sure to add it to the get_openai_tools() function in the functions.py script:
def get_openai_tools() -> List[dict]: functions = [ # ... get_new_function, # ... ] tools = [convert_to_openai_tool(f) for f in functions] return tools
This will ensure that your new function is included in the list of available tools for the model to use.
Adding Custom Pydantic Model
To add your own pydantic models to create json schema for the model to use, you can replace the pydantic models in the jsonmode.py script.
Here's an example of how to add a new pydantic model:
from typing import List, Optional
from pydantic import BaseModel
class Character(BaseModel):
name: str
species: str
role: str
personality_traits: Optional[List[str]]
special_attacks: Optional[List[str]]
class Config:
schema_extra = {
"additionalProperties": False
}You need to serialize the pydantic model into json schema as follows:
pydantic_schema = Character.schema_json()
Key Scripts
The repository contains several key scripts that work together to enable function calling with the Hermes Pro Large Language Model:
functions.py: This script is where all the functions/tools you want the model to have access to are made available.
functioncall.py: This script is the main entry point for running the function call inference. It initializes the model, tokenizer, and other necessary components, and handles the recursive loop for generating function calls and executing them.
jsonmode.py: This script can be used for running json mode inference. It has similar functionality as functioncall.py but for generating json object adhering to the json schema and validating it.
prompter.py: This script manages the prompt generation process. It reads the system prompt from a YAML file, formats it with the necessary variables (e.g., tools, examples, schema), and generates the final prompt for the model.
schema.py: This script defines the Pydantic models used for representing function calls and function definitions. It provides a structured way to define and validate the function call schema.
Inference Example Output
Here's an example of the inference output:
user
Fetch the stock fundamentals data for Tesla (TSLA)
assistant
{"name": "get_stock_fundamentals", "arguments": {"symbol": "TSLA"}}
tool
{"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": null, "eps": 4.3, "beta": 2.427, "52_week_high": 299.29, "52_week_low": 152.37}}
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.Prompt Format
Hermes 2 Pro uses ChatML as the prompt format, opening up a much more structured system for engaging the LLM in multi-turn chat dialogue.…
Excerpt shown — open the source for the full document.