File size: 6,712 Bytes
302ee5a
 
 
 
 
 
 
 
 
 
 
 
 
0aec042
302ee5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ae264c
302ee5a
 
 
 
 
 
 
 
 
 
 
0aec042
302ee5a
 
4ae264c
302ee5a
 
 
 
 
 
 
0aec042
302ee5a
 
4ae264c
3fcc106
302ee5a
3fcc106
 
302ee5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aec042
 
 
 
 
302ee5a
 
 
 
 
 
 
0aec042
302ee5a
 
 
 
 
 
 
 
 
 
 
 
 
0aec042
302ee5a
 
 
 
 
 
 
 
 
 
 
 
0aec042
302ee5a
 
 
 
 
 
 
 
0aec042
 
 
302ee5a
 
 
4ae264c
 
0aec042
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import chainlit as cl
from openai import AuthenticationError

from aicore.logger import _logger
from aicore.config import LlmConfig
from aicore.const import STREAM_END_TOKEN, STREAM_START_TOKEN, REASONING_START_TOKEN, REASONING_STOP_TOKEN

from aicore.llm import Llm

from ulid import ulid
import asyncio
import time

from utils import MODELS_PROVIDERS_MAP, PROVIDERS_API_KEYS, REASONER_PROVIDERS_MAP, check_openai_api_key, trim_messages
from settings import PROFILES_SETTINGS

DEFAULT_REASONER_CONFIG = LlmConfig(
    provider="groq",
    api_key=PROVIDERS_API_KEYS.get("groq"),
    model="deepseek-r1-distill-llama-70b",
    temperature=0.5,
    max_tokens=1024
)

DEFAULT_LLM_CONFIG = {
    "Reasoner4All": LlmConfig(
        provider="mistral",
        api_key=PROVIDERS_API_KEYS.get("mistral"),
        model="mistral-small-latest",
        temperature=0,
        max_tokens=1024,
        reasoner=DEFAULT_REASONER_CONFIG
    ),
    "OpenAi": LlmConfig(
        provider="openai",
        api_key=PROVIDERS_API_KEYS.get("openai", ""),
        model="gpt-4o-mini",
        temperature=0,
        max_tokens=1024,
        reasoner=DEFAULT_REASONER_CONFIG
    )
}

@cl.set_chat_profiles
async def chat_profile():
    return [
        cl.ChatProfile(
            name="Reasoner4All",
            markdown_description="A deepseek-r1-distill-llama-70b powered Reasoner for your favourite open-source LLMs",
            icon="https://picsum.photos/200",
        ),
        cl.ChatProfile(
            name="OpenAi",
            markdown_description="A deepseek-r1-distill-llama-70b powered Reasoner for closed source LLMs",
            icon="https://picsum.photos/200",
        )
    ]
    
@cl.on_settings_update
async def setup_agent(settings):
    provider = MODELS_PROVIDERS_MAP.get(settings.get("Model"), "openai")
    llm_config = LlmConfig(
        provider=provider,
        api_key=PROVIDERS_API_KEYS.get(provider) or settings.get("Api Key"),
        model=settings.get("Model"),
        temperature=settings.get("Temperature"),
        max_tokens=settings.get("Max Tokens")
    )
    if settings.get("Use Reasoner"):
        reasoner_provder = REASONER_PROVIDERS_MAP.get(settings.get("Reasoner Model"), "openai")
        reasoner_config = LlmConfig(
            provider=reasoner_provder,
            api_key=PROVIDERS_API_KEYS.get(reasoner_provder) or settings.get("Reasoner Api Key"),
            model=settings.get("Reasoner Model"),
            temperature=settings.get("Reasoner Temperature"),
            max_tokens=settings.get("Reasoner Max Tokens")
        )
        llm_config.reasoner = reasoner_config
    
    llm = Llm.from_config(llm_config)
    llm.session_id = ulid()
    llm.system_prompt = settings.get("System Prompt")
    if llm.reasoner:
        llm.reasoner.system_prompt = settings.get("Reasoner System Prompt")

    cl.user_session.set(
        "llm", llm
    )

@cl.on_chat_start
async def start_chat():
    user_profile = cl.user_session.get("chat_profile")
    cl.user_session.set("history", [])
    llm_config = DEFAULT_LLM_CONFIG.get(user_profile)
    llm = Llm.from_config(llm_config)
    llm.session_id = ulid()
    cl.user_session.set(
        "llm", llm
    )
    
    settings = await cl.ChatSettings(
        PROFILES_SETTINGS.get(user_profile)
    ).send()
    

async def run_concurrent_tasks(llm, message):
    asyncio.create_task(llm.acomplete(message))
    asyncio.create_task(_logger.distribute())
    # Stream logger output while LLM is running
    while True:        
        async for chunk in _logger.get_session_logs(llm.session_id):
            yield chunk  # Yield each chunk directly

@cl.on_message
async def main(message: cl.Message):
    llm = cl.user_session.get("llm")
    if not llm.config.api_key:
        while True:
            api_key_msg = await cl.AskUserMessage(content="Please provide a valid api_key", timeout=10).send()
            if api_key_msg:
                api_key = api_key_msg.get("output")
                valid = check_openai_api_key(api_key)
                if valid:
                    await cl.Message(
                        content=f"Config updated with key.",
                    ).send()
                    llm.config.api_key = api_key
                    cl.user_session.set("llm", llm)
                    break
    
    start = time.time()
    thinking=False
    
    history = cl.user_session.get("history")
    history.append(message.content)
    history = trim_messages(history, llm.tokenizer)
    model_id = None
    try:
        if llm.reasoner is not None or llm.config.model in REASONER_PROVIDERS_MAP:
            # Streaming the thinking
            async with cl.Step(name=f"{llm.reasoner.config.provider} - {llm.reasoner.config.model} to think", type="llm") as thinking_step:
                msg = cl.Message(content="")
                async for chunk in run_concurrent_tasks(
                        llm,
                        message=history
                    ):
                    if chunk == STREAM_START_TOKEN:
                        continue
                    if chunk == REASONING_START_TOKEN:
                        thinking = True
                        continue
                        # chunk = " - *reasoning*\n```html\n"
                    if chunk == REASONING_STOP_TOKEN:
                        thinking = False
                        thought_for = round(time.time() - start)
                        thinking_step.name = f"{llm.reasoner.config.model} to think for {thought_for}s"
                        await thinking_step.update()
                        chunk = f"```{llm.config.model}```\n"
                        model_id = f"```{llm.config.model}```\n"

                    if chunk == STREAM_END_TOKEN:
                        break
                    
                    if thinking:
                        await thinking_step.stream_token(chunk)
                    else:
                        await msg.stream_token(chunk)
        else:
            msg = cl.Message(content="")
            async for chunk in run_concurrent_tasks(
                    llm,
                    message=history
                ):
                if chunk == STREAM_START_TOKEN:
                    continue

                if chunk == STREAM_END_TOKEN:
                        break
                
                await msg.stream_token(chunk)
            
        hst_msg = msg.content.replace(model_id, "") if model_id else msg.content
        history.append(hst_msg)
        await msg.send()
    
    except Exception as e:
        await cl.ErrorMessage("Internal Server Error").send()

### TODO add future todos, include support for images and pdf upload for conversation