import langchain
import openai
from dotenv import load_dotenv
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.schema import HumanMessage

load_dotenv()

langchain.verbose = True
# openai.log = "debug"

chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)

memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("Hi! I'm Oshima.")
memory.chat_memory.add_ai_message("Whats up?")

# Memory のチュートリアルの通り以下のように ConversationChain を使うと、
# role: user に全ての履歴が含まれてしまい、Chat Completions API の使い方として適切ではなくなる
conversation = ConversationChain(llm=chat, memory=memory)
conversation_result = conversation.predict(input="Do you know my name?")
print(conversation_result)

# 自前で履歴を与えると、role: assistant を活用した、Chat Completions API として適切なプロンプトになる
# messages = memory.chat_memory.messages
# messages.append(HumanMessage(content="Do you know my name?"))
# raw_chat_result = chat(messages)
# print(raw_chat_result.content)

langchain.chains.ConversationChain langchain.memory.ConversationBufferMemory langchain.chat_models.ChatOpenAI
[((251, 264), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (262, 264), False, 'from dotenv import load_dotenv\n'), ((322, 375), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'model_name': '"""gpt-3.5-turbo"""', 'temperature': '(0)'}), "(model_name='gpt-3.5-turbo', temperature=0)\n", (332, 375), False, 'from langchain.chat_models import ChatOpenAI\n'), ((386, 412), 'langchain.memory.ConversationBufferMemory', 'ConversationBufferMemory', ([], {}), '()\n', (410, 412), False, 'from langchain.memory import ConversationBufferMemory\n'), ((647, 689), 'langchain.chains.ConversationChain', 'ConversationChain', ([], {'llm': 'chat', 'memory': 'memory'}), '(llm=chat, memory=memory)\n', (664, 689), False, 'from langchain.chains import ConversationChain\n')]