2023-12-11 21:53:30 +00:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
from typing import (
|
|
|
|
|
Any,
|
|
|
|
|
AsyncIterator,
|
|
|
|
|
Dict,
|
|
|
|
|
Iterator,
|
|
|
|
|
List,
|
|
|
|
|
Optional,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from langchain_core.callbacks import (
|
|
|
|
|
AsyncCallbackManagerForLLMRun,
|
|
|
|
|
CallbackManagerForLLMRun,
|
|
|
|
|
)
|
|
|
|
|
from langchain_core.language_models.llms import LLM
|
|
|
|
|
from langchain_core.outputs import GenerationChunk
|
2024-07-11 16:24:26 +00:00
|
|
|
|
from langchain_core.pydantic_v1 import Field, SecretStr
|
2024-07-08 20:09:29 +00:00
|
|
|
|
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QianfanLLMEndpoint(LLM):
|
2024-08-07 14:57:27 +00:00
|
|
|
|
"""Baidu Qianfan completion model integration.
|
|
|
|
|
|
|
|
|
|
Setup:
|
|
|
|
|
Install ``qianfan`` and set environment variables ``QIANFAN_AK``, ``QIANFAN_SK``.
|
|
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
|
|
pip install qianfan
|
|
|
|
|
export QIANFAN_AK="your-api-key"
|
|
|
|
|
export QIANFAN_SK="your-secret_key"
|
|
|
|
|
|
|
|
|
|
Key init args — completion params:
|
|
|
|
|
model: str
|
|
|
|
|
Name of Qianfan model to use.
|
|
|
|
|
temperature: Optional[float]
|
|
|
|
|
Sampling temperature.
|
|
|
|
|
endpoint: Optional[str]
|
|
|
|
|
Endpoint of the Qianfan LLM
|
|
|
|
|
top_p: Optional[float]
|
|
|
|
|
What probability mass to use.
|
|
|
|
|
|
|
|
|
|
Key init args — client params:
|
|
|
|
|
timeout: Optional[int]
|
|
|
|
|
Timeout for requests.
|
|
|
|
|
api_key: Optional[str]
|
|
|
|
|
Qianfan API KEY. If not passed in will be read from env var QIANFAN_AK.
|
|
|
|
|
secret_key: Optional[str]
|
|
|
|
|
Qianfan SECRET KEY. If not passed in will be read from env var QIANFAN_SK.
|
|
|
|
|
|
|
|
|
|
See full list of supported init args and their descriptions in the params section.
|
|
|
|
|
|
|
|
|
|
Instantiate:
|
|
|
|
|
.. code-block:: python
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
2024-08-07 14:57:27 +00:00
|
|
|
|
from langchain_community.llms import QianfanLLMEndpoint
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
2024-08-07 14:57:27 +00:00
|
|
|
|
llm = QianfanLLMEndpoint(
|
|
|
|
|
model="ERNIE-3.5-8K",
|
|
|
|
|
# api_key="...",
|
|
|
|
|
# secret_key="...",
|
|
|
|
|
# other params...
|
|
|
|
|
)
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
2024-08-07 14:57:27 +00:00
|
|
|
|
Invoke:
|
2023-12-11 21:53:30 +00:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
input_text = "用50个字左右阐述,生命的意义在于"
|
|
|
|
|
llm.invoke(input_text)
|
2024-08-07 14:57:27 +00:00
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
'生命的意义在于体验、成长、爱与被爱、贡献与传承,以及对未知的勇敢探索与自我超越。'
|
2024-08-07 14:57:27 +00:00
|
|
|
|
|
|
|
|
|
Stream:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
for chunk in llm.stream(input_text):
|
2024-08-07 14:57:27 +00:00
|
|
|
|
print(chunk)
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
生命的意义 | 在于不断探索 | 与成长 | ,实现 | 自我价值,| 给予爱 | 并接受 | 爱, | 在经历 | 中感悟 | ,让 | 短暂的存在 | 绽放出无限 | 的光彩 | 与温暖 | 。
|
2024-08-07 14:57:27 +00:00
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
stream = llm.stream(input_text)
|
2024-08-07 14:57:27 +00:00
|
|
|
|
full = next(stream)
|
|
|
|
|
for chunk in stream:
|
|
|
|
|
full += chunk
|
|
|
|
|
full
|
|
|
|
|
|
|
|
|
|
.. code-block::
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
'生命的意义在于探索、成长、爱与被爱、贡献价值、体验世界之美,以及在有限的时间里追求内心的平和与幸福。'
|
2024-08-07 14:57:27 +00:00
|
|
|
|
|
|
|
|
|
Async:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
await llm.ainvoke(input_text)
|
2024-08-07 14:57:27 +00:00
|
|
|
|
|
|
|
|
|
# stream:
|
2024-08-19 13:23:09 +00:00
|
|
|
|
# async for chunk in llm.astream(input_text):
|
2024-08-07 14:57:27 +00:00
|
|
|
|
# print(chunk)
|
|
|
|
|
|
|
|
|
|
# batch:
|
2024-08-19 13:23:09 +00:00
|
|
|
|
# await llm.abatch([input_text])
|
2024-08-07 14:57:27 +00:00
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2024-08-19 13:23:09 +00:00
|
|
|
|
'生命的意义在于探索、成长、爱与被爱、贡献社会,在有限的时间里追寻无限的可能,实现自我价值,让生活充满色彩与意义。'
|
2024-08-07 14:57:27 +00:00
|
|
|
|
|
|
|
|
|
""" # noqa: E501
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
2024-01-01 21:12:31 +00:00
|
|
|
|
init_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
"""init kwargs for qianfan client init, such as `query_per_second` which is
|
|
|
|
|
associated with qianfan resource object to limit QPS"""
|
|
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
|
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
2024-01-01 21:12:31 +00:00
|
|
|
|
"""extra params for model invoke using with `do`."""
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
|
|
|
|
client: Any
|
|
|
|
|
|
2024-08-07 14:57:27 +00:00
|
|
|
|
qianfan_ak: Optional[SecretStr] = Field(default=None, alias="api_key")
|
|
|
|
|
qianfan_sk: Optional[SecretStr] = Field(default=None, alias="secret_key")
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
|
|
|
|
streaming: Optional[bool] = False
|
|
|
|
|
"""Whether to stream the results or not."""
|
|
|
|
|
|
2024-08-06 14:11:05 +00:00
|
|
|
|
model: Optional[str] = Field(default=None)
|
2023-12-11 21:53:30 +00:00
|
|
|
|
"""Model name.
|
|
|
|
|
you could get from https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu
|
|
|
|
|
|
|
|
|
|
preset models are mapping to an endpoint.
|
|
|
|
|
`model` will be ignored if `endpoint` is set
|
2024-08-06 14:11:05 +00:00
|
|
|
|
|
|
|
|
|
Default is set by `qianfan` SDK, not here
|
2023-12-11 21:53:30 +00:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
endpoint: Optional[str] = None
|
|
|
|
|
"""Endpoint of the Qianfan LLM, required if custom model used."""
|
|
|
|
|
|
2024-08-07 14:57:27 +00:00
|
|
|
|
request_timeout: Optional[int] = Field(default=60, alias="timeout")
|
2023-12-11 21:53:30 +00:00
|
|
|
|
"""request timeout for chat http requests"""
|
|
|
|
|
|
|
|
|
|
top_p: Optional[float] = 0.8
|
|
|
|
|
temperature: Optional[float] = 0.95
|
|
|
|
|
penalty_score: Optional[float] = 1
|
|
|
|
|
"""Model params, only supported in ERNIE-Bot and ERNIE-Bot-turbo.
|
|
|
|
|
In the case of other model, passing these params will not affect the result.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-07-08 20:09:29 +00:00
|
|
|
|
@pre_init
|
2023-12-11 21:53:30 +00:00
|
|
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
2023-12-20 05:49:33 +00:00
|
|
|
|
values["qianfan_ak"] = convert_to_secret_str(
|
|
|
|
|
get_from_dict_or_env(
|
|
|
|
|
values,
|
2024-08-07 14:57:27 +00:00
|
|
|
|
["qianfan_ak", "api_key"],
|
2023-12-20 05:49:33 +00:00
|
|
|
|
"QIANFAN_AK",
|
|
|
|
|
default="",
|
|
|
|
|
)
|
2023-12-11 21:53:30 +00:00
|
|
|
|
)
|
2023-12-20 05:49:33 +00:00
|
|
|
|
values["qianfan_sk"] = convert_to_secret_str(
|
|
|
|
|
get_from_dict_or_env(
|
|
|
|
|
values,
|
2024-08-07 14:57:27 +00:00
|
|
|
|
["qianfan_sk", "secret_key"],
|
2023-12-20 05:49:33 +00:00
|
|
|
|
"QIANFAN_SK",
|
|
|
|
|
default="",
|
|
|
|
|
)
|
2023-12-11 21:53:30 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
params = {
|
2024-01-01 21:12:31 +00:00
|
|
|
|
**values.get("init_kwargs", {}),
|
2023-12-11 21:53:30 +00:00
|
|
|
|
"model": values["model"],
|
|
|
|
|
}
|
2023-12-20 05:49:33 +00:00
|
|
|
|
if values["qianfan_ak"].get_secret_value() != "":
|
|
|
|
|
params["ak"] = values["qianfan_ak"].get_secret_value()
|
|
|
|
|
if values["qianfan_sk"].get_secret_value() != "":
|
|
|
|
|
params["sk"] = values["qianfan_sk"].get_secret_value()
|
2023-12-11 21:53:30 +00:00
|
|
|
|
if values["endpoint"] is not None and values["endpoint"] != "":
|
|
|
|
|
params["endpoint"] = values["endpoint"]
|
|
|
|
|
try:
|
|
|
|
|
import qianfan
|
|
|
|
|
|
|
|
|
|
values["client"] = qianfan.Completion(**params)
|
|
|
|
|
except ImportError:
|
|
|
|
|
raise ImportError(
|
|
|
|
|
"qianfan package not found, please install it with "
|
|
|
|
|
"`pip install qianfan`"
|
|
|
|
|
)
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def _identifying_params(self) -> Dict[str, Any]:
|
|
|
|
|
return {
|
|
|
|
|
**{"endpoint": self.endpoint, "model": self.model},
|
|
|
|
|
**super()._identifying_params,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def _llm_type(self) -> str:
|
|
|
|
|
"""Return type of llm."""
|
|
|
|
|
return "baidu-qianfan-endpoint"
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def _default_params(self) -> Dict[str, Any]:
|
|
|
|
|
"""Get the default parameters for calling Qianfan API."""
|
|
|
|
|
normal_params = {
|
|
|
|
|
"model": self.model,
|
|
|
|
|
"endpoint": self.endpoint,
|
|
|
|
|
"stream": self.streaming,
|
|
|
|
|
"request_timeout": self.request_timeout,
|
|
|
|
|
"top_p": self.top_p,
|
|
|
|
|
"temperature": self.temperature,
|
|
|
|
|
"penalty_score": self.penalty_score,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {**normal_params, **self.model_kwargs}
|
|
|
|
|
|
|
|
|
|
def _convert_prompt_msg_params(
|
|
|
|
|
self,
|
|
|
|
|
prompt: str,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> dict:
|
|
|
|
|
if "streaming" in kwargs:
|
|
|
|
|
kwargs["stream"] = kwargs.pop("streaming")
|
|
|
|
|
return {
|
|
|
|
|
**{"prompt": prompt, "model": self.model},
|
|
|
|
|
**self._default_params,
|
|
|
|
|
**kwargs,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def _call(
|
|
|
|
|
self,
|
|
|
|
|
prompt: str,
|
|
|
|
|
stop: Optional[List[str]] = None,
|
|
|
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> str:
|
|
|
|
|
"""Call out to an qianfan models endpoint for each generation with a prompt.
|
|
|
|
|
Args:
|
|
|
|
|
prompt: The prompt to pass into the model.
|
|
|
|
|
stop: Optional list of stop words to use when generating.
|
|
|
|
|
Returns:
|
|
|
|
|
The string generated by the model.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
.. code-block:: python
|
2024-04-24 23:39:23 +00:00
|
|
|
|
response = qianfan_model.invoke("Tell me a joke.")
|
2023-12-11 21:53:30 +00:00
|
|
|
|
"""
|
|
|
|
|
if self.streaming:
|
|
|
|
|
completion = ""
|
|
|
|
|
for chunk in self._stream(prompt, stop, run_manager, **kwargs):
|
|
|
|
|
completion += chunk.text
|
|
|
|
|
return completion
|
|
|
|
|
params = self._convert_prompt_msg_params(prompt, **kwargs)
|
2024-03-28 18:21:49 +00:00
|
|
|
|
params["stop"] = stop
|
2023-12-11 21:53:30 +00:00
|
|
|
|
response_payload = self.client.do(**params)
|
|
|
|
|
|
|
|
|
|
return response_payload["result"]
|
|
|
|
|
|
|
|
|
|
async def _acall(
|
|
|
|
|
self,
|
|
|
|
|
prompt: str,
|
|
|
|
|
stop: Optional[List[str]] = None,
|
|
|
|
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> str:
|
|
|
|
|
if self.streaming:
|
|
|
|
|
completion = ""
|
|
|
|
|
async for chunk in self._astream(prompt, stop, run_manager, **kwargs):
|
|
|
|
|
completion += chunk.text
|
|
|
|
|
return completion
|
|
|
|
|
|
|
|
|
|
params = self._convert_prompt_msg_params(prompt, **kwargs)
|
2024-03-28 18:21:49 +00:00
|
|
|
|
params["stop"] = stop
|
2023-12-11 21:53:30 +00:00
|
|
|
|
response_payload = await self.client.ado(**params)
|
|
|
|
|
|
|
|
|
|
return response_payload["result"]
|
|
|
|
|
|
|
|
|
|
def _stream(
|
|
|
|
|
self,
|
|
|
|
|
prompt: str,
|
|
|
|
|
stop: Optional[List[str]] = None,
|
|
|
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> Iterator[GenerationChunk]:
|
|
|
|
|
params = self._convert_prompt_msg_params(prompt, **{**kwargs, "stream": True})
|
2024-03-28 18:21:49 +00:00
|
|
|
|
params["stop"] = stop
|
2023-12-11 21:53:30 +00:00
|
|
|
|
for res in self.client.do(**params):
|
|
|
|
|
if res:
|
|
|
|
|
chunk = GenerationChunk(text=res["result"])
|
|
|
|
|
if run_manager:
|
|
|
|
|
run_manager.on_llm_new_token(chunk.text)
|
2024-03-03 22:13:22 +00:00
|
|
|
|
yield chunk
|
2023-12-11 21:53:30 +00:00
|
|
|
|
|
|
|
|
|
async def _astream(
|
|
|
|
|
self,
|
|
|
|
|
prompt: str,
|
|
|
|
|
stop: Optional[List[str]] = None,
|
|
|
|
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> AsyncIterator[GenerationChunk]:
|
|
|
|
|
params = self._convert_prompt_msg_params(prompt, **{**kwargs, "stream": True})
|
2024-03-28 18:21:49 +00:00
|
|
|
|
params["stop"] = stop
|
2023-12-11 21:53:30 +00:00
|
|
|
|
async for res in await self.client.ado(**params):
|
|
|
|
|
if res:
|
|
|
|
|
chunk = GenerationChunk(text=res["result"])
|
|
|
|
|
if run_manager:
|
|
|
|
|
await run_manager.on_llm_new_token(chunk.text)
|
2024-03-03 22:13:22 +00:00
|
|
|
|
yield chunk
|