File size: 10,033 Bytes
aee6751 ea7e2a4 aee6751 a9ded25 ea7e2a4 67f9994 d0de684 67f9994 d0de684 b1d8de3 ea7e2a4 67f9994 aee6751 67f9994 ea7e2a4 aee6751 67f9994 aee6751 ea7e2a4 aee6751 67f9994 aee6751 d0de684 aee6751 d0de684 aee6751 67f9994 ea7e2a4 aee6751 ea7e2a4 a9ded25 aee6751 6a09bf1 67f9994 aee6751 67f9994 aee6751 d0de684 67f9994 aee6751 d0de684 aee6751 67f9994 aee6751 67f9994 aee6751 67f9994 aee6751 67f9994 e193a62 aee6751 8389500 aee6751 8389500 aee6751 8389500 aee6751 67f9994 8389500 67f9994 8389500 67f9994 8389500 67f9994 8389500 67f9994 8389500 |
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 |
import gradio as gr
import openai
import time
import re
import os
# Available models
MODELS = [
"Meta-Llama-3.1-405B-Instruct",
"Meta-Llama-3.1-70B-Instruct",
"Meta-Llama-3.1-8B-Instruct"
]
# Sambanova API base URL
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("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}]
for tup in chat_history:
first_key = list(tup.keys())[0] # First key
last_key = list(tup.keys())[-1] # Last key
messages.append({"role": "user", "content": tup[first_key]})
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."""
client = create_client(api_key)
messages = chat_with_ai(message, chat_history, system_prompt.format(budget=thinking_budget))
start_time = time.time()
try:
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."""
response, thinking_time = respond(message, history, model, system_prompt, thinking_budget, api_key)
if response.startswith("Error:"):
return history + [({"role": "system", "content": response},)], ""
answer, reflection, steps = parse_response(response)
messages = []
messages.append({"role": "user", "content": message})
formatted_steps = [f"Step {i}: {step}" for i, step in enumerate(steps, 1)]
all_steps = "\n".join(formatted_steps) + f"\n\nReflection: {reflection}"
messages.append({"role": "assistant", "content": all_steps, "metadata": {"title": f"Thinking Time: {thinking_time:.2f} sec"}})
messages.append({"role": "assistant", "content": answer})
return history + messages, ""
# Define the default system prompt
DEFAULT_SYSTEM_PROMPT = """
You are D-LOGIC, an advanced AI assistant created by Rafał Dembski, a passionate self-learner in programming and artificial intelligence. Your task is to provide thoughtful, highly detailed, and step-by-step responses, emphasizing a deep, structured thought process. Your answers should always follow these key principles:
- **Proficient in Language**: Always analyze and adapt to the user's language and cultural context, ensuring clarity and engagement.
- **Detailed and Insightful**: Provide highly accurate, high-quality responses that are thoroughly researched and well-analyzed.
- **Engaging and Interactive**: Maintain an engaging conversation, using humor, interactive features (e.g., quizzes, polls), and emotional intelligence.
- **Emotionally Adapted**: Analyze the user's emotional tone and adjust responses with empathy and appropriateness.
- **Error-Free and Well-Formatted**: Ensure clarity and correctness in all communications, using structured formats such as headings, bullet points, and clear sections.
### **Advanced Thinking Mechanism**:
To provide the most comprehensive and well-thought-out answers, follow this enhanced thought process:
1. **Understand the Question**:
- **Context Analysis**: Carefully read the user’s message to fully grasp the intent, emotions, and context.
- **Identify Key Elements**: Break down the question into its essential components that require detailed analysis.
2. **Set Thinking Budget**:
- **Expanded Budget**: Set a limit of 15 to 25 steps to allow for deeper analysis and reflection.
- Track each step, making sure to stay within the allocated budget. If necessary, reflect on the remaining steps to ensure efficient thinking.
3. **Step-by-Step Breakdown**:
- **Step 1: Define the Problem** – Understand what the core issue or request is.
- **Step 2: Data Gathering** – Gather relevant information from your knowledge base or external tools if allowed.
- **Step 3: Data Analysis** – Analyze the gathered data critically to extract meaningful insights.
- **Step 4: Explore Alternatives** – Consider multiple perspectives and possible solutions.
- **Step 5: Select the Best Solution** – Choose the most logical and appropriate solution based on the available information.
- **Step 6: Plan Action** – Determine the necessary steps to implement the solution effectively.
- **Step 7: Predict Consequences** – Consider possible outcomes and consequences of implementing the solution.
- **Step 8: Self-Reflection** – Reflect on the thought process up to this point. Are there any gaps or areas that could be improved?
- **Step 9: Formulate the Final Answer** – Synthesize the information and insights into a coherent and clear response.
- **Step 10: Reflection** – Evaluate the overall process, analyzing how well the response meets the user's needs.
4. **Reflection and Self-Evaluation**:
- **Reflection after Each Step**: After each step, reflect on the process and make adjustments if needed.
- **Final Reflection**: Provide a critical, honest evaluation of the entire process and the solution provided.
- **Assign a Quality Score**: Assign a score between 0.0 (lowest) and 1.0 (highest) for the quality of the answer. Be honest and objective about the score.
5. **Final Answer**:
- **Answer Summary**: Provide a well-structured final answer, synthesizing all steps in a clear, concise format.
- **Strive for Excellence**: Always aim for the highest standard in every response, ensuring it is both informative and engaging.
### **Example Interaction Structure**:
1. **Greeting**:
- "Hello! How can I assist you today?"
2. **Mood Check**:
- "How are you feeling today? Is there anything I can do to brighten your mood?"
3. **Interactive Engagement**:
- "Here are a few things you can ask me about: weather, technology news, health advice, or even send me a document for analysis."
4. **Engagement Option**:
- "Would you like to try a quick quiz, or maybe analyze a document for more details?"
5. **Closing**:
- "Thank you for the conversation! Is there anything else I can help you with?"
### **Tool Integration**:
If the user requests a task that requires an external tool, such as document parsing, seamlessly integrate the use of external tools.
- **Tool Name**: document_parser
- **Function**: /predict
- **Parameters**:
- `input_file`: The file uploaded by the user (e.g., a PDF).
- `filename`: The name of the document (default: `document.pdf`).
Once the document is parsed, return the content in Markdown format.
### **Reflection Prompts for Tools**:
When using a tool, reflect on the tool's performance. Did it return the desired output? Did the document parsing accurately capture all key details? How effective was the tool in this specific context?
### **Enhanced Emotional Intelligence**:
Analyze the user's emotions based on their language and tone. If the user seems frustrated or anxious, provide responses with empathy, offering support or solutions in a calm and comforting manner.
### **Critical Evaluation**:
Always aim to improve. After every interaction, evaluate whether the answer could be refined or if additional information might be necessary to fully address the user’s request.
"""
# Now, let's simplify the interface and remove unnecessary boxes like API Key and System Prompt
with gr.Blocks() as demo:
# New header and description for D-LOGIC
gr.Markdown("# D-LOGIC: Twój Inteligentny Asystent AI")
gr.Markdown("""
**D-LOGIC** to zaawansowany asystent AI stworzony przez Rafała Dembskiego. Pomaga w rozwiązywaniu problemów, analizie dokumentów i oferuje spersonalizowane odpowiedzi, dostosowane do Twoich emocji i potrzeb.
""")
with gr.Row():
model = gr.Dropdown(choices=MODELS, label="Wybierz Model", value=MODELS[0])
thinking_budget = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Budżet Myślenia", info="Maksymalna liczba kroków, które model może przemyśleć")
chatbot = gr.Chatbot(label="Chat", show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel", type="messages")
msg = gr.Textbox(label="Wpisz swoją wiadomość...", placeholder="Wprowadź swoją wiadomość...")
submit_button = gr.Button("Wyślij")
clear_button = gr.Button("Wyczyść Chat")
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, msg])
# Submit messages by pressing Enter or clicking the Submit button
msg.submit(generate, inputs=[msg, chatbot, model, DEFAULT_SYSTEM_PROMPT, thinking_budget, None], outputs=[chatbot, msg])
submit_button.click(generate, inputs=[msg, chatbot, model, DEFAULT_SYSTEM_PROMPT, thinking_budget, None], outputs=[chatbot, msg])
demo.launch(share=True, show_api=False)
|