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 all 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
4 changes: 0 additions & 4 deletions python/qianfan/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,22 +535,18 @@ def get_res(self) -> Union[ChatCompletion, Completion, Embedding, Text2Image]:
log_warn("service status unknown, service could be unavailable.")
if self.service_type == ServiceType.Chat:
return ChatCompletion(
model=(self.model.name if self.model is not None else None),
endpoint=self.endpoint,
)
elif self.service_type == ServiceType.Completion:
return Completion(
model=(self.model.name if self.model is not None else None),
endpoint=self.endpoint,
)
elif self.service_type == ServiceType.Embedding:
return Embedding(
model=(self.model.name if self.model is not None else None),
endpoint=self.endpoint,
)
elif self.service_type == ServiceType.Text2Image:
return Text2Image(
model=(self.model.name if self.model is not None else None),
endpoint=self.endpoint,
)
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") or ""
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