fix: chat_models Qianfan not compatiable with SystemMessage (#10642)

- **Description:** QianfanEndpoint bugs for SystemMessages. When the
`SystemMessage` is input as the messages to
`chat_models.QianfanEndpoint`. A `TypeError` will be raised.
  - **Issue:** #10643
  - **Dependencies:** 
  - **Tag maintainer:** @baskaryan
  - **Twitter handle:** no
This commit is contained in:
DanielZzz
2023-09-20 13:35:51 +08:00
committed by GitHub
parent f0198354d9
commit ebe08412ad
5 changed files with 293 additions and 72 deletions
@@ -34,26 +34,47 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[INFO] [09-15 20:01:35] logging.py:55 [t:140292313159488]: trying to refresh access_token\n",
"[INFO] [09-15 20:01:35] logging.py:55 [t:140292313159488]: sucessfully refresh access_token\n",
"[INFO] [09-15 20:01:35] logging.py:55 [t:140292313159488]: requesting llm api endpoint: /embeddings/embedding-v1\n",
"[INFO] [09-15 20:01:35] logging.py:55 [t:140292313159488]: async requesting llm api endpoint: /embeddings/embedding-v1\n",
"[INFO] [09-15 20:01:35] logging.py:55 [t:140292313159488]: async requesting llm api endpoint: /embeddings/embedding-v1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-0.03313107788562775, 0.052325375378131866, 0.04951248690485954, 0.0077608139254152775, -0.05907672271132469, -0.010798933915793896, 0.03741293027997017, 0.013969100080430508]\n",
" [0.0427522286772728, -0.030367236584424973, -0.14847028255462646, 0.055074431002140045, -0.04177454113960266, -0.059512972831726074, -0.043774791061878204, 0.0028191760648041964]\n",
" [0.03803155943751335, -0.013231384567916393, 0.0032379645854234695, 0.015074018388986588, -0.006529552862048149, -0.13813287019729614, 0.03297128155827522, 0.044519297778606415]\n"
]
}
],
"source": [
"\"\"\"For basic init and call\"\"\"\n",
"from langchain.embeddings.baidu_qianfan_endpoint import QianfanEmbeddingsEndpoint \n",
"from langchain.embeddings import QianfanEmbeddingsEndpoint \n",
"\n",
"import os\n",
"os.environ[\"QIANFAN_AK\"] = \"xx\"\n",
"os.environ[\"QIANFAN_SK\"] = \"xx\"\n",
"os.environ[\"QIANFAN_AK\"] = \"your_ak\"\n",
"os.environ[\"QIANFAN_SK\"] = \"your_sk\"\n",
"\n",
"embed = QianfanEmbeddingsEndpoint(qianfan_ak='xxx', \n",
" qianfan_sk='xxx')\n",
"embed = QianfanEmbeddingsEndpoint(\n",
" # qianfan_ak='xxx', \n",
" # qianfan_sk='xxx'\n",
")\n",
"res = embed.embed_documents([\"hi\", \"world\"])\n",
"\n",
"import asyncio\n",
"\n",
"async def aioEmbed():\n",
" res = await embed.aembed_query(\"qianfan\")\n",
" print(res)\n",
" print(res[:8])\n",
"await aioEmbed()\n",
"\n",
"import asyncio\n",
@@ -81,16 +102,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[INFO] [09-15 20:01:40] logging.py:55 [t:140292313159488]: requesting llm api endpoint: /embeddings/bge_large_zh\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-0.0001582596160005778, -0.025089964270591736, -0.03997539356350899, 0.013156415894627571, 0.000135212714667432, 0.012428865768015385, 0.016216561198234558, -0.04126659780740738]\n",
"[0.0019113451708108187, -0.008625439368188381, -0.0531032420694828, -0.0018436014652252197, -0.01818147301673889, 0.010310115292668343, -0.008867680095136166, -0.021067561581730843]\n"
]
}
],
"source": [
"embed = QianfanEmbeddingsEndpoint(qianfan_ak='xxx', \n",
" qianfan_sk='xxx',\n",
"embed = QianfanEmbeddingsEndpoint(\n",
" model=\"bge_large_zh\",\n",
" endpoint=\"bge_large_zh\")\n",
" endpoint=\"bge_large_zh\"\n",
" )\n",
"\n",
"res = embed.embed_documents([\"hi\", \"world\"])"
"res = embed.embed_documents([\"hi\", \"world\"])\n",
"for r in res :\n",
" print(r[:8])"
]
}
],