-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (46 loc) · 1.56 KB
/
app.py
File metadata and controls
65 lines (46 loc) · 1.56 KB
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
import chainlit as cl
import ollama
import time
@cl.on_chat_start
async def start_chat():
cl.user_session.set(
"interaction",
[
{
"role": "system",
"content": "You are a helpful assistant.",
}
],
)
msg = cl.Message(content="")
start_message = "Hello, I'm your 100% local ChatGPT powered by Google Deepmind's Gemma 3. How can I help you today?"
for token in start_message:
await msg.stream_token(token)
time.sleep(0.005)
await msg.send()
@cl.step(type="tool")
async def tool(input_message, image=None):
interaction = cl.user_session.get("interaction")
if image:
interaction.append({"role": "user",
"content": input_message,
"images": image})
else:
interaction.append({"role": "user",
"content": input_message})
response = ollama.chat(model="gemma3:4b",
messages=interaction)
interaction.append({"role": "assistant",
"content": response.message.content})
return response
@cl.on_message
async def main(message: cl.Message):
images = [file for file in message.elements if "image" in file.mime]
if images:
tool_res = await tool(message.content, [i.path for i in images])
else:
tool_res = await tool(message.content)
msg = cl.Message(content="")
for token in tool_res.message.content:
await msg.stream_token(token)
await msg.send()