見出し画像

LangChainのChatbotプロンプトを理解して日本語を出力するように誘導する

LangChainはLLMを使ったAIサービスを繋げ、チャットボットを作ったりReActフレームワークに従ったエージェントを簡単に作ることができるサービスです。

LangChainが用意しているエージェント`AgentType.CONVERSATIONAL_REACT_DESCRIPTION`は日本語で聞いても英語が返ってきたりします。これはエージェントに設定されているPromptが英語である理由が大きいです(もしかしたらGPT側が今後賢くなって言語の問題は解消されるかもしれませんが)。

単語だけ入れると英語で返ってくる

実際にどのようなプロンプトを入れて動作しているか、日本語に訳したカスタマイズを試してみました。

AgentType.CONVERSATIONAL_REACT_DESCRIPTION

"""An agent designed to hold a conversation in addition to using tools."""

https://python.langchain.com/en/stable/modules/agents/agents/examples/conversational_agent.html

会話用にデザインされ、会話の履歴と外部ツールを利用可能にデザインされたエージェントです。

Prompt

プロンプトの中身はこちらで見ることができます。

# flake8: noqa
PREFIX = """Assistant is a large language model trained by OpenAI.

Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.

TOOLS:
------

Assistant has access to the following tools:"""
FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:

```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
```

When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:

```
Thought: Do I need to use a tool? No
{ai_prefix}: [your response here]
```"""

SUFFIX = """Begin!

Previous conversation history:
{chat_history}

New input: {input}
{agent_scratchpad}"""

PREFIX、FORMAT_INSTRUCTIONS、SUFFIXの3つのPromptが設定されています。これはLangChainのinitialize_agent関数でagent_kwargsという変数で与えることができます。

日本語で出力するためにSUFFIXを変えてみます。

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
from langchain.chains.conversation.memory import ConversationBufferMemory


agent_kwargs = {
"suffix": """開始!ここからの会話は全て日本語で行われる。

以前のチャット履歴
{chat_history}

新しいインプット: {input}
{agent_scratchpad}""",
}

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    memory=memory,
    agent_kwargs=agent_kwargs,
    verbose=True,
)

日本語が返ってくるようになりました。

agent.run("クリスマス")


クリスマスは1225日に祝われる、キリスト教の祝日です。多くの人々は家族や友人と一緒に過ごし、贈り物を交換したり特別な食事を楽しんだりします。また、クリスマスツリーやイルミネーションなどの飾り付けも行われます。


FORMAT_INSTRUCTIONSも変えたくなりますが、このフォーマットはToolが使う上で重要なため、変数名や構造を変えるとParse errorが発生したりします。使用言語を誘導するだけならSuffixを変えるくらいが良いようです。