Skip to content

Commit

Permalink
Merge pull request #819 from PyThaiNLP/add-pythainlp-chat
Browse files Browse the repository at this point in the history
Add pythainlp.chat and WangChanGLM to pythainlp.generate
  • Loading branch information
wannaphong committed Jul 14, 2023
2 parents e4f9c9b + 484c034 commit 797432f
Show file tree
Hide file tree
Showing 9 changed files with 2,113 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/api/chat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. currentmodule:: pythainlp.chat

pythainlp.chat
==============

.. autoclass:: ChatBotModel
:members:
4 changes: 3 additions & 1 deletion docs/api/generate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ Modules
:members:
.. autoclass:: Trigram
:members:
.. autofunction:: pythainlp.generate.thai2fit.gen_sentence
.. autofunction:: pythainlp.generate.thai2fit.gen_sentence
.. autoclass:: pythainlp.generate.wangchanglm.WangChanGLM
:members:
1 change: 1 addition & 0 deletions docs/notes/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ where ``extras`` can be
- ``transformers_ud`` (to support transformers_ud engine)
- ``dependency_parsing`` (to support dependency parsing with all engine)
- ``coreference_resolution`` (to support coreference esolution with all engine)
- ``wangchanglm`` (to support wangchanglm model)
- ``wsd`` (to support pythainlp.wsd)
- ``full`` (install everything)

Expand Down
236 changes: 236 additions & 0 deletions notebooks/test-chat.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3ad128a6-2959-431f-b5ff-d9e15761c9c0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from pythainlp.chat.core import ChatBotModel\n",
"import torch"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "35760aec-f47a-4d33-ad1c-a8230194180c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"chatbot = ChatBotModel()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "99129184-3a9f-4871-bfb9-ce611e80ff55",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Setting ds_accelerator to cuda (auto detect)\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "54dd6a2c6afa41959bfb11ec98b30562",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/98 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"chatbot.load_model(device=\"cpu\",torch_dtype=torch.bfloat16)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2f43d3a7-049c-47b4-86a4-7d89cc61f09d",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'ยินดีที่ได้รู้จัก'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chatbot.chat(\"สวัสดี\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "736436d8-2c49-466c-ad3e-006b4827274b",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[('สวัสดี', 'ยินดีที่ได้รู้จัก')]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chatbot.history"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "cd9bdae5-93c5-4990-ad18-1171148d0a8c",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'ดอกไม้เป็นสิ่งสวยงามที่ธรรมชาติมอบให้มนุษย์ มนุษย์จึงได้นําดอกไม้มาประดับตกแต่งบ้าน สวน ร้านค้า ร้านอาหาร และสถานที่ต่างๆ มากมาย ปัจจุบันมีดอกไม้หลากหลายชนิดให้เราได้เลือกปลูก แต่ละชนิดมีวิธีการปลูกที่แตกต่างกันไป ขึ้นอยู่กับชนิดของดอกไม้นั้นๆ ดังนี้'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chatbot.chat(\"แนะนำวิธีปลูกดอกไม้ให้หน่อย\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "73bd8293-f4ad-4bb1-b912-9542cf404d74",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[('สวัสดี', 'ยินดีที่ได้รู้จัก'),\n",
" ('แนะนำวิธีปลูกดอกไม้ให้หน่อย',\n",
" 'ดอกไม้เป็นสิ่งสวยงามที่ธรรมชาติมอบให้มนุษย์ มนุษย์จึงได้นําดอกไม้มาประดับตกแต่งบ้าน สวน ร้านค้า ร้านอาหาร และสถานที่ต่างๆ มากมาย ปัจจุบันมีดอกไม้หลากหลายชนิดให้เราได้เลือกปลูก แต่ละชนิดมีวิธีการปลูกที่แตกต่างกันไป ขึ้นอยู่กับชนิดของดอกไม้นั้นๆ ดังนี้')]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chatbot.history"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3e0bac2d-7ba4-4bf7-aff1-2b47f0223e41",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'ดอกกุหลาบ ปลูกง่าย ดูแลง่าย ให้ดอกดก ทนทาน นิยมปลูกเป็นไม้กระถาง ดินที่ใช้ปลูกควรเป็นดินร่วนซุยผสมปุ๋ยคอกหรือปุ่ยหมัก รดน้ําให้ชุ่มแต่อย่าแฉะเกินไป แสงแดดจัด อุณหภูมิที่เหมาะสมคือ 20-25 องศาเซลเซียส ปุยหมักจะใช้ปุ้ยหอยหรือแกลบดิบก็ได้ ต้นกุหลายจะออกดอกในช่วงเดือนมีนาคม-เมษายน'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chatbot.chat(\"ขอรายละเอียดเพิ่มเติมหน่อย\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c047ee41-3850-4d0d-88af-6e659b2b2acb",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[('สวัสดี', 'ยินดีที่ได้รู้จัก'),\n",
" ('แนะนำวิธีปลูกดอกไม้ให้หน่อย',\n",
" 'ดอกไม้เป็นสิ่งสวยงามที่ธรรมชาติมอบให้มนุษย์ มนุษย์จึงได้นําดอกไม้มาประดับตกแต่งบ้าน สวน ร้านค้า ร้านอาหาร และสถานที่ต่างๆ มากมาย ปัจจุบันมีดอกไม้หลากหลายชนิดให้เราได้เลือกปลูก แต่ละชนิดมีวิธีการปลูกที่แตกต่างกันไป ขึ้นอยู่กับชนิดของดอกไม้นั้นๆ ดังนี้'),\n",
" ('ขอรายละเอียดเพิ่มเติมหน่อย',\n",
" 'ดอกกุหลาบ ปลูกง่าย ดูแลง่าย ให้ดอกดก ทนทาน นิยมปลูกเป็นไม้กระถาง ดินที่ใช้ปลูกควรเป็นดินร่วนซุยผสมปุ๋ยคอกหรือปุ่ยหมัก รดน้ําให้ชุ่มแต่อย่าแฉะเกินไป แสงแดดจัด อุณหภูมิที่เหมาะสมคือ 20-25 องศาเซลเซียส ปุยหมักจะใช้ปุ้ยหอยหรือแกลบดิบก็ได้ ต้นกุหลายจะออกดอกในช่วงเดือนมีนาคม-เมษายน')]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chatbot.history"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b77ea2f-df5a-442b-a5ae-537511479d8e",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 797432f

Please sign in to comment.