Add Baichuan chat model (#11923)

Description: A large language models developed by Baichuan Intelligent
Technology,https://www.baichuan-ai.com/home
Issue: None
Dependencies: None
Tag maintainer:
Twitter handle:
This commit is contained in:
John Mai
2023-10-17 13:30:57 -05:00
committed by GitHub
parent 9ecb7240a4
commit 3fb5e4d185
3 changed files with 433 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Baichuan Chat\n",
"\n",
"Baichuan chat models API by Baichuan Intelligent Technology. For more information, see [https://platform.baichuan-ai.com/docs/api](https://platform.baichuan-ai.com/docs/api)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2023-10-17T15:14:24.186131Z",
"start_time": "2023-10-17T15:14:23.831767Z"
}
},
"outputs": [],
"source": [
"from langchain.chat_models import ChatBaichuan\n",
"from langchain.schema import HumanMessage"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2023-10-17T15:14:24.191123Z",
"start_time": "2023-10-17T15:14:24.186330Z"
}
},
"outputs": [],
"source": [
"chat = ChatBaichuan(\n",
" baichuan_api_key='YOUR_API_KEY',\n",
" baichuan_secret_key='YOUR_SECRET_KEY'\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or you can set `api_key` and `secret_key` in your environment variables\n",
"```bash\n",
"export BAICHUAN_API_KEY=YOUR_API_KEY\n",
"export BAICHUAN_SECRET_KEY=YOUR_SECRET_KEY\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2023-10-17T15:14:25.853218Z",
"start_time": "2023-10-17T15:14:24.192408Z"
}
},
"outputs": [
{
"data": {
"text/plain": "AIMessage(content='首先我们需要确定闰年的二月有多少天。闰年的二月有29天。\\n\\n然后我们可以计算你的月薪\\n\\n日薪 = 月薪 / (当月天数)\\n\\n所以你的月薪 = 日薪 * 当月天数\\n\\n将数值代入公式\\n\\n月薪 = 8元/天 * 29天 = 232元\\n\\n因此你在闰年的二月的月薪是232元。')"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat([\n",
" HumanMessage(content='我日薪8块钱请问在闰年的二月我月薪多少')\n",
"])"
]
},
{
"cell_type": "markdown",
"source": [
"## For ChatBaichuan with Streaming"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [],
"source": [
"chat = ChatBaichuan(\n",
" baichuan_api_key='YOUR_API_KEY',\n",
" baichuan_secret_key='YOUR_SECRET_KEY',\n",
" streaming=True\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-10-17T15:14:25.870044Z",
"start_time": "2023-10-17T15:14:25.863381Z"
}
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"data": {
"text/plain": "AIMessageChunk(content='首先我们需要确定闰年的二月有多少天。闰年的二月有29天。\\n\\n然后我们可以计算你的月薪\\n\\n日薪 = 月薪 / (当月天数)\\n\\n所以你的月薪 = 日薪 * 当月天数\\n\\n将数值代入公式\\n\\n月薪 = 8元/天 * 29天 = 232元\\n\\n因此你在闰年的二月的月薪是232元。')"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat([\n",
" HumanMessage(content='我日薪8块钱请问在闰年的二月我月薪多少')\n",
"])"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-10-17T15:14:27.153546Z",
"start_time": "2023-10-17T15:14:25.868470Z"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}