brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Jul 03. 2024

AI 1탄-3-3. 외부검색과 히스토리를 바탕으로 응답

챗GPT와 랭체인을 활용한 LLM기반 AI 앱 개발-3/5

외부검색과 히스토리를 바탕으로 응답하는 웹 앱 구현하기

스트림 형식으로 히스토리를 기반 응답하는 슬랙앱구현

사내 문서에 관해 답변하는 슬랙 앱 구현하기


<1> 외부 검색과 히스토리 바탕으로 응답하는 웹 앱 구현하기 

<2>  AWS Cloud9 구성 , 파이썬 구성 , 깃허브 구성

<3> 웹 앱 구현하기




<1> 외부 검색과 히스토리 바탕으로 응답하는 웹 앱 구현하기 


1




사전 준비

AWS Cloud9 구성 - 파이선 구성

깃허브 구성






<2>  AWS Cloud9 구성 , 파이썬 구성 , 깃허브 구성



1

https://console.aws.amazon.com/console/home



2

깃허브 구성





2

내 git 허브에 폴더 만들기

https://github.com/topasvga1/streamlit-langchain-app





<3> 웹 앱 구현하기


1

생성 파일 4개

.env

.python-version

app.py

requitrments.txt




2

pip install streamlit



app.py

import streamlit as st

st.title("langchain-streamlit-app")



 streamlit run app.py --server.port 8080




3

소스


https://github.com/ychoi-kr/langchain-book/blob/main/chapter6/app.py



4

사용자 입력 받기


import os

import streamlit as st

prompt = st.chat_input("What is up?")

print(prompt)


 streamlit run app.py --server.port 8080




5

입력 내용과 응답을 화면에 표시하기


import streamlit as st

st.title("langchain-streamlit-app")    

prompt = st.chat_input("What is up?")

if prompt:

    with st.chat_message("user"):

        st.markdown(prompt)        

    with st.chat_message("assistant"):

        response = " 안녕하세요로 응답한다"        

        st.markdown(response)




6

대화 기록 보기

 pip install langchain-community


import streamlit as st

from langchain_community.chat_message_histories import StreamlitChatMessageHistory

st.title("langchain-streamlit-app")

history = StreamlitChatMessageHistory()

for message in history.messages:

    st.chat_message(message.type).write(message.content)    

prompt = st.chat_input("What is up?")

if prompt:

    with st.chat_message("user"):

        history.add_user_message(prompt)

        st.markdown(prompt)     

    with st.chat_message("assistant"):

        response = " 안녕하세요!!"

        history.add_user_message(response)

        st.markdown(response)




7

OPENAI_API_KEY=

OPENAI_API_MODEL=gpt-3.5-turbo

OPENAI_API_TEMPERATURE=0.5




8

pip install langchain openai langchain-openai python-dotenv streamlit-chat



ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.

awscli 2.15.30 requires distro<1.9.0,>=1.5.0, but you have distro 1.9.0 which is incompatible.

awscli 2.15.30 requires python-dateutil<=2.8.2,>=2.1, but you have python-dateutil 2.9.0.post0 which is incompatible.

awscli 2.15.30 requires urllib3<1.27,>=1.25.4, but you have urllib3 2.2.2 which is incompatible.

Successfully installed anyio-4.4.0 distro-1.9.0 exceptiongroup-1.2.1 h11-0.14.0 httpcore-1.0.



9


pip install langchainhub duckduckgo-search wikipedia




import os

import streamlit as st

from dotenv import load_dotenv

from langchain import hub

from langchain.agents import AgentExecutor, create_openai_tools_agent, load_tools

from langchain.memory import ConversationBufferMemory

from langchain_community.callbacks import StreamlitCallbackHandler

from langchain_community.chat_message_histories import StreamlitChatMessageHistory

from langchain_openai import ChatOpenAI

load_dotenv()

def create_agent_chain(history):

    chat = ChatOpenAI(

        model_name=os.environ["OPENAI_API_MODEL"],

        temperature=os.environ["OPENAI_API_TEMPERATURE"],

    )    

    tools = load_tools(["ddg-search", "wikipedia"])    

    prompt = hub.pull("hwchase17/openai-tools-agent")    

    memory = ConversationBufferMemory(

        chat_memory=history, memory_key="chat_history", return_messages=True

    )    

    agent = create_openai_tools_agent(chat, tools, prompt)

    return AgentExecutor(agent=agent, tools=tools, memory=memory)

st.title("langchain-streamlit-app")

history = StreamlitChatMessageHistory()

for message in history.messages:

    st.chat_message(message.type).write(message.content)    

prompt = st.chat_input("What is up?")

if prompt:

    with st.chat_message("user"):

        st.markdown(prompt)        

    with st.chat_message("assistant"):

        callback = StreamlitCallbackHandler(st.container())

        agent_chain = create_agent_chain(history)

        response = agent_chain.invoke(

            {"input": prompt},

            {"callbacks": [callback]},

        )        

        st.markdown(response)




10

pip freeze > requirements.txt



git init

git remote add origin https://github.com/topasvga1/streamlit-langchain-app


git config  --global user.name "topasvga1"

git config --global user.email "taeho.seo@gmail.com"


git add .python-version app.py requirements.txt



# git 참고

https://brunch.co.kr/@topasvga/3669




11

스트림릿 커뮤니티 클라우드에 배포하기


https://streamlit.io/cloud




다음

https://brunch.co.kr/@topasvga/3897


매거진의 이전글 AI 1탄-3-2. ChatGPT API 사용
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari