FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
docs/python/api-examples-source/chat.llm.py at main · pypae/docs · GitHub
pypae
/
docs
Public
forked from
streamlit/docs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
docs
/
python
/
api-examples-source
/
chat.llm.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
66 lines (57 loc) · 2.58 KB
Breadcrumbs
docs
/
python
/
api-examples-source
/
chat.llm.py
Copy path
File metadata and controls
66 lines (57 loc) · 2.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
streamlit
as
st
from
openai
import
OpenAI
st
.
title
(
"ChatGPT-like clone"
)
with
st
.
expander
(
"ℹ️ Disclaimer"
):
st
.
caption
(
"""We appreciate your engagement! Please note, this demo is designed to
process a maximum of 10 interactions and may be unavailable if too many
people use the service concurrently. Thank you for your understanding.
"""
)
client
=
OpenAI
(
api_key
=
st
.
secrets
[
"OPENAI_API_KEY"
])
if
"openai_model"
not
in
st
.
session_state
:
st
.
session_state
[
"openai_model"
]
=
"gpt-3.5-turbo"
if
"messages"
not
in
st
.
session_state
:
st
.
session_state
.
messages
=
[]
if
"max_messages"
not
in
st
.
session_state
:
# Counting both user and assistant messages, so 10 rounds of conversation
st
.
session_state
.
max_messages
=
20
for
message
in
st
.
session_state
.
messages
:
with
st
.
chat_message
(
message
[
"role"
]):
st
.
markdown
(
message
[
"content"
])
if
len
(
st
.
session_state
.
messages
)
>=
st
.
session_state
.
max_messages
:
st
.
info
(
"""Notice: The maximum message limit for this demo version has been reached. We value your interest!
We encourage you to experience further interactions by building your own application with instructions
from Streamlit's [Build a basic LLM chat app](https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps)
tutorial. Thank you for your understanding."""
)
else
:
if
prompt
:=
st
.
chat_input
(
"What is up?"
):
st
.
session_state
.
messages
.
append
({
"role"
:
"user"
,
"content"
:
prompt
})
with
st
.
chat_message
(
"user"
):
st
.
markdown
(
prompt
)
with
st
.
chat_message
(
"assistant"
):
try
:
stream
=
client
.
chat
.
completions
.
create
(
model
=
st
.
session_state
[
"openai_model"
],
messages
=
[
{
"role"
:
m
[
"role"
],
"content"
:
m
[
"content"
]}
for
m
in
st
.
session_state
.
messages
],
stream
=
True
,
)
response
=
st
.
write_stream
(
stream
)
st
.
session_state
.
messages
.
append
(
{
"role"
:
"assistant"
,
"content"
:
response
}
)
except
:
st
.
session_state
.
max_messages
=
len
(
st
.
session_state
.
messages
)
rate_limit_message
=
"""
Oops! Sorry, I can't talk now. Too many people have used
this service recently.
"""
st
.
session_state
.
messages
.
append
(
{
"role"
:
"assistant"
,
"content"
:
rate_limit_message
}
)
st
.
rerun
()
Back
|
FazBrowse Home
|
New Git URL