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╗ █████╗ ██████╗ ██╗ ██╗
|
| 2 |
+
# ██╔══██╗ ██╔══██╗ ██╔══██╗ ██║ ██╔╝
|
| 3 |
+
# ██║ ██║ ███████║ ██████╔╝ █████╔╝
|
| 4 |
+
# ██║ ██║ ██╔══██║ ██╔══██╗ ██╔═██╗
|
| 5 |
+
# ██████╔╝ ██║ ██║ ██║ ██║ ██║ ██╗
|
| 6 |
+
# ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
|
| 7 |
+
#
|
| 8 |
+
# ███████╗ ███╗ ██╗ ██████╗ ████╗ ███╗ ██╗ ███████╗
|
| 9 |
+
# ██╔════╝ ████╗ ██║ ██╔════╝ ██╔╝ ████╗ ██║ ██╔════╝
|
| 10 |
+
# █████╗ ██╔██╗ ██║ ██║ ███╗ ██║ ██╔██╗ ██║ █████╗
|
| 11 |
+
# ██╔══╝ ██║╚██╗██║ ██║ ██║ ██║ ██║╚██╗██║ ██╔══╝
|
| 12 |
+
# ███████╗ ██║ ╚████║ ╚██████╔╝ ████╗ ██║ ╚████║ ███████╗
|
| 13 |
+
# ╚══════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═══╝ ╚═╝ ╚═══╝ ╚══════╝
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# This is a demo for the REN-AI architecture that will be used in our upcoming products. Please visit darkengine.ai to learn more!
|
| 17 |
+
|
| 18 |
+
import gradio as gr
|
| 19 |
+
import os
|
| 20 |
+
from groq import Groq
|
| 21 |
+
from datetime import datetime
|
| 22 |
+
import pytz
|
| 23 |
+
|
| 24 |
+
# We use Groq for our API demo to showcase models that can run locally on your device (just faster with the LPU engine, if you have $20k you can do this speed locally lmao)
|
| 25 |
+
# Local inference speeds will depend on your device (regarding the Dark Engine app or DarkOS for the REN-X3 robot)
|
| 26 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 27 |
+
# We are still updating the final system prompt architecture that will be shared in the future. For now, we store it as an secret variable on HF spaces
|
| 28 |
+
system_prompt = os.getenv("SYSTEM_PROMPT")
|
| 29 |
+
|
| 30 |
+
# Not calculated, just rounded. This can be updated easily...
|
| 31 |
+
MAX_TOKENS = 8192
|
| 32 |
+
RESPONSE_TOKENS = 1024
|
| 33 |
+
USER_INPUT_TOKENS = 250
|
| 34 |
+
TOKEN_THRESHOLD = 7000
|
| 35 |
+
|
| 36 |
+
# Ren needs timestamps
|
| 37 |
+
def get_current_central_time():
|
| 38 |
+
central = pytz.timezone('America/Chicago')
|
| 39 |
+
return datetime.now(central).strftime('%Y-%m-%d %H:%M:%S')
|
| 40 |
+
|
| 41 |
+
# Ren needs to have weights initiated and then updated between interaction-states
|
| 42 |
+
def create_system_message(weights, short=False):
|
| 43 |
+
current_time = get_current_central_time()
|
| 44 |
+
if short:
|
| 45 |
+
return f"Updated weights: {weights}, Current time: {current_time}"
|
| 46 |
+
return system_prompt.format(
|
| 47 |
+
personality=weights['personality'],
|
| 48 |
+
selfReflection=weights['selfReflection'],
|
| 49 |
+
abstractionLevel=weights['abstractionLevel'],
|
| 50 |
+
metaCognition=weights['metaCognition'],
|
| 51 |
+
current_time=current_time
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def calculate_token_count(text):
|
| 55 |
+
return len(text.split())
|
| 56 |
+
|
| 57 |
+
def trim_conversation_history(conversation_history, max_tokens):
|
| 58 |
+
total_tokens = sum(calculate_token_count(message['content']) for message in conversation_history)
|
| 59 |
+
while total_tokens > max_tokens and len(conversation_history) > 1:
|
| 60 |
+
removed_message = conversation_history.pop(0)
|
| 61 |
+
total_tokens -= calculate_token_count(removed_message['content'])
|
| 62 |
+
return conversation_history
|
| 63 |
+
|
| 64 |
+
async def predict(message, chat_history, personality, selfReflection, abstractionLevel, metaCognition):
|
| 65 |
+
current_weights = {
|
| 66 |
+
"personality": 100,
|
| 67 |
+
"selfReflection": 100,
|
| 68 |
+
"abstractionLevel": 100,
|
| 69 |
+
"metaCognition": 100
|
| 70 |
+
}
|
| 71 |
+
# This stores between clear-states to mimic long term memory with traditional database storage. We intend on adding Vector DB solutions soon!
|
| 72 |
+
# Might try pinecone for online (easy setup) or weaviate specifically for local stuff
|
| 73 |
+
conversation_history = []
|
| 74 |
+
|
| 75 |
+
# Reset command for conversation
|
| 76 |
+
# Other commands can be added to expand features, running functions to handle specific tasks
|
| 77 |
+
if message.lower() == "reset":
|
| 78 |
+
conversation_history = []
|
| 79 |
+
yield "Conversation history has been reset."
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
# As weights are updated, Ren should be notified
|
| 83 |
+
new_weights = {
|
| 84 |
+
"personality": personality,
|
| 85 |
+
"selfReflection": selfReflection,
|
| 86 |
+
"abstractionLevel": abstractionLevel,
|
| 87 |
+
"metaCognition": metaCognition
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
if not conversation_history: # Add the full system message only once at the beginning
|
| 91 |
+
system_message = create_system_message(new_weights, short=False)
|
| 92 |
+
conversation_history.append({"role": "system", "content": system_message})
|
| 93 |
+
|
| 94 |
+
if new_weights != current_weights:
|
| 95 |
+
current_weights = new_weights
|
| 96 |
+
short_system_message = create_system_message(new_weights, short=True)
|
| 97 |
+
conversation_history.append({"role": "system", "content": short_system_message})
|
| 98 |
+
|
| 99 |
+
conversation_history.append({"role": "user", "content": message})
|
| 100 |
+
|
| 101 |
+
total_tokens = sum(calculate_token_count(message['content']) for message in conversation_history) + RESPONSE_TOKENS
|
| 102 |
+
|
| 103 |
+
if total_tokens > TOKEN_THRESHOLD:
|
| 104 |
+
yield "Message Limit Reached. Please type 'reset' to start another chat."
|
| 105 |
+
return
|
| 106 |
+
|
| 107 |
+
conversation_history = trim_conversation_history(conversation_history, MAX_TOKENS - RESPONSE_TOKENS)
|
| 108 |
+
|
| 109 |
+
messages = conversation_history
|
| 110 |
+
# I believe this should work with openAI API with a little editing, as Groq API is designed for easy migration of API usage
|
| 111 |
+
# ^HOWEVER- Groq is lightning fast, able to handle threaded swarms better for online apps.
|
| 112 |
+
client = Groq(api_key=api_key)
|
| 113 |
+
response_accumulator = ""
|
| 114 |
+
|
| 115 |
+
try:
|
| 116 |
+
stream = client.chat.completions.create(
|
| 117 |
+
messages=messages,
|
| 118 |
+
# Try the larger or smaller LLama3 model (these should work locally depending on your hardware)
|
| 119 |
+
# ^We just like llama3 better for the demo. The REN-AI architecture implemented in our prompt and functions found here can be used across models.
|
| 120 |
+
# ^^We also intend on using fine-tuned or custom AI for our future systems, but this helps test core concepts
|
| 121 |
+
model="llama3-70b-8192",
|
| 122 |
+
temperature=0.4,
|
| 123 |
+
max_tokens=1024,
|
| 124 |
+
top_p=1,
|
| 125 |
+
stop=None,
|
| 126 |
+
stream=True,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
for chunk in stream:
|
| 130 |
+
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
|
| 131 |
+
response_accumulator += chunk.choices[0].delta.content
|
| 132 |
+
yield response_accumulator
|
| 133 |
+
|
| 134 |
+
conversation_history.append({"role": "assistant", "content": response_accumulator})
|
| 135 |
+
|
| 136 |
+
except Exception as e:
|
| 137 |
+
yield f"An error occurred: {str(e)}"
|
| 138 |
+
|
| 139 |
+
finally:
|
| 140 |
+
return
|
| 141 |
+
|
| 142 |
+
personality_slider = gr.Slider(minimum=0, maximum=100, value=100, label="Personality")
|
| 143 |
+
self_reflection_slider = gr.Slider(minimum=0, maximum=100, value=100, label="Self-Reflection")
|
| 144 |
+
abstraction_level_slider = gr.Slider(minimum=0, maximum=100, value=100, label="Abstraction Level")
|
| 145 |
+
meta_cognition_slider = gr.Slider(minimum=0, maximum=100, value=100, label="Meta-Cognition")
|
| 146 |
+
|
| 147 |
+
iface = gr.ChatInterface(
|
| 148 |
+
fn=predict,
|
| 149 |
+
title="REN-AI DEMO | DARK ENGINE",
|
| 150 |
+
description="Welcome to our limited demo | Learn more at [darkengine.ai](https://darkengine.ai)\n\nType 'reset' to remove error messages or delete AI memory",
|
| 151 |
+
additional_inputs=[personality_slider, self_reflection_slider, abstraction_level_slider, meta_cognition_slider],
|
| 152 |
+
additional_inputs_accordion=gr.Accordion(open=True, label="Cognition Settings"),
|
| 153 |
+
theme="monochrome",
|
| 154 |
+
css="footer{display:none !important}"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
if __name__ == "__main__":
|
| 158 |
+
iface.launch(show_api=False)
|