Upload 2 files
Browse files- app.py +48 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
from openai import AsyncOpenAI
|
3 |
+
|
4 |
+
import chainlit as cl
|
5 |
+
|
6 |
+
client = AsyncOpenAI(
|
7 |
+
api_key="ollama",
|
8 |
+
base_url="https://mahadih534-own-ollama-api-server.hf.space/v1/"
|
9 |
+
)
|
10 |
+
|
11 |
+
@cl.on_message
|
12 |
+
async def on_message(msg: cl.Message):
|
13 |
+
start = time.time()
|
14 |
+
stream = await client.chat.completions.create(
|
15 |
+
model="deepseek-r1:1.5b",
|
16 |
+
messages=[
|
17 |
+
{"role": "system", "content": "You are an helpful assistant"},
|
18 |
+
*cl.chat_context.to_openai()
|
19 |
+
],
|
20 |
+
stream=True
|
21 |
+
)
|
22 |
+
|
23 |
+
thinking = False
|
24 |
+
|
25 |
+
# Streaming the thinking
|
26 |
+
async with cl.Step(name="Thinking") as thinking_step:
|
27 |
+
final_answer = cl.Message(content="")
|
28 |
+
|
29 |
+
async for chunk in stream:
|
30 |
+
delta = chunk.choices[0].delta
|
31 |
+
|
32 |
+
if delta.content == "<think>":
|
33 |
+
thinking = True
|
34 |
+
continue
|
35 |
+
|
36 |
+
if delta.content == "</think>":
|
37 |
+
thinking = False
|
38 |
+
thought_for = round(time.time() - start)
|
39 |
+
thinking_step.name = f"Thought for {thought_for}s"
|
40 |
+
await thinking_step.update()
|
41 |
+
continue
|
42 |
+
|
43 |
+
if thinking:
|
44 |
+
await thinking_step.stream_token(delta.content)
|
45 |
+
else:
|
46 |
+
await final_answer.stream_token(delta.content)
|
47 |
+
|
48 |
+
await final_answer.send()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
chainlit
|
2 |
+
openai
|