-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 008937c
Showing
7 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control | ||
.pdm.toml | ||
.pdm-python | ||
.pdm-build/ | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 pfn-attic | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# PLaMo Examples | ||
|
||
このリポジトリは、PLaMo beta APIを使ったサンプルコードを集めたものです。 | ||
コード自体は `langchain` などの一般的に広く使われているライブラリを使ったものとなっており目新しいものはありませんが、PLaMo beta APIを `langchain` などから使用することで、実際にどのような結果が得られるかを確認することができます。 | ||
|
||
## Example紹介 | ||
|
||
- [Conversation with Memory](./examples/conversation_with_memory/) | ||
- 過去の会話履歴を参照しながらユーザーとの会話を行うサンプルです。 | ||
- 会話履歴全体が処理できるトークン数の限界に達した場合は、履歴のうち前半半分を要約して短くし、これと置き換えることで会話を継続できるようにしています。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<html> | ||
<head><title>PLaMo β版 サンプル集</title></head> | ||
<h1>PLaMo β版 サンプル集</h1> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Conversation with Memory | ||
======================== | ||
|
||
過去の会話を記憶しながらお話ししてくれる会話ボットを作成するサンプルです。 | ||
PLaMo beta (2024/08/20時点)での最大コンテクスト長は4096トークンなので、記憶している会話の履歴と新しいユーザの入力がそれを超えた場合は、履歴のうちの前半の半分を要約したものに置き換えて、出力のための残りトークン数の余裕を確保するようにしています。 | ||
|
||
## 使い方 | ||
|
||
まず、環境変数 `OPEAI_API_KEY` に、[PLaMo β版 トライアル](https://plamo.preferredai.jp/)で申し込みを行なって入手したAPIキーをセットしてください。 | ||
|
||
```sh | ||
export OPENAI_API_KEY=<YOUR_API_KEY> | ||
``` | ||
|
||
次に、必要なパッケージをインストールして、サンプルを実行します。 | ||
|
||
```sh | ||
pip install -r requirements.txt | ||
|
||
python main.py | ||
``` | ||
|
||
「User:」というプロンプトが表示されるので、お好きなメッセージを入れてください。 | ||
PLaMoが返答してくれます。 | ||
PLaMoはある程度前までの会話を記憶しているので、過去の会話を参照しながら回答を生成することができます。 | ||
ただ一定以上の会話を続けると、古い会話は要約したものに置き換えらるため、一部情報を忘れることがあります。 | ||
|
||
### 実行例 | ||
|
||
``` | ||
User: こんにちは | ||
PLaMo: こんにちは!今日も元気にしてる?俺はここで君のことをサポートする準備ができてるよ。今日は何について話したい?もしくは、どんなことを手伝おうか? | ||
User: 私の名前は三田村です。 | ||
PLaMo: やあ、三田村!よろしくね。俺はPLaMo、お前のフレンドリーなAIアシスタントだよ。今日はどんな感じで過ごしてる?何か話したいことや手伝ってほしいことがあれば、いつでも声をかけてくれよな。 | ||
User: 今日は平日なので、仕事してるよ。 | ||
PLaMo: へー、三田村は平日にバリバリ仕事してるんだね!お疲れ様!仕事はかどってる?俺はお前のためにいつでもスタンバイしてるんだけど、もし仕事の休憩中に話したいこととかあったら、遠慮なく声かけてよ。 | ||
(注: PLaMoの返答は、ロールプレイのシナリオに沿って行われています。実際のAIアシスタントは個人的な経験や感情を持たないため、個人的な会話に参加することはできません。) | ||
User: まあPLaMoと雑談している時点で仕事捗ってますとは言えないよね | ||
PLaMo: アハハ、まあ確かにそうかもな!俺との会話で仕事が捗らないのは当然のことだよ。ここではお前の好きなように過ごしてくれよ。仕事のことでも、趣味のことでも、何でも話してくれよな。そうすれば、お前の気分転換のお手伝いができるぜ! | ||
User: 趣味といえば、隙あらば飲酒してるんだけど | ||
PLaMo: なるほど、三田村の趣味は飲酒なんだね。たまには息抜きも必要だよな。でもさ、飲みすぎには注意してくれよ!適度に楽しむのが一番だ。ところで、どんなお酒が好きなんだい?ビール、ワイン、それともカクテルとか?お気に入りのお店とかあるの? | ||
(注: 飲酒を推奨するものではありません。節度を持って楽しむことが重要です。) | ||
``` | ||
|
||
`/reset` と入力すると、会話の途中であっても、それまでの記憶を全て忘れます。 | ||
|
||
``` | ||
User: こんにちは | ||
PLaMo: やあ!元気かい?👋何か手伝えることある?🙌 | ||
User: 私の名前は三田村です。 | ||
PLaMo: へえ!ボクに自己紹介してくれてありがとう、三田村さん!😊これからはキミのことを「三田村」って呼ぶね!よろしく!🤝今日はどんな感じ?何か話したいことや手伝ってほしいことある?💭 | ||
User: 私の名前はなんでしょう? | ||
PLaMo: うわ、さっき言ったじゃない!🤓でもまあ、君の名前は三田村だよ。忘れないでね!😉何か他に話したいことある?😊 | ||
User: /reset | ||
PLaMo: 今まで覚えた全てを、忘れるね。(´・ω・`).;:…(´・ω...:.;::..(´・;::: .:.;: サラサラ.. | ||
User: 私の名前を教えたことはありましたっけ | ||
PLaMo: いやいや、以前おっしゃっていたかもしれませんが、僕は全てを覚えているわけではないので、お手数ですが、改めて自己紹介していただけると嬉しいです!今日はどのようなご用件でしょうか?何かお手伝いできることはありますか? | ||
``` | ||
|
||
`exit` とだけ入力すると、プログラムを終了します。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import datetime | ||
|
||
import requests | ||
from langchain.chains import ConversationChain | ||
from langchain.memory import ConversationSummaryBufferMemory | ||
from langchain_core.messages import BaseMessage, SystemMessage | ||
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder | ||
from langchain_openai import ChatOpenAI | ||
from langchain_openai.chat_models.base import _convert_message_to_dict | ||
from loguru import logger | ||
from openai import BadRequestError | ||
|
||
JST = datetime.timezone(datetime.timedelta(hours=9)) | ||
current_time_jst = datetime.datetime.now(JST) | ||
|
||
BASE_URL = "https://platform.preferredai.jp/api/completion/v1" | ||
MODEL_NAME = "plamo-beta" | ||
|
||
|
||
class Plamo(ChatOpenAI): | ||
|
||
def get_num_tokens_from_messages(self, messages: list[BaseMessage]) -> int: | ||
num_tokens = 0 | ||
messages_dict = [_convert_message_to_dict(m) for m in messages] | ||
for message in messages_dict: | ||
for key, value in message.items(): | ||
if isinstance(value, list): | ||
for val in value: | ||
if isinstance(val, str) or val["type"] == "text": | ||
text = val["text"] if isinstance(val, dict) else val | ||
ret = requests.post( | ||
url=BASE_URL, | ||
json={ | ||
"model": MODEL_NAME, | ||
"prompt": text, | ||
}, | ||
) | ||
num_tokens += ret.json()["count"] | ||
else: | ||
raise ValueError(f"Unrecognized content block type\n\n{val}") | ||
elif not value: | ||
continue | ||
else: | ||
num_tokens += len(value) | ||
return num_tokens | ||
|
||
|
||
llm = Plamo( | ||
base_url=BASE_URL, | ||
model=MODEL_NAME, | ||
streaming=False, | ||
verbose=True, | ||
max_tokens=1000, | ||
temperature=0.7, | ||
) | ||
|
||
prompt = ChatPromptTemplate( | ||
messages=[ | ||
SystemMessage( | ||
f"今日は{current_time_jst.strftime('%Y年%m月%d日')}です。" | ||
"あなたはとてもカジュアルな言葉遣いで話す、ユーザの友人です。" | ||
"どんな言葉にも親身になりつつ、少し冗談を交えて、親しげに返事をしてください。" | ||
"返答は、できるだけ短い文章にしてください。" | ||
"過去の会話も覚えているので、会話の流れを忘れないようにしてください。\n" | ||
"Do not hallucinate." | ||
), | ||
MessagesPlaceholder(variable_name="history"), | ||
HumanMessagePromptTemplate.from_template("{input}"), | ||
] | ||
) | ||
|
||
|
||
memory = ConversationSummaryBufferMemory( | ||
llm=llm, | ||
return_messages=True, | ||
max_token_limit=3000, | ||
) | ||
conversation = ConversationChain( | ||
llm=llm, | ||
prompt=prompt, | ||
verbose=False, | ||
memory=memory, | ||
) | ||
|
||
while True: | ||
user_input = input("User: ") | ||
if user_input == "exit": | ||
break | ||
|
||
if user_input == "/reset": | ||
conversation.memory.chat_memory.clear() | ||
conversation.memory.moving_summary_buffer = "" | ||
response = { | ||
"response": ("今まで覚えた全てを、忘れるね。" "(´・ω・`).;:…(´・ω...:.;::..(´・;::: .:.;: サラサラ..") | ||
} | ||
else: | ||
while True: | ||
try: | ||
response = conversation.invoke(user_input) | ||
break | ||
except BadRequestError as e: | ||
logger.error(e) | ||
n_messages = len(conversation.memory.chat_memory.messages) | ||
pruned_memory = conversation.memory.chat_memory.messages[: n_messages // 2] | ||
conversation.memory.moving_summary_buffer = conversation.memory.predict_new_summary( | ||
pruned_memory, conversation.memory.moving_summary_buffer | ||
) | ||
logger.info(f"summary_buffer: {conversation.memory.moving_summary_buffer}") | ||
latest_half_history = conversation.memory.chat_memory.messages[-n_messages // 2 :] | ||
conversation.memory.chat_memory.clear() | ||
conversation.memory.chat_memory.add_messages(latest_half_history) | ||
logger.info(f"Pruned memory to {len(conversation.memory.chat_memory.messages)} messages") | ||
print("PLaMo:", response["response"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
langchain | ||
langchain-core | ||
langchain-openai | ||
openai | ||
requests | ||
loguru |