Skip to content

Commit

Permalink
feat: added experimental support to tool function loading for agents …
Browse files Browse the repository at this point in the history
…and refactored prompt loading accordingly
  • Loading branch information
MoritzLaurer committed Nov 27, 2024
1 parent 87a509e commit d9383a3
Show file tree
Hide file tree
Showing 19 changed files with 1,718 additions and 724 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ repos:
rev: v1.13.0
hooks:
- id: mypy
additional_dependencies: [types-PyYAML==6.0.12.20240917]
additional_dependencies:
- types-PyYAML==6.0.12.20240917
- types-setuptools
args: ["--config-file=pyproject.toml"]
exclude: ^tests/
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pip install hf-hub-prompts
['code_teacher.yaml', 'translate.yaml']

>>> # 2. Download a prompt template:
>>> from hf_hub_prompts import download_prompt_template
>>> prompt_template = download_prompt_template(
>>> from hf_hub_prompts import PromptTemplateLoader
>>> prompt_template = PromptTemplateLoader.from_hub(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
Expand Down
7 changes: 5 additions & 2 deletions docs/reference/hub_api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Downloading prompts
# Downloading prompts and tools

This section documents the functions for downloading and listing prompt templates from the Hugging Face Hub.
This section documents the classes and functions for downloading prompt templates and tools from the Hugging Face Hub.

!!! note
Note that the ToolLoader class and related functionalities for working with tools is still highly experimental.

::: hf_hub_prompts.hub_api
options:
Expand Down
10 changes: 10 additions & 0 deletions docs/reference/tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Tools

This section documents the Tool class.

!!! note
This class is still in an early experimental stage.

::: hf_hub_prompts.tools
options:
show_if_no_docstring: false
37 changes: 25 additions & 12 deletions docs/repo_types_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ list_prompt_templates(repo_id="MoritzLaurer/closed_system_prompts")
Here, we download the leaked prompt for Claude-3.5 Sonnet for creating Artifacts.

```python
from hf_hub_prompts import download_prompt_template
prompt_template = download_prompt_template(
from hf_hub_prompts import PromptTemplateLoader
prompt_template = PromptTemplateLoader.from_hub(
repo_id="MoritzLaurer/closed_system_prompts",
filename="claude-3-5-artifacts-leak-210624.yaml"
)

print(prompt_template)
# ChatPromptTemplate(messages=[{'role': 'system', 'content': '<artifacts_info> The assistant can create and reference artifacts during conversations. Artifacts are ... Claude is now being connected with a human.'}, {'role': 'user', 'content': '{user_message}'}], input_variables=['current_date', 'user_message'], metadata=[{'source': 'https://gist.github.com/dedlim/6bf6d81f77c19e20cd40594aa09e3ecd'}])
```
Expand Down Expand Up @@ -82,9 +83,12 @@ response = client_anthropic.messages.create(
The paper "JudgeBench: A Benchmark for Evaluating LLM-Based Judges" (<a href="https://arxiv.org/pdf/2410.12784">paper</a>) collects several prompts for using LLMs to evaluate unstructured LLM outputs. After copying them into a <a href="https://huggingface.co/MoritzLaurer/judgebench-prompts">HF Hub model repo</a> in the standardized YAML format, they can be directly loaded and populated.

```python
from hf_hub_prompts import download_prompt_template
from hf_hub_prompts import PromptTemplateLoader
prompt_template = PromptTemplateLoader.from_hub(
repo_id="MoritzLaurer/judgebench-prompts",
filename="vanilla-prompt.yaml"
)

prompt_template = download_prompt_template(repo_id="MoritzLaurer/judgebench-prompts", filename="vanilla-prompt.yaml")
```
</details>

Expand All @@ -95,11 +99,14 @@ The community has extracted system prompts from closed API providers like OpenAI


```python
from hf_hub_prompts import list_prompt_templates, download_prompt_template
from hf_hub_prompts import list_prompt_templates, PromptTemplateLoader
list_prompt_templates(repo_id="MoritzLaurer/closed_system_prompts")
# out: ['claude-3-5-artifacts-leak-210624.yaml', 'claude-3-5-sonnet-text-090924.yaml', 'claude-3-5-sonnet-text-image-090924.yaml', 'jokes-prompt.yaml', 'openai-metaprompt-audio.yaml', 'openai-metaprompt-text.yaml']

prompt_template = download_prompt_template(repo_id="MoritzLaurer/closed_system_prompts", filename="openai-metaprompt-text.yaml")
prompt_template = PromptTemplateLoader.from_hub(
repo_id="MoritzLaurer/closed_system_prompts",
filename="openai-metaprompt-text.yaml"
)
```
</details>

Expand All @@ -118,10 +125,12 @@ These prompts are currently either mentioned unsystematically in model cards or
<summary>1. Example: Sharing the <a href="https://huggingface.co/MoritzLaurer/open_models_special_prompts">InternVL2 special task prompts</a></summary>

```python
from hf_hub_prompts import download_prompt_template

# download image prompt template
prompt_template = download_prompt_template(repo_id="MoritzLaurer/open_models_special_prompts", filename="internvl2-bbox-prompt.yaml")
from hf_hub_prompts import PromptTemplateLoader
prompt_template = PromptTemplateLoader.from_hub(
repo_id="MoritzLaurer/open_models_special_prompts",
filename="internvl2-bbox-prompt.yaml"
)

# populate prompt
image_url = "https://unsplash.com/photos/ZVw3HmHRhv0/download?ixid=M3wxMjA3fDB8MXxhbGx8NHx8fHx8fDJ8fDE3MjQ1NjAzNjl8&force=true&w=1920"
Expand Down Expand Up @@ -183,11 +192,15 @@ See this <a href="https://huggingface.co/datasets/MoritzLaurer/dataset_prompts">


```python
from hf_hub_prompts import download_prompt_template
from hf_hub_prompts import PromptTemplateLoader
import torch
from transformers import pipeline

prompt_template = download_prompt_template(repo_id="MoritzLaurer/dataset_prompts", filename="fineweb-edu-prompt.yaml", repo_type="dataset")
prompt_template = PromptTemplateLoader.from_hub(
repo_id="MoritzLaurer/dataset_prompts",
filename="fineweb-edu-prompt.yaml",
repo_type="dataset"
)

# populate the prompt
text_to_score = "The quick brown fox jumps over the lazy dog"
Expand Down Expand Up @@ -229,7 +242,7 @@ The prompts could be directly added to the dataset repository in the standardize

## 4. Attaching prompts to HF Spaces

See also the [Agents](agents.md) and [Tools](tools.md) page for using HF Spaces for hosting prompts and tools as part of agents.
See also the [Agents](agents.md) and [Tools](standard_tool_format.md) page for using HF Spaces for hosting prompts and tools as part of agents.

[TODO: create example]

Expand Down
2 changes: 1 addition & 1 deletion docs/standard_prompt_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ prompt_template_langchain = prompt_template.to_langchain_template()
- [LangGraph Templates](https://blog.langchain.dev/launching-langgraph-templates/) (underlying data structure unclear, does not seem to have a collaborative way of sharing templates)
- [LlamaHub](https://llamahub.ai/) (seems to use GitHub as backend)
- [Deepset Prompt Hub](https://github.com/deepset-ai/prompthub) (seems not maintained anymore, used YAML with {...} for input variables)
- distilabel [templates](https://github.com/argilla-io/distilabel/tree/main/src/distilabel/steps/tasks/templates) and [tasks](https://distilabel.argilla.io/latest/components-gallery/tasks/) (using pure jinja2 with {{ ... }} for input variables)
- distilabel [templates](https://github.com/argilla-io/distilabel/tree/main/src/distilabel/steps/tasks/templates) and [tasks](https://distilabel.argilla.io/latest/components-gallery/tasks/) ([source](https://github.com/argilla-io/distilabel/tree/main/src/distilabel/steps/tasks)) (using pure jinja2 with {{ ... }} for input variables)
- [Langfuse](https://langfuse.com/docs/prompts/get-started), see also [example here](https://langfuse.com/guides/cookbook/prompt_management_langchain) (no public prompt repo, using JSON internally with {{...}} for input variables)
- [Promptify](https://github.com/promptslab/Promptify/tree/27a53fa8e8f2a4d90f887d06ece65a44466f873a/promptify/prompts) (not maintained anymore, used jinja1 and {{ ... }} for input variables)
File renamed without changes.
104 changes: 99 additions & 5 deletions examples/example-usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"id": "43c0e4d4-b25d-48a9-ba4c-7b412c641677",
"metadata": {},
"source": [
"# Testing approaches to prompt sharing/downloading on the HF Hub"
"# Testing approaches to prompt sharing/downloading on the HF Hub\n",
"\n",
"> [!WARNING] \n",
"> This notebook is not maintained or tested and contains outdated code. I'm just using it for quick testing of code."
]
},
{
Expand Down Expand Up @@ -47,17 +50,17 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "947ac23c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<module 'hf_hub_prompts.prompt_templates' from '/Users/moritzlaurer/huggingface/projects/hf-hub-prompts/hf_hub_prompts/prompt_templates.py'>"
"<module 'hf_hub_prompts.tools' from '/Users/moritzlaurer/huggingface/projects/hf-hub-prompts/hf_hub_prompts/tools.py'>"
]
},
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -69,7 +72,98 @@
"\n",
"# Reload the specific module\n",
"importlib.reload(hf_hub_prompts.hub_api)\n",
"importlib.reload(hf_hub_prompts.prompt_templates)"
"importlib.reload(hf_hub_prompts.prompt_templates)\n",
"importlib.reload(hf_hub_prompts.tools)\n"
]
},
{
"cell_type": "markdown",
"id": "509335ed",
"metadata": {},
"source": [
"### Example tool use"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "37f739bd",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.chdir(\"/Users/moritzlaurer/huggingface/projects/hf-hub-prompts\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ab33eeae",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tool class: {'func': <function get_stock_price at 0x108a09300>, 'name': 'get_stock_price', 'description': 'Retrieve stock price data for a given ticker symbol.', 'args_description': {'ticker': \"The stock ticker symbol (e.g., 'AAPL' for Apple Inc.)\", 'days': 'Number of days of historical data to fetch (default: 1d).'}, 'return_description': \"Dict[str, Union[float, str, list, datetime]]: Dictionary containing: - prices (list): List of closing prices for requested days - currency (str): The currency of the price (e.g., 'USD') - timestamps (list): List of timestamps for each price\", 'raises_description': {'ValueError': 'If days parameter is not one of the allowed values'}, 'metadata': {'version': '0.0.1', 'author': 'John Doe', 'requires_gpu': 'False', 'requires_api_key': 'False'}, 'dependencies': {'yfinance'}}\n",
"OpenAI function: {'name': 'get_stock_price', 'description': 'Retrieve stock price data for a given ticker symbol.', 'parameters': {'type': 'object', 'properties': {'ticker': {'type': 'string', 'description': \"The stock ticker symbol (e.g., 'AAPL' for Apple Inc.)\"}, 'days': {'type': 'string', 'description': 'Number of days of historical data to fetch (default: 1d).'}}, 'required': ['ticker']}}\n",
"Result: {'prices': [228.52000427246094, 229.8699951171875, 232.8699951171875, 235.05999755859375, 234.9499969482422], 'currency': 'USD', 'timestamps': [datetime.datetime(2024, 11, 21, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 22, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 25, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 26, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 27, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>)]}\n",
"Tool class: {'func': <function get_stock_price at 0x10b9edda0>, 'name': 'get_stock_price', 'description': 'Retrieve stock price data for a given ticker symbol.', 'args_description': {'ticker': \"The stock ticker symbol (e.g., 'AAPL' for Apple Inc.)\", 'days': 'Number of days of historical data to fetch (default: 1d).'}, 'return_description': \"Dict[str, Union[float, str, list, datetime]]: Dictionary containing: - prices (list): List of closing prices for requested days - currency (str): The currency of the price (e.g., 'USD') - timestamps (list): List of timestamps for each price\", 'raises_description': {'ValueError': 'If days parameter is not one of the allowed values'}, 'metadata': {'version': '0.0.1', 'author': 'John Doe', 'requires_gpu': 'False', 'requires_api_key': 'False'}, 'dependencies': {'yfinance'}}\n",
"OpenAI function: {'name': 'get_stock_price', 'description': 'Retrieve stock price data for a given ticker symbol.', 'parameters': {'type': 'object', 'properties': {'ticker': {'type': 'string', 'description': \"The stock ticker symbol (e.g., 'AAPL' for Apple Inc.)\"}, 'days': {'type': 'string', 'description': 'Number of days of historical data to fetch (default: 1d).'}}, 'required': ['ticker']}}\n",
"Result: {'prices': [228.52000427246094, 229.8699951171875, 232.8699951171875, 235.05999755859375, 234.9499969482422], 'currency': 'USD', 'timestamps': [datetime.datetime(2024, 11, 21, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 22, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 25, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 26, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>), datetime.datetime(2024, 11, 27, 0, 0, tzinfo=<DstTzInfo 'America/New_York' EST-1 day, 19:00:00 STD>)]}\n"
]
}
],
"source": [
"from hf_hub_prompts import ToolLoader\n",
"\n",
"tool = ToolLoader.from_local(\"./tests/test_data/get_stock_price.py\")\n",
"#print(\"Tool class:\", tool.__dict__)\n",
"#print(\"OpenAI function:\", tool.to_openai_function())\n",
"result = tool(ticker=\"AAPL\", days=\"5d\")\n",
"print(\"Result:\", result)\n",
"\n",
"tool = ToolLoader.from_hub(repo_id=\"MoritzLaurer/example_tools\", filename=\"get_stock_price.py\")\n",
"#print(\"Tool class:\", tool.__dict__)\n",
"#print(\"OpenAI function:\", tool.to_openai_function())\n",
"result = tool(ticker=\"AAPL\", days=\"5d\")\n",
"print(\"Result:\", result)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "307d7efb",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "module 'hf_hub_prompts' has no attribute 'load_tool'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[4], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mhf_hub_prompts\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m tool \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_prompts\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload_tool\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m./tests/test_data/get_stock_price.py\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUninstalled dependencies:\u001b[39m\u001b[38;5;124m\"\u001b[39m, tool\u001b[38;5;241m.\u001b[39mreturn_uninstalled_dependencies())\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTool class:\u001b[39m\u001b[38;5;124m\"\u001b[39m, tool\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__dict__\u001b[39m)\n",
"\u001b[0;31mAttributeError\u001b[0m: module 'hf_hub_prompts' has no attribute 'load_tool'"
]
}
],
"source": [
"import hf_hub_prompts\n",
"import os\n",
"\n",
"tool = hf_hub_prompts.load_tool(\"./tests/test_data/get_stock_price.py\")\n",
"\n",
"print(\"Uninstalled dependencies:\", tool.return_uninstalled_dependencies())\n",
"\n",
"print(\"Tool class:\", tool.__dict__)\n",
"\n",
"print(\"OpenAI function:\", tool.to_openai_function())\n",
"\n",
"result = tool(ticker=\"AAPL\", days=\"5d\")\n",
"\n",
"print(\"Result:\", result)\n"
]
},
{
Expand Down
12 changes: 8 additions & 4 deletions hf_hub_prompts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from .hub_api import download_prompt_template, list_prompt_templates
from .hub_api import PromptTemplateLoader, ToolLoader, list_prompt_templates, list_tools
from .populated_prompt import PopulatedPrompt
from .prompt_templates import BasePromptTemplate, ChatPromptTemplate, TextPromptTemplate
from .tools import Tool


__all__ = [
"PromptTemplateLoader",
"list_prompt_templates",
"BasePromptTemplate",
"TextPromptTemplate",
"ChatPromptTemplate",
"BasePromptTemplate",
"PopulatedPrompt",
"download_prompt_template",
"list_prompt_templates",
"ToolLoader",
"list_tools",
"Tool",
]
Loading

0 comments on commit d9383a3

Please sign in to comment.