-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
43 lines (36 loc) · 1.69 KB
/
app.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
import streamlit as st
from llama_index.llms.ollama import Ollama
from llama_index.core.llms import ChatMessage
# 使用モデルを選択します。
st.title('Ollama phi3')
if "llm" not in st.session_state:
st.session_state.llm = Ollama(model="phi3", request_timeout=30.0)
# Initialize chat history
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 prompt := st.chat_input("入力"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
def response_generator():
messages = [
ChatMessage(role="system", content="あなたは日本人の優秀なアシスタントです。"),
ChatMessage(role="user", content="日本語で答えることはできますか?"),
ChatMessage(role="assistant", content="はい、日本語で丁寧にお答えします。"),
]
for conversation in st.session_state.messages:
messages.append(ChatMessage(role=conversation["role"], content=conversation["content"]))
messages.append(ChatMessage(role="user", content=prompt))
response = st.session_state.llm.stream_chat(messages=messages)
for chunk in response:
yield chunk.delta
if not len(st.session_state.messages) ==0:
with st.chat_message("assistant"):
response = st.write_stream(response_generator())
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})