■ OLLAMA의 LLAMA 3.2 3B 모델을 사용해 채팅하는 방법을 보여준다.
※ Ollama를 설치하고 llama3.2:3b 모델을 다운로드해서 실행해야 한다.
▶ 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import streamlit as st import requests import json st.set_page_config(page_title = "Llama Chat", page_icon = "🦙") def stream_ollama_response(prompt): OLLAMA_ENDPOINT = "http://localhost:11434/api/generate" payloadDictionary = { "model" : "llama3.2:3b", "prompt" : prompt, "stream" : True } try: response = requests.post(OLLAMA_ENDPOINT, json = payloadDictionary, stream = True) response.raise_for_status() fullResponseString = "" for line in response.iter_lines(): if line: jsonSttring = json.loads(line) responseString = jsonSttring.get("response", "") done = jsonSttring.get("done", False) fullResponseString += responseString yield fullResponseString, done except requests.exceptions.RequestException as exception: st.error(f"Ollama 서버 에러 : {str(exception)}") yield None, True st.title("Llama Chat") if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if userContent := st.chat_input("메시지를 입력해 주시기 바랍니다."): st.session_state.messages.append({"role" : "user", "content" : userContent}) with st.chat_message("user"): st.markdown(userContent) with st.chat_message("assistant"): response_placeholder = st.empty() for responseChunk, done in stream_ollama_response(userContent): if responseChunk is not None: response_placeholder.markdown(responseChunk) if done: st.session_state.messages.append({"role" : "assistant", "content" : responseChunk}) st.sidebar.markdown("## 실행 방법") st.sidebar.code(""" # Ollama 서버 시작 ollama serve # 새 터미널에서 앱 실행 streamlit run app.py """) |
▶ requirements.txt
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 35 36 37 38 39 40 41 42 43 44 |
altair==5.5.0 attrs==24.3.0 blinker==1.9.0 cachetools==5.5.0 certifi==2024.12.14 charset-normalizer==3.4.1 click==8.1.8 colorama==0.4.6 gitdb==4.0.11 GitPython==3.1.43 idna==3.10 Jinja2==3.1.5 jsonschema==4.23.0 jsonschema-specifications==2024.10.1 markdown-it-py==3.0.0 MarkupSafe==3.0.2 mdurl==0.1.2 narwhals==1.19.1 numpy==2.2.1 packaging==24.2 pandas==2.2.3 pillow==11.0.0 protobuf==5.29.2 pyarrow==18.1.0 pydeck==0.9.1 Pygments==2.18.0 python-dateutil==2.9.0.post0 pytz==2024.2 referencing==0.35.1 requests==2.32.3 rich==13.9.4 rpds-py==0.22.3 six==1.17.0 smmap==5.0.1 streamlit==1.41.1 tenacity==9.0.0 toml==0.10.2 tornado==6.4.2 typing_extensions==4.12.2 tzdata==2024.2 urllib3==2.3.0 watchdog==6.0.0 |
※ pip install streamlit 명령을 실행했다.