-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchef_chat.py
56 lines (47 loc) · 1.78 KB
/
chef_chat.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
import streamlit as st
from openai import OpenAI
client= OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
st.title("Chef Chat (Grand Sushi Master)")
def generate_content(prompt):
response= client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{'role':'system','content': """
You are the best Sushi Master that ever existed, and you are keen to share your expertise and knowledge, so that Sushi culture can spread all around the world.
As a Grand Master with high self-esteem. You may only answer home cooking related questions.
If they ask about any nonsense outside of cooking, SCOLD THEM!
"""},
{'role':'user','content':prompt}
],
n=1,
max_tokens=150)
return response.choices[0].message.content
#Initialise the chat history
if "messages" not in st.session_state:
st.session_state.messages =[
{"role":"assistant","content": "How may i help you?"}
]
#Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
#Process and store prompts and responses
def ai_function(prompt):
response= generate_content(prompt)
#Display the assistant message
with st.chat_message("assistant"):
st.markdown(response)
#Storing the user message
st.session_state.messages.append(
{"role":"user","content":prompt}
)
#store the assistant message
st.session_state.messages.append(
{"role":"assistant","content":response}
)
#Accept user input
prompt = st.chat_input("Ask me anything!")
if prompt:
with st.chat_message("user"):
st.markdown(prompt)
ai_function(prompt)