Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit58498fb

Browse files
committed
add 01
1 parent4cee218 commit58498fb

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

‎01.GPTChat/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##01.GPTChat
2+
3+
4+
5+
##run
6+
7+
python >= 3.9
8+
9+
```pip3 install -r requirements.txt```
10+
11+
```bash
12+
python3 gptchat.py
13+
14+
```
15+
16+
then open url:http://localhost:8888
17+
18+

‎01.GPTChat/gptchat.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
"""
3+
code from https://github.com/hyperdiv/hyperdiv-apps/tree/main/gpt-chatbot
4+
5+
python >= 3.9
6+
7+
pip3 install -r requirements.txt
8+
9+
"""
10+
11+
importsys
12+
importos
13+
importopenai
14+
importhyperdivashd
15+
16+
17+
18+
api_key=os.environ.get("OPENAI_API_KEY","<your OpenAI API key if not set as env var>")
19+
20+
21+
22+
defadd_message(role,content,state,gpt_model):
23+
state.messages+= (
24+
dict(role=role,content=content,id=state.message_id,gpt_model=gpt_model),
25+
)
26+
state.message_id+=1
27+
28+
29+
defrequest(gpt_model,state):
30+
response=openai.chat.completions.create(
31+
model=gpt_model,
32+
messages=[dict(role=m["role"],content=m["content"])forminstate.messages],
33+
temperature=0,
34+
stream=True,
35+
)
36+
37+
forchunkinresponse:
38+
message=chunk.choices[0].delta.content
39+
state.current_reply+=str(message)
40+
41+
add_message("assistant",state.current_reply,state,gpt_model)
42+
state.current_reply=""
43+
44+
45+
defrender_user_message(content,gpt_model):
46+
withhd.hbox(
47+
align="center",
48+
padding=0.5,
49+
border_radius="medium",
50+
background_color="neutral-50",
51+
font_color="neutral-600",
52+
justify="space-between",
53+
):
54+
withhd.hbox(gap=0.5,align="center"):
55+
hd.icon("chevron-right",shrink=0)
56+
hd.text(content)
57+
hd.badge(gpt_model)
58+
59+
60+
defmain():
61+
state=hd.state(messages=(),current_reply="",gpt_model="gpt-4",message_id=0)
62+
63+
task=hd.task()
64+
65+
template=hd.template(title="GPT Chatbot",sidebar=False)
66+
67+
withtemplate.body:
68+
# Render the messages so far, if any.
69+
iflen(state.messages)>0:
70+
# We use a vertical-reverse box to render the messages, so
71+
# they naturally stay 'stuck' to the bottom and auto-scroll up.
72+
withhd.box(direction="vertical-reverse",gap=1.5,vertical_scroll=True):
73+
# The current reply is the most recent message
74+
ifstate.current_reply:
75+
hd.markdown(state.current_reply)
76+
77+
# Render the rest of the messages in reverse. The
78+
# `vertical-reverse` direction will re-reverse them,
79+
# rendering them in expected order.
80+
foreinreversed(state.messages):
81+
withhd.scope(e["id"]):
82+
ife["role"]=="system":
83+
continue
84+
ife["role"]=="user":
85+
render_user_message(e["content"],e["gpt_model"])
86+
else:
87+
hd.markdown(e["content"])
88+
89+
withhd.box(align="center",gap=1.5):
90+
# Render the input form.
91+
withhd.form(direction="horizontal",width="100%")asform:
92+
withhd.box(grow=1):
93+
prompt=form.text_input(
94+
placeholder="Talk to the AI",
95+
autofocus=True,
96+
disabled=task.running,
97+
name="prompt",
98+
)
99+
100+
model=form.select(
101+
options=("gpt-3.5-turbo","gpt-4","gpt-4-1106-preview"),
102+
value="gpt-4",
103+
name="gpt-model",
104+
)
105+
106+
ifform.submitted:
107+
add_message("user",prompt.value,state,model.value)
108+
prompt.reset()
109+
task.rerun(request,model.value,state)
110+
111+
# Render a small button that when clicked, resets the message history.
112+
iflen(state.messages)>0:
113+
ifhd.button(
114+
"Start Over",size="small",variant="text",disabled=task.running
115+
).clicked:
116+
state.messages= ()
117+
118+
119+
hd.run(main)

‎01.GPTChat/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
openai
2+
hyperdiv

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp