Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model wrapper with local fine-tuning #169

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7b754a9
added features to download models from the hugging face model hub/loa…
zyzhang1130 Apr 19, 2024
ea00db0
added customized hyperparameters specification
zyzhang1130 Apr 23, 2024
3e8c468
added docstring and made changes in accordance with the comments
zyzhang1130 Apr 25, 2024
10a9870
decoupled model loading and tokenizer loading. Now can load tokenizer…
zyzhang1130 Apr 25, 2024
5237356
removed unnecessary info in README
zyzhang1130 Apr 25, 2024
a6918eb
resolved all issues flagged by `pre-commit run`
zyzhang1130 Apr 25, 2024
b4f4f40
further removed info irrelevant to model loading and finetuning
zyzhang1130 Apr 25, 2024
e33b3de
Update huggingface_model.py
zyzhang1130 Apr 26, 2024
8023820
updated according to suggestions given
zyzhang1130 May 2, 2024
0a079b9
added updated README
zyzhang1130 May 2, 2024
a4d1f1b
updated README for two examples and tested on 3 model_type.
zyzhang1130 May 5, 2024
6b5410e
undo update to conversation_with_mentions README (created a dedicated…
zyzhang1130 May 6, 2024
6d10051
reverted changes made to conversation_with_RAG_agents\README.md
zyzhang1130 May 6, 2024
db27edd
resolved pre-commit related issues
zyzhang1130 May 6, 2024
b371226
resolved pre-commit related issues
zyzhang1130 May 6, 2024
7f3a012
resolved pre-commit related issues
zyzhang1130 May 6, 2024
15bf79a
resolve issues mentioned
zyzhang1130 May 8, 2024
9998e66
resolve issues raised
zyzhang1130 May 8, 2024
f6b46ed
resolve issues raised
zyzhang1130 May 8, 2024
6bf09f1
Update README.md
zyzhang1130 May 10, 2024
8d7e880
Update README.md
zyzhang1130 May 10, 2024
195ac69
Merge branch 'modelscope:main' into main
zyzhang1130 May 17, 2024
98b471e
Update huggingface_model.py
zyzhang1130 May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/load_finetune_huggingface_model/README.md
ZiTao-Li marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Multi-Agent Conversation with Custom Model Loading and Fine-Tuning in AgentScope
ZiTao-Li marked this conversation as resolved.
Show resolved Hide resolved

This example demonstrates how to load and optionally fine-tune a Hugging Face model within a multi-agent conversation setup using AgentScope. The complete code is provided in `load_finetune_huggingface_model.py`.

## Background

In the context of AgentScope, agents are designed to mimic user and assistant roles in a conversation. This setup allows for the integration and testing of different models from the Hugging Face Hub, enhancing their capabilities through fine-tuning with custom datasets.
ZiTao-Li marked this conversation as resolved.
Show resolved Hide resolved

## Functionality Overview

This example allows you to:

- Set up a user agent and an assistant agent for interactive conversations.
- Modify the `sys_prompt` to customize the assistant agent's role.
- Terminate the conversation by entering "exit".

## Advanced Features

Beyond basic conversation setup, the example introduces advanced functionalities:

- Use `dialog_agent.load_model(model_id, local_model_path)` to load a model either from the Hugging Face Model Hub or a local directory.
- Apply `dialog_agent.fine_tune(data_path)` to fine-tune the model based on your dataset.

The default hyperparameters for (SFT) fine-tuning are specified in `agentscope/src/agentscope/models/huggingface_model.py`. For customized hyperparameters, specify them in `model_configs` if the model needs to be fine-tuned at initialization, or specify through `fine_tune_config` in `Finetune_DialogAgent`'s `fine_tune` method after initialization, as shown in the example script `load_finetune_huggingface_model.py`.
ZiTao-Li marked this conversation as resolved.
Show resolved Hide resolved

## Agent Initialization

When initializing an agent, the following parameters need specification:

- `model_id` (str): Identifier for the model on Hugging Face.
- `local_model_path` (str): Local path to the model (defaults to loading from Hugging Face if not provided).
- `data_path` (str): Path to training data (fine-tuning is skipped if not provided).
- `device` (str): The device (e.g., 'cuda', 'cpu') for model operation, defaulting to 'cuda' if available.
- `fine_tune_config` (dict, Optional): A configuration dictionary for fine-tuning the model. It allows specifying hyperparameters and other training options that will be passed to the fine-tuning method. If not provided, default settings will be used. This allows for customization of the fine-tuning process to optimize model performance based on specific requirements.
- `huggingface_token` (from .env file): Token required for models needing authentication from Hugging Face.

## Tested Models

The example is tested using specific Hugging Face models. While it is designed to be flexible, some models may require additional configuration or modification of the provided scripts.

## Prerequisites

Before running this example, ensure you have installed the following packages:

- `transformers`
- `peft`
- `python-dotenv`
- `datasets`
- `trl`

Additionally, set your Hugging Face token in the `.env` file:
ZiTao-Li marked this conversation as resolved.
Show resolved Hide resolved

```bash
python load_finetune_huggingface_model.py
Loading
Loading