-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-model-switcher.py
130 lines (104 loc) · 3.53 KB
/
chat-model-switcher.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import panel as pn
from openai import OpenAI, Stream
from openai.types.chat import ChatCompletionChunk
from openai.types.chat.chat_completion_chunk import Choice
from panel.chat import ChatInterface
from chat_utils.core import get_timestamp, get_models
from chat_utils.fs import prepare_folders, save_jsonl
# https://panel.holoviz.org/
pn.extension()
prepare_folders(["chats"])
AVATAR_USER = "https://api.iconify.design/carbon:user.svg"
AVATAR_BOT = "https://api.iconify.design/carbon:chat-bot.svg"
AVATAR_SYSTEM = "https://api.iconify.design/carbon:ibm-event-automation.svg"
chat_memory = []
chat_memory_file = f"chats/chat_memory_{get_timestamp()}.jsonl"
sidebar_selector = pn.widgets.Select(
name="Model",
description="Select the model to use",
options=get_models()
)
sidebar_keep_memory = pn.widgets.Checkbox(
name="Keep memory on model switch",
value=True,
)
client = OpenAI(
api_key=os.getenv("GROQ_API_KEY"),
base_url="https://api.groq.com/openai/v1"
)
model = "llama3-8b-8192"
def model_selected(event):
if not sidebar_keep_memory.value:
global chat_memory
chat_memory = []
selected_model = sidebar_selector.value
chat_interface.send(
f"""
Model changed to `{selected_model['model']}`. This model has \
`{selected_model['context_window']}` tokens context window.
""",
avatar=AVATAR_SYSTEM,
user='System',
respond=False
)
if selected_model["provider"] == "groq":
client.base_url = selected_model["base_url"]
client.api_key = os.getenv("GROQ_API_KEY")
if selected_model["provider"] == "openai":
client.base_url = selected_model["base_url"]
client.api_key = os.getenv("OPENAI_API_KEY")
sidebar_selector.param.watch(model_selected, "value")
def get_response(user_input: str, user, instance: ChatInterface):
chat_memory.append({"role": "user", "content": user_input})
response: Stream[ChatCompletionChunk] = client.chat.completions.create(
model=sidebar_selector.value["model"],
messages=chat_memory,
stream=True
)
replies = ""
try:
for chunk in response:
if chunk.choices:
choice: Choice = chunk.choices[0]
text = choice.delta.content
if text:
replies += text
yield {
"avatar": AVATAR_BOT,
"user": "Assistant",
"object": replies
}
if 'end' in chunk and chunk.end:
break # Exit the loop if the stream is marked as ended
finally:
# Append the collected replies as a single entry to the chat history
if replies: # Ensure we do not add empty responses
chat_memory.append({"role": "assistant", "content": replies})
response.close() # Ensure the stream is properly closed after processing
save_jsonl(chat_memory_file, chat_memory)
chat_interface = ChatInterface(
scroll=True,
user="You",
avatar=AVATAR_USER,
callback=get_response,
callback_user="Assistant",
show_clear=False,
show_undo=False,
show_rerun=False,
)
ui = pn.template.FastListTemplate(
site="Demo App",
title=f"Chat",
header_background="black",
sidebar=[
sidebar_selector,
sidebar_keep_memory
],
main=[chat_interface]
)
if __name__ == "__main__":
chat_interface.show()
else:
chat_interface.send("Hello, how can I help you?", user='Assistant', respond=False)
ui.servable()