Lambda + DynamoDB + LangChainãçµã¿åããã¦å¤é¨æ å ±ã«å¯¾å¿ãããã£ããããããä½ã£ã¦ã¿ã¾ããã
æ§æ

Lambda
LangChainã®ããã±ã¼ã¸ã250MBãè¶ ãã¦ãã¾ãã®ã§ãã³ã³ããã¤ã¡ã¼ã¸ããLambda颿°ã使ããããã«ãã¾ãã
Dockerfile
FROM public.ecr.aws/lambda/python:3.10
# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}
# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}
RUN pip install --upgrade pip
# Install the specified packages
RUN pip install -r requirements.txt
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
requirements.txt
langchain==0.0.262 google-search-results==2.4.2 openai==0.27.8
lambda_function.py
from langchain import OpenAI,SerpAPIWrapper from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType from langchain.chat_models import ChatOpenAI from langchain.prompts import MessagesPlaceholder from langchain.memory import ConversationBufferMemory from langchain.memory.chat_message_histories import DynamoDBChatMessageHistory llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613") search = SerpAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events. You should ask targeted questions", ) ] agent_kwargs = { "extra_prompt_messages": [MessagesPlaceholder(variable_name="chat_history")], } def handler(event, context): session_id = event["session_id"] prompt = event["prompt"] message_history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id=session_id) memory = ConversationBufferMemory( memory_key="chat_history", chat_memory=message_history, return_messages=True ) agent = initialize_agent( tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True, agent_kwargs=agent_kwargs, memory=memory, ) output = agent.run(prompt) return { 'statusCode': 200, 'body': output }
OpenAI Functions Agent
ä»åã¯Agentã«OpenAI Functions Agentã使ç¨ãã¦ãã¾ããããã¯OpenAIã®Function callingã«å¯¾å¿ããAgentã§ãå人çã«å¾æ¥ã®Agentã«æ¯ã¹ã¦ããåä½ãå®å®ãã¦ããå°è±¡ãããã®ã§ãã¡ãã使ç¨ãã¾ãã
https://python.langchain.com/docs/modules/agents/agent_types/openai_functions_agentpython.langchain.com
llmã«ã¯Function callingã«å¯¾å¿ããgpt-3.5-turbo-0613以éã®ã¢ãã«ã使ç¨ããå¿
è¦ããããã¨ã«æ³¨æãã¦ãã ããã
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613") agent = initialize_agent( tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True, agent_kwargs=agent_kwargs, memory=memory, )
ä¼è©±å±¥æ´ã®ä¿æ
ä»åã¯Agentã使ç¨ãã¦ã¾ãããä½ãè¨å®ããªãã¨éå»ã®ã¡ãã»ã¼ã¸å±¥æ´ãä¿æãã¦ããã¾ãããããã§ä»åã¯DynamoDBChatMessageHistoryã使ã£ã¦å±¥æ´ãä¿æããããã«ãã¦ãã¾ãã ã¾ãsession_idãæå®ãããã¨ã§ãä»ã®äººã®ä¼è©±å±¥æ´ãæ··ãããªãããsession_idãã¨ã«å±¥æ´ãåãã¦ä¿åããããã«ãªã£ã¦ãã¾ãã
https://python.langchain.com/docs/integrations/memory/dynamodb_chat_message_historypython.langchain.com
message_history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id=session_id) agent_kwargs = { "extra_prompt_messages": [MessagesPlaceholder(variable_name="chat_history")], } memory = ConversationBufferMemory( memory_key="chat_history", chat_memory=message_history, return_messages=True )
å¤é¨æ å ±ã®åç §
ä»åã¯å¤é¨æ å ±ã®åç §ãã§ããããã«ãAgentã®Toolã«serpAPIã§Googleæ¤ç´¢ãè¡ãSerpAPIWrapper()ã使ç¨ãã¾ããserpAPIã®API_KEYãåå¾ããLambdaã®ç°å¢å¤æ°ã«SERPAPI_API_KEYã¨ãã¦å®ç¾©ãã¦ããã¾ãã
search = SerpAPIWrapper()
tools = [
Tool(
name="Search",
func=search.run,
description="useful for when you need to answer questions about current events. You should ask targeted questions",
)
]
DynamoDB
ãã£ããå±¥æ´ã®ä¿æã§ç¨ããDynamoDBã使ãã¾ãã
aws dynamodb create-table \
--table-name SessionTable \
--attribute-definitions AttributeName=sessionid,AttributeType=S \
--key-schema AttributeName=sessionid,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
Lambda颿°ã®å®è¡ãã¼ã«ã«DynamoDBã«å¯¾ããæ¸ãè¾¼ã¿ãåç §æ¨©éã追å ããå¿ è¦ãããã®ã§è¿½å ãã¦ããã¾ãã
åä½ç¢ºèª
Lambda颿°ãå®è¡ãã¦ãã£ãããã¦ã¿ã¾ãã ä»åã¯Googleæ¤ç´¢ãè¡ããªãã¨åçã§ããªãææ°ã®æ å ±ã«ã¤ãã¦è³ªåãã¦ã¿ã¾ãã


å®è¡ãã°
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m Invoking: `Search` with `2023å¹´ ç¾å¨ã®æ¥æ¬ã®é¦ç¸` [0m[36;1m[1;3mFumio Kishida is a Japanese politician serving as prime minister of Japan and president of the Liberal Democratic Party since 2021. A member of the House of Representatives, he previously served as Minister for Foreign Affairs from 2012 to 2017 and as acting Minister of Defense in 2017.[0m[32;1m[1;3m2023å¹´ç¾å¨ãæ¥æ¬ã®é¦ç¸ã¯å²¸ç°æéï¼ããã ãµã¿ãï¼æ°ã§ããå½¼ã¯2021年以éãæ¥æ¬ã®é¦ç¸ããã³èªç±æ°ä¸»å ã®å é¦ãåãã¦ãã¾ããå½¼ã¯è¡è°é¢ã®è°å¡ã§ããã2012å¹´ãã2017å¹´ã¾ã§å¤å大è£ã2017å¹´ã«ã¯è¨æã®é²è¡å¤§è£ãåãã¾ããã[0m [1m> Finished chain.[0m
å®è¡ãã°ããã2023å¹´ ç¾å¨ã®æ¥æ¬ã®é¦ç¸ãã§æ¤ç´¢ããããªããæ£ããåçãå°ãåºãã¦ãããã¨ããããã¾ãã
ãã£ããå±¥æ´ãä¿æãã¦ããã確èªãã¦ã¿ã¾ãããã


ã¡ããã¨éå»ã®ä¼è©±å±¥æ´ãè¦ãã¦ãããã¨ããããã¾ãã
ä»åº¦ã¯session_idãå¤ãã¦åã質åããã¦ã¿ã¾ãã


ä»åã¯ååã®è³ªåå 容ããã¼ãã¦ãã¾ãããããã«ããAgentãDynamoDBããä¼è©±å±¥æ´ãsession_idãã¨ã«åå¾ãã¦ãããã¨ã確èªã§ãã¾ããã
ã¾ã¨ã
ä»åã¯Lambda + DynamoDB + LangChainãçµã¿åããã¦å¤é¨æ å ±ã«å¯¾å¿ãããã£ãããããããµã¼ãã¬ã¹ã§ä½ã£ã¦ã¿ã¾ãããä»åã¯Lambda颿°ãå®è¡ãã¦ããã ãã§ãããSlackãLINEãªã©ã¨é£æºããããã«ãã¦ãé¢ç½ããã§ããLangChainã使ããã¨ã§ç°¡åã«ãã£ããããããå®è£ ã§ããã®ã§ãã²ã試ããã ããã