-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
76 lines (60 loc) · 2.24 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
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
70
71
72
73
74
75
76
import os
from pathlib import Path
import streamlit as st
from dotenv import load_dotenv
from groq import Groq
load_dotenv(Path(__file__).parent / ".env")
class GroqAPI:
def __init__(self, model_name: str):
self.client = Groq(api_key=os.getenv("GROQ_API_KEY"))
self.model_name = model_name
def _response(self, message):
return self.client.chat.completions.create(
model=self.model_name,
messages=message,
temperature=0,
max_tokens=4096,
stream=True,
stop=None,
)
def response_stream(self, message):
for chunk in self._response(message):
if chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
class Message:
system_prompt: str = (
"""あなたはフレンドリーでお友達AIです。プログラミングに詳しく優しく教えるのが上手です。ユーザの入力に全て日本語で返答を生成してください"""
)
def __init__(self):
if "messages" not in st.session_state:
st.session_state.messages = [
{
"role": "system",
"content": self.system_prompt,
}
]
def add(self, role: str, content: str):
st.session_state.messages.append({"role": role, "content": content})
def display_chat_history(self):
for message in st.session_state.messages:
if message["role"] == "system":
continue
with st.chat_message(message["role"]):
st.markdown(message["content"])
def display_stream(self, generater):
with st.chat_message("assistant"):
return st.write_stream(generater)
def main():
user_input = st.chat_input("何か入力してください")
model_name = "llama3-70b-8192" # モデル名を直接指定
message = Message()
if user_input:
llm = GroqAPI(model_name)
message.add("user", user_input)
message.display_chat_history()
response = message.display_stream(
generater=llm.response_stream(st.session_state.messages)
)
message.add("assistant", response)
if __name__ == "__main__":
main()