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

feat[resources]: 支持chat_completion调用ERNIE-Functions-8K #678

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1,939 changes: 1,804 additions & 135 deletions python/poetry.lock

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ aiolimiter = ">=1.1.0"
importlib-metadata = { version = ">=1.4.0", python = "<=3.7" }
bce-python-sdk = ">=0.8.79"
typing-extensions = { version = ">=4.0.0", python = "<=3.10" }
pydantic = "*"
pydantic = ">=1.0"
python-dotenv = [
{ version = "<=0.21.1", python = "<3.8" },
{ version = ">=1.0", python = ">=3.8" }
]
tenacity = "^8.2.3"
multiprocess = "*"
multiprocess = ">=0.70.12"
langchain = { version = ">=0.1.10", python = ">=3.8.1", optional = true }
langchain-community = { version = ">=0.2.0", python = ">=3.8.1", optional = true}
langchain-community = { version = ">=0.2.0", python = ">=3.8.1", optional = true }
numpy = [
{ version = "<1.22.0", python = ">=3.7 <3.8", optional = true },
{ version = ">=1.22.0", python = ">=3.8", optional = true }
Expand All @@ -39,26 +39,27 @@ pyarrow = [
{ version = ">=14.0.1", python = ">=3.8", optional = true },
{ version = "<=12.0.1", python = ">=3.7 <3.8", optional = true }
]
locust = { version = "*", optional = true }
tabulate = { version = "*", optional = true }
locust = { version = ">=2.9.0", optional = true }
tabulate = { version = ">=0.9.0", optional = true }
python-dateutil = { version = "^2.8.2", optional = true }
rich = ">=13.0.0"
typer = ">=0.9.0"
pyyaml = "^6.0.1"
prompt-toolkit = ">=3.0.38"
torch = [
{ version = "<=1.13.1", python = "<3.8", optional = true},
{ version = "<=1.13.1", python = "<3.8", optional = true },
{ version = ">=1.4.0", python = ">=3.8", optional = true }
]
ltp = { version = ">=4.2.0", optional = true }
emoji = { version = ">=2.2.0", optional = true }
sentencepiece = { version = ">=0.1.98", optional = true }
diskcache = "^5.6.3"
diskcache = ">=5.6.3"
cachetools = ">=5.0.0"

ijson = { version = "*", optional = true }
fastapi = { version = "*", optional = true }
uvicorn = { version = "*", optional = true }
filelock = { version = "*", optional = true}
ijson = { version = ">=3.0", optional = true }
fastapi = { version = ">=0.85.0", optional = true }
uvicorn = { version = ">=0.15.0", optional = true }
filelock = { version = ">=3.7.0", optional = true }

[tool.poetry.scripts]
qianfan = "qianfan.common.client.main:main"
Expand Down
53 changes: 41 additions & 12 deletions python/qianfan/common/client/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.


import json
import time
from typing import Any, Callable, Dict, List, Optional

Expand Down Expand Up @@ -41,7 +42,7 @@
from qianfan.model.configs import DeployConfig
from qianfan.model.consts import ServiceType
from qianfan.resources.console.consts import DeployPoolType, FinetuneSupportModelType
from qianfan.trainer import DPO, LLMFinetune, PostPreTrain
from qianfan.trainer import DPO, Finetune, PostPreTrain, Trainer
from qianfan.trainer.actions import (
DeployAction,
EvaluateAction,
Expand Down Expand Up @@ -220,7 +221,7 @@ def list_train_type(
if cmd == "postpretrain":
model_list = PostPreTrain.train_type_list()
elif cmd in ["finetune", "run"]:
model_list = LLMFinetune.train_type_list()
model_list = Finetune.train_type_list()
elif cmd in ["dpo"]:
model_list = DPO.train_type_list()
else:
Expand Down Expand Up @@ -306,7 +307,7 @@ def show_config_limit(
if cmd == "postpretrain":
model_list = PostPreTrain.train_type_list()
elif cmd in ["finetune", "run"]:
model_list = LLMFinetune.train_type_list()
model_list = Finetune.train_type_list()
elif cmd in ["dpo"]:
model_list = DPO.train_type_list()
else:
Expand Down Expand Up @@ -366,6 +367,9 @@ def finetune(
list_train_type: Optional[bool] = list_train_type_option,
show_config_limit: Optional[str] = typer.Option(
None,
"--show-config-limit",
"--show",
"-s",
callback=show_config_limit,
is_eager=True,
help="Show config limit for specified train type.",
Expand Down Expand Up @@ -445,7 +449,7 @@ def finetune(
callback = MyEventHandler(console=console)

if trainer_pipeline_file is not None:
trainer = LLMFinetune.load(file=trainer_pipeline_file)
trainer = Finetune.load(file=trainer_pipeline_file)
trainer.register_event_handler(callback)
else:
ds = None
Expand All @@ -465,7 +469,7 @@ def finetune(
pool_type=DeployPoolType[deploy_pool_type],
service_type=ServiceType[deploy_service_type],
)
trainer = LLMFinetune(
trainer = Finetune(
dataset=ds,
train_type=train_type,
event_handler=callback,
Expand Down Expand Up @@ -515,6 +519,36 @@ def finetune(
time.sleep(0.1)


@trainer_app.command()
@credential_required
def info(
trainer_id: Optional[str] = typer.Option(None, help="trainer id"),
task_id: Optional[str] = typer.Option(None, help="task id"),
) -> None:
"""
get a trainer info.
"""
console = replace_logger_handler()
trainer: Optional[Trainer] = None
if trainer_id:
trainer = Finetune.load(id=trainer_id)
elif task_id:
trainers = Finetune.list()
for t in trainers:
for action in t.actions:
if isinstance(action, TrainAction) and action.task_id == task_id:
trainer = t
break
else:
console.log("Must provide either trainer id or task id.")
if trainer:
json_str = json.dumps(trainer.info(), ensure_ascii=False, indent=2)
print(json_str)

# wait a second for the log to be flushed
time.sleep(0.1)


@trainer_app.command()
@credential_required
def postpretrain(
Expand Down Expand Up @@ -665,11 +699,6 @@ def postpretrain(
time.sleep(0.1)


@trainer_app.command(
"run",
deprecated=True,
help="Run a dpo trainer task.",
)
@trainer_app.command()
@credential_required
def dpo(
Expand Down Expand Up @@ -768,7 +797,7 @@ def dpo(
callback = MyEventHandler(console=console)

if trainer_pipeline_file is not None:
trainer = LLMFinetune.load(file=trainer_pipeline_file)
trainer = Finetune.load(file=trainer_pipeline_file)
trainer.register_event_handler(callback)
else:
ds = None
Expand All @@ -788,7 +817,7 @@ def dpo(
pool_type=DeployPoolType[deploy_pool_type],
service_type=ServiceType[deploy_service_type],
)
trainer = LLMFinetune(
trainer = Finetune(
dataset=ds,
train_type=train_type,
event_handler=callback,
Expand Down
6 changes: 3 additions & 3 deletions python/qianfan/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ def compress(
comp_task_detail_resp["result"]["status"]
== console_const.ModelCompTaskStatus.Succeeded.value
):
new_model_version_id = comp_task_detail_resp["result"].get("modelId")
new_model_id = comp_task_detail_resp["result"].get("modelId")
log_info(
f"compress task {model_comp_task_id} run with status"
f" {comp_task_detail_resp['result']['status']}"
f" new model_version_id: {new_model_version_id}"
f" new model_id: {new_model_id}"
)
new_model = Model(id=new_model_version_id)
new_model = Model(id=new_model_id)
new_model.auto_complete_info()
return new_model
else:
Expand Down
6 changes: 3 additions & 3 deletions python/qianfan/resources/llm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ def __init__(
self, version: Optional[Literal["1", "2", 1, 2]] = None, **kwargs: Any
) -> None:
self._version = str(version) if version else "1"
self._real = self._real_base(self._version)(**kwargs)
self._backup = self._real_base("1")(**kwargs)
self._real = self._real_base(self._version, **kwargs)(**kwargs)
self._backup = self._real_base("1", **kwargs)(**kwargs)

@classmethod
def _real_base(cls, version: str) -> Type[BaseResource]:
def _real_base(cls, version: str, **kwargs: Any) -> Type[BaseResource]:
"""
return the real base class
"""
Expand Down
20 changes: 19 additions & 1 deletion python/qianfan/resources/llm/chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
BatchRequestFuture,
VersionBase,
)
from qianfan.resources.llm.function import Function
from qianfan.resources.tools.tokenizer import Tokenizer
from qianfan.resources.typing import JsonBody, QfLLMInfo, QfMessages, QfResponse, QfRole
from qianfan.utils.logging import log_error, log_info
Expand Down Expand Up @@ -1537,7 +1538,24 @@ class ChatCompletion(VersionBase):
_real: Union[_ChatCompletionV1, _ChatCompletionV2]

@classmethod
def _real_base(cls, version: str) -> Type:
def _real_base(cls, version: str, **kwargs: Any) -> Type:
# convert to qianfan.Function
if kwargs.get("use_function"):
return Function
else:
model = kwargs.get("model", "")
func_model_info_list = {
k.lower(): v for k, v in Function._supported_models().items()
}
func_model_info = func_model_info_list.get(model.lower())
if model and func_model_info:
if func_model_info and func_model_info.endpoint:
return Function
endpoint = kwargs.get("endpoint", "")
for m in func_model_info_list.values():
if endpoint and m.endpoint == endpoint:
return Function

if version == "1":
return _ChatCompletionV1
elif version == "2":
Expand Down
2 changes: 2 additions & 0 deletions python/qianfan/resources/llm/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def _supported_models(cls) -> Dict[str, QfLLMInfo]:
"user_id",
"stop",
"max_output_tokens",
"enable_user_memory",
"user_memory_extract_level",
},
max_input_chars=11200,
max_input_tokens=7168,
Expand Down
8 changes: 4 additions & 4 deletions python/qianfan/tests/trainer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_trainer_sft_run():

eh = MyEventHandler()
sft_task = LLMFinetune(
train_type="ERNIE-Bot-turbo-0725",
train_type="ERNIE-Speed-8K",
dataset=ds,
train_config=train_config,
event_handler=eh,
Expand All @@ -157,11 +157,11 @@ def test_trainer_sft_run():
def test_trainer_sft_run_from_bos():
with pytest.raises(InvalidArgumentError):
sft_task = LLMFinetune(
train_type="ERNIE-Bot-turbo-0725",
train_type="ERNIE-Speed-8K",
)
sft_task.run()
sft_task = LLMFinetune(
train_type="ERNIE-Bot-turbo-0725", dataset_bos_path="bos:/sdk-test/"
train_type="ERNIE-Speed-8K", dataset_bos_path="bos:/sdk-test/"
)
sft_task.run()
res = sft_task.result
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_trainer_resume():
ds = Dataset.load(source=qianfan_data_source, organize_data_as_group=True)

sft_task = LLMFinetune(
train_type="ERNIE-Bot-turbo-0725",
train_type="ERNIE-Speed-8K",
dataset=ds,
)
ppl = sft_task.ppls[0]
Expand Down
Loading
Loading