vortex123 commited on
Commit
60e3903
·
verified ·
1 Parent(s): b041fc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -8
app.py CHANGED
@@ -1,17 +1,25 @@
1
  import gradio as gr
 
2
  import time
3
  import re
4
  import os
5
 
6
- # Available models
7
- MODEL = "models/mistralai/Mixtral-8x7B-Instruct-v0.1"
 
 
8
 
9
  # Sambanova API base URL
10
  API_BASE = "https://api.sambanova.ai/v1"
11
 
12
- def create_client():
13
- """Creates an client instance."""
14
- return
 
 
 
 
 
15
 
16
  def chat_with_ai(message, chat_history, system_prompt):
17
  """Formats the chat history for the API call."""
@@ -24,13 +32,15 @@ def chat_with_ai(message, chat_history, system_prompt):
24
  messages.append({"role": "user", "content": message})
25
  return messages
26
 
27
- def respond(message, chat_history, system_prompt, thinking_budget):
28
  """Sends the message to the API and gets the response."""
 
29
  messages = chat_with_ai(message, chat_history, system_prompt.format(budget=thinking_budget))
30
  start_time = time.time()
31
 
32
  try:
33
- response =
 
34
  thinking_time = time.time() - start_time
35
  return response, thinking_time
36
  except Exception as e:
@@ -39,4 +49,96 @@ def respond(message, chat_history, system_prompt, thinking_budget):
39
 
40
  def parse_response(response):
41
  """Parses the response from the API."""
42
- answer_match = re.search(r'<answer>(.*?)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import openai
3
  import time
4
  import re
5
  import os
6
 
7
+ MODELS = [
8
+ "Mixtral-8x7B-Instruct-v0.1"
9
+ ]
10
+
11
 
12
  # Sambanova API base URL
13
  API_BASE = "https://api.sambanova.ai/v1"
14
 
15
+ def create_client(api_key=None):
16
+ """Creates an OpenAI client instance."""
17
+ if api_key:
18
+ openai.api_key = api_key
19
+ else:
20
+ openai.api_key = os.getenv("API_KEY")
21
+
22
+ return openai.OpenAI(api_key=openai.api_key, base_url=API_BASE)
23
 
24
  def chat_with_ai(message, chat_history, system_prompt):
25
  """Formats the chat history for the API call."""
 
32
  messages.append({"role": "user", "content": message})
33
  return messages
34
 
35
+ def respond(message, chat_history, model, system_prompt, thinking_budget, api_key):
36
  """Sends the message to the API and gets the response."""
37
+ client = create_client(api_key)
38
  messages = chat_with_ai(message, chat_history, system_prompt.format(budget=thinking_budget))
39
  start_time = time.time()
40
 
41
  try:
42
+ completion = client.chat.completions.create(model=model, messages=messages)
43
+ response = completion.choices[0].message.content
44
  thinking_time = time.time() - start_time
45
  return response, thinking_time
46
  except Exception as e:
 
49
 
50
  def parse_response(response):
51
  """Parses the response from the API."""
52
+ answer_match = re.search(r'<answer>(.*?)</answer>', response, re.DOTALL)
53
+ reflection_match = re.search(r'<reflection>(.*?)</reflection>', response, re.DOTALL)
54
+
55
+ answer = answer_match.group(1).strip() if answer_match else ""
56
+ reflection = reflection_match.group(1).strip() if reflection_match else ""
57
+ steps = re.findall(r'<step>(.*?)</step>', response, re.DOTALL)
58
+
59
+ if answer == "":
60
+ return response, "", ""
61
+
62
+ return answer, reflection, steps
63
+
64
+ def generate(message, history, model, system_prompt, thinking_budget, api_key):
65
+ """Generates the chatbot response."""
66
+ response, thinking_time = respond(message, history, model, system_prompt, thinking_budget, api_key)
67
+
68
+ if response.startswith("Error:"):
69
+ return history + [({"role": "system", "content": response},)], ""
70
+
71
+ answer, reflection, steps = parse_response(response)
72
+
73
+ messages = []
74
+ messages.append({"role": "user", "content": message})
75
+
76
+ formatted_steps = [f"Step {i}: {step}" for i, step in enumerate(steps, 1)]
77
+ all_steps = "\n".join(formatted_steps) + f"\n\nReflection: {reflection}"
78
+
79
+ messages.append({"role": "assistant", "content": all_steps, "metadata": {"title": f"Thinking Time: {thinking_time:.2f} sec"}})
80
+ messages.append({"role": "assistant", "content": answer})
81
+
82
+ return history + messages, ""
83
+
84
+ # Define the default system prompt
85
+ DEFAULT_SYSTEM_PROMPT = """
86
+ You are a helpful assistant in normal conversation.
87
+ When given a problem to solve, you are an expert problem-solving assistant.
88
+ Your task is to provide a detailed, step-by-step solution to a given question.
89
+ Follow these instructions carefully:
90
+ 1. Read the given question carefully and reset counter between <count> and </count> to {budget}
91
+ 2. Generate a detailed, logical step-by-step solution.
92
+ 3. Enclose each step of your solution within <step> and </step> tags.
93
+ 4. You are allowed to use at most {budget} steps (starting budget),
94
+ keep track of it by counting down within tags <count> </count>,
95
+ STOP GENERATING MORE STEPS when hitting 0, you don't have to use all of them.
96
+ 5. Do a self-reflection when you are unsure about how to proceed,
97
+ based on the self-reflection and reward, decides whether you need to return
98
+ to the previous steps.
99
+ 6. After completing the solution steps, reorganize and synthesize the steps
100
+ into the final answer within <answer> and </answer> tags.
101
+ 7. Provide a critical, honest and subjective self-evaluation of your reasoning
102
+ process within <reflection> and </reflection> tags.
103
+ 8. Assign a quality score to your solution as a float between 0.0 (lowest
104
+ quality) and 1.0 (highest quality), enclosed in <reward> and </reward> tags.
105
+ Example format:
106
+ <count> [starting budget] </count>
107
+ <step> [Content of step 1] </step>
108
+ <count> [remaining budget] </count>
109
+ <step> [Content of step 2] </step>
110
+ <reflection> [Evaluation of the steps so far] </reflection>
111
+ <reward> [Float between 0.0 and 1.0] </reward>
112
+ <count> [remaining budget] </count>
113
+ <step> [Content of step 3 or Content of some previous step] </step>
114
+ <count> [remaining budget] </count>
115
+ ...
116
+ <step> [Content of final step] </step>
117
+ <count> [remaining budget] </count>
118
+ <answer> [Final Answer] </answer> (must give final answer in this format)
119
+ <reflection> [Evaluation of the solution] </reflection>
120
+ <reward> [Float between 0.0 and 1.0] </reward>
121
+ """
122
+
123
+ with gr.Blocks() as demo:
124
+ gr.Markdown("# Llama3.1-Instruct-O1")
125
+ gr.Markdown("[Powered by SambaNova Cloud, Get Your API Key Here](https://cloud.sambanova.ai/apis)")
126
+
127
+ with gr.Row():
128
+ api_key = gr.Textbox(label="API Key", type="password", placeholder="(Optional) Enter your API key here for more availability")
129
+
130
+ with gr.Row():
131
+ model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0])
132
+ thinking_budget = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Thinking Budget", info="maximum times a model can think")
133
+
134
+ chatbot = gr.Chatbot(label="Chat", show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel", type="messages")
135
+
136
+ msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
137
+
138
+ gr.Button("Clear Chat").click(lambda: ([], ""), inputs=None, outputs=[chatbot, msg])
139
+
140
+ system_prompt = gr.Textbox(label="System Prompt", value=DEFAULT_SYSTEM_PROMPT, lines=15, interactive=True)
141
+
142
+ msg.submit(generate, inputs=[msg, chatbot, model, system_prompt, thinking_budget, api_key], outputs=[chatbot, msg])
143
+
144
+ demo.launch(share=True, show_api=False)