File size: 11,113 Bytes
37086cb
 
 
 
 
b106933
 
f57de53
 
37086cb
f57de53
 
 
 
 
 
 
37086cb
 
f57de53
37086cb
 
 
f1891ea
f57de53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b106933
f57de53
 
 
b106933
 
f57de53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a251098
f57de53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37086cb
 
42b2938
 
 
 
f57de53
42b2938
f1891ea
 
 
 
 
 
 
 
 
42b2938
f57de53
42b2938
 
 
 
db85900
 
 
 
 
 
f57de53
 
d87d5a8
42b2938
 
 
f57de53
37086cb
f57de53
 
 
 
db85900
f57de53
 
 
37086cb
f57de53
 
37086cb
 
f57de53
 
37086cb
f57de53
37086cb
 
f57de53
597e9c5
37086cb
f57de53
 
37086cb
f57de53
 
a447294
f57de53
 
37086cb
 
 
f57de53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b106933
f57de53
 
 
 
 
 
 
 
 
 
 
37086cb
f57de53
37086cb
 
 
f57de53
37086cb
f57de53
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
# 財政部財政資訊中心 江信宗

import gradio as gr
import openai
from zhconv_rs import zhconv
import time
import re
import os

MODELS = [
    "Meta-Llama-3.1-405B-Instruct",
    "Meta-Llama-3.1-70B-Instruct",
    "Meta-Llama-3.1-8B-Instruct"
]

API_BASE = "https://api.sambanova.ai/v1"

def create_client(api_key=None):
    """Creates an OpenAI client instance."""
    if api_key:
        openai.api_key = api_key
    else:
        openai.api_key = os.getenv("YOUR_API_KEY")
    return openai.OpenAI(api_key=openai.api_key, base_url=API_BASE)

def chat_with_ai(message, chat_history, system_prompt):
    """Formats the chat history for the API call."""
    # 初始化訊息列表,首先新增系統提示詞
    messages = [{"role": "system", "content": system_prompt}]
    # 遍歷聊天歷史,將使用者和AI機器人的對話新增到訊息列表中
    for tup in chat_history:
        # 獲取字典的第一個鍵(通常是使用者的訊息)
        first_key = list(tup.keys())[0]
        # 獲取字典的最後一個鍵(通常是AI機器人的回應)
        last_key = list(tup.keys())[-1]
        # 將使用者的訊息新增到messages列表中
        messages.append({"role": "user", "content": tup[first_key]})
        # 將AI機器人的回應新增到messages列表中
        messages.append({"role": "assistant", "content": tup[last_key]})
    # 新增當前使用者的訊息
    messages.append({"role": "user", "content": message})
    return messages

def respond(message, chat_history, model, system_prompt, thinking_budget, api_key):
    """Sends the message to the API and gets the response."""
    # 建立OpenAI客戶端
    client = create_client(api_key)
    # 格式化聊天歷史
    messages = chat_with_ai(message, chat_history, system_prompt.format(budget=thinking_budget))
    # 記錄開始時間
    start_time = time.time()
    try:
        # 呼叫API獲取回應
        completion = client.chat.completions.create(model=model, messages=messages)
        response = completion.choices[0].message.content
        # 計算思考時間
        thinking_time = time.time() - start_time
        return response, thinking_time
    except Exception as e:
        # 捕獲並返回錯誤資訊
        error_message = f"Error: {str(e)}"
        return error_message, time.time() - start_time

def parse_response(response):
    """Parses the response from the API."""
    # 使用正規表示式提取回答部分
    answer_match = re.search(r'<answer>(.*?)</answer>', response, re.DOTALL)
    # 使用正規表示式提取反思部分
    reflection_match = re.search(r'<reflection>(.*?)</reflection>', response, re.DOTALL)
    # 提取答案和反思內容,如果沒有匹配則設爲空字串
    answer = answer_match.group(1).strip() if answer_match else ""
    reflection = reflection_match.group(1).strip() if reflection_match else ""
    # 提取所有步驟
    steps = re.findall(r'<step>(.*?)</step>', response, re.DOTALL)
    # 如果沒有提取到答案,則返回原始回應
    if answer == "":
        return response, "", ""
    return answer, reflection, steps

def generate(message, history, model, system_prompt, thinking_budget, api_key):
    """Generates the chatbot response."""
    # 獲取AI回應和思考時間
    response, thinking_time = respond(message, history, model, system_prompt, thinking_budget, api_key)
    # 如果回應是錯誤資訊,直接返回
    if response.startswith("Error:"):
        return history + [({"role": "system", "content": response},)], ""
    # 解析AI的回應
    answer, reflection, steps = parse_response(response)
    answer = zhconv(answer, "zh-tw")
    reflection = zhconv(reflection, "zh-tw")
    # 初始化訊息列表
    messages = []
    # 新增使用者的輸入
    messages.append({"role": "user", "content": f'<div style="text-align: left;">{message}</div>'})
    # 格式化AI回應的步驟和反思
    formatted_steps = [f"Step {i}{zhconv(step, 'zh-tw')}" for i, step in enumerate(steps, 1)]
    all_steps = "<br>".join(formatted_steps) + f"<br><br>Reflection:{reflection}"
    # 新增AI的推理過程和思考時間
    messages.append({
        "role": "assistant",
        "content": f'<div style="text-align: left;">{all_steps}</div>',
        "metadata": {"title": f"推理過程時間: {thinking_time:.2f} 秒"}
    })
    # 新增AI的最終答案
    messages.append({"role": "assistant", "content": f"<b>{answer}</b>"})
    # 返回更新後的歷史記錄和空字串(用於清空輸入框)
    return history + messages, ""

DEFAULT_SYSTEM_PROMPT = """You are a helpful assistant in normal conversation.

When given a problem to solve, you are an expert problem-solving assistant.

Your task is to provide a detailed, step-by-step solution to a given question.



Follow these instructions carefully:

1. What are the key elements of the problem? What information is given, and what is being asked? Read the given question carefully and reset counter between <count> and </count> to {budget}

2. How can we break down the problem into smaller, manageable parts? What relationships exist between these components? Generate a detailed, logical step-by-step solution.

3. What are possible solutions or explanations for this problem? Can we think of at least three different approaches? Enclose each step of your solution within <step> and </step> tags.

4. You are allowed to use at most {budget} steps (starting budget),

   keep track of it by counting down within tags <count> </count>,

   STOP GENERATING MORE STEPS when hitting 0, you don't have to use all of them.

5. How can we test each hypothesis? What evidence supports or contradicts each one? Are there any potential biases in our reasoning? Do a self-reflection when you are unsure about how to proceed,

   based on the self-reflection and reward, decides whether you need to return

   to the previous steps.

6. Based on our evaluation, what is the most likely solution or explanation? After completing the solution steps, reorganize and synthesize the steps

   into the final answer within <answer> and </answer> tags.

7. What have we learned from this reasoning process? Are there any areas where our reasoning could be improved? Provide a critical, honest and subjective self-evaluation of your reasoning

   process within <reflection> and </reflection> tags.

8. Assign a quality score to your solution as a float between 0.0 (lowest

   quality) and 1.0 (highest quality), enclosed in <reward> and </reward> tags.



Example format:

<count> [starting budget] </count>

<step> [Content of step 1] </step>

<count> [remaining budget] </count>

<step> [Content of step 2] </step>

<reflection> [Evaluation of the steps so far] </reflection>

<reward> [Float between 0.0 and 1.0] </reward>

<count> [remaining budget] </count>

<step> [Content of step 3 or Content of some previous step] </step>

<count> [remaining budget] </count>

...

<step>  [Content of final step] </step>

<count> [remaining budget] </count>

<answer> [Final Answer] </answer> (must give final answer in this format)

<reflection> [Evaluation of the solution] </reflection>

<reward> [Float between 0.0 and 1.0] </reward>

"""

custom_css = """

.center-aligned {

    text-align: center !important;

    color: #ff4081;

    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);

    margin-bottom: -5px !important;

}

.gr-input, .gr-box, .gr-dropdown {

    border-radius: 10px !important;

    border: 2px solid #ff4081 !important;

    margin: 0 !important;

}

.gr-input:focus, .gr-box:focus, .gr-dropdown:focus {

    border-color: #f50057 !important;

    box-shadow: 0 0 0 2px rgba(245,0,87,0.2) !important;

}

.input-background {

    background-color: #ffe0b2 !important;

    padding: 15px !important;

    border-radius: 10px !important;

    margin: 0 !important;

}

.input-background textarea {

    font-size: 18px !important;

    background-color: #ffffff;

    border: 1px solid #f0f8ff;

    border-radius: 8px;

}

.api-background {

    background-color: #FFCFB3 !important;

    padding: 10px !important;

    border-radius: 10px !important;

    margin: 0 !important;

}

.custom-button {

    border-radius: 10px !important;

    background-color: #333333 !important;

    color: white !important;

    font-weight: bold !important;

    transition: all 0.3s ease !important;

}

.custom-button:hover {

    background-color: #000000 !important;

    transform: scale(1.05);

}

.pink-bg {

    background-color: #ff4081 !important;

    border-radius: 10px !important;

}

.pink-bg label, .pink-bg .label-wrap {

    color: white !important;

}

.pink-bg textarea {

    color: black !important;

}

.user-message .message.user {

    background-color: #FFF4B5 !important;

    border-radius: 10px !important;

    padding: 10px !important;

    margin: -8px 0px -8px -20px !important;

}

.assistant-message .message.bot {

    background-color: #B7E0FF !important;

    border-radius: 10px !important;

    padding: 10px !important;

    margin: -8px 0px -8px -20px !important;

}

"""

with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
    gr.Markdown("# 🏹 Multi-Chain Reasoning using Llama-3.1-405B-Instruct. Deployed by 江信宗 🏹", elem_classes="center-aligned")
    chatbot = gr.Chatbot(
        label="Chat",
        show_label=False,
        show_share_button=False,
        show_copy_button=True,
        likeable=True,
        layout="panel",
        type="messages",
        height="auto-max",
        elem_classes=["user-message", "assistant-message"],
        container=True
    )
    msg = gr.Textbox(label="請輸入您的問題:", placeholder="輸入完成後直接按 Enter 開始執行......", autofocus=True, max_lines=10, elem_classes="pink-bg")
    with gr.Row():
        model = gr.Dropdown(choices=MODELS, label="選擇模型", value=MODELS[0], elem_classes="input-background")
        thinking_budget = gr.Slider(minimum=1, maximum=100, value=40, step=1, label="思維規劃", info="模型所能進行的最大思考次數", elem_classes="input-background")
        api_key = gr.Textbox(label="API Key", type="password", placeholder="API authentication key for large language models", elem_classes="api-background")
    clear_button = gr.Button("清除聊天記錄", elem_classes="custom-button")
    clear_button.click(
        lambda: ([], "", gr.Info("已成功清除聊天記錄,歡迎繼續提問....")),
        inputs=None,
        outputs=[chatbot, msg]
    )
    system_prompt = gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, visible=False)
    msg.submit(generate, inputs=[msg, chatbot, model, system_prompt, thinking_budget, api_key], outputs=[chatbot, msg])

demo.load(js=None)

if __name__ == "__main__":
    if "SPACE_ID" in os.environ:
        demo.launch()
    else:
        demo.launch(share=True, show_api=False)