■ RunnableSequence 클래스의 invoke 메소드에서 MessagePlaceHloder 객체에 인자를 전달하는 방법을 보여준다.
※ OPENAI_API_KEY 환경 변수 값은 .env 파일에 정의한다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from dotenv import load_dotenv from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI load_dotenv() chatPromptTemplate = ChatPromptTemplate.from_messages( [ ("system" , "You are a helpful assistant. Answer all questions to the best of your ability."), ("placeholder", "{messages}") ] ) chatOpenAI = ChatOpenAI(model = "gpt-4o-mini") runnableSequence = chatPromptTemplate | chatOpenAI responseAIMessage = runnableSequence.invoke( { "messages" : [ ("human", "Translate this sentence from English to French : I love programming."), ("ai" , "J'adore la programmation." ), ("human", "What did you just say?" ) ] } ) print(responseAIMessage.content) """ I translated the sentence "I love programming" into French, which is "J'adore la programmation." """ |
▶ requirements.txt
1 2 3 |