File size: 5,690 Bytes
55ccc4d 7b9d31d 55ccc4d 7b9d31d b39ac03 7b9d31d a5a5ea6 7b9d31d 55ccc4d 7b9d31d b39ac03 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d 55ccc4d 7b9d31d b39ac03 7b9d31d b39ac03 7b9d31d 55ccc4d 7b9d31d b39ac03 55ccc4d 7d89f41 55ccc4d | 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | import os
import uuid
import gradio as gr
from huggingface_hub import InferenceClient
from duckduckgo_search import DDGS
import chromadb
# -----------------------------
# LLM CONFIGURATION
# -----------------------------
MODEL_NAME = "openai/gpt-oss-20b"
HF_TOKEN = os.getenv("HF_TOKEN")
# -----------------------------
# VECTOR MEMORY SETUP
# -----------------------------
client_db = chromadb.PersistentClient(path="./medini_memory")
collection = client_db.get_or_create_collection(
name="conversation_memory"
)
# -----------------------------
# WEB SEARCH TOOL
# -----------------------------
def web_search(query):
results = []
try:
with DDGS() as ddgs:
for r in ddgs.text(query, max_results=3):
body = r.get("body", "")
if body:
results.append(body)
except Exception as e:
return f"Web search failed: {str(e)}"
return "\n".join(results)
# -----------------------------
# MEMORY FUNCTIONS
# -----------------------------
def save_memory(user_message, assistant_response):
memory_text = f"""
USER: {user_message}
ASSISTANT: {assistant_response}
"""
collection.add(
documents=[memory_text],
ids=[str(uuid.uuid4())]
)
def retrieve_memory(query):
try:
results = collection.query(
query_texts=[query],
n_results=3
)
docs = results.get("documents", [[]])[0]
return "\n".join(docs)
except Exception:
return ""
# -----------------------------
# PLANNER AGENT
# -----------------------------
def planner(user_input):
user_input = user_input.lower()
if any(
keyword in user_input
for keyword in [
"search",
"latest",
"news",
"find",
"lookup",
"web",
]
):
return "web_search"
return "chat"
# -----------------------------
# MAIN AI AGENT
# -----------------------------
def agent_respond(
message,
history,
system_message,
max_tokens,
temperature,
top_p,
):
# -----------------------------
# CONNECT MODEL
# -----------------------------
client = InferenceClient(
model=MODEL_NAME,
token=HF_TOKEN
)
# -----------------------------
# PLANNING
# -----------------------------
action = planner(message)
tool_context = ""
# -----------------------------
# TOOL EXECUTION
# -----------------------------
if action == "web_search":
tool_context = web_search(message)
# -----------------------------
# MEMORY RETRIEVAL
# -----------------------------
memory_context = retrieve_memory(message)
# -----------------------------
# SYSTEM PROMPT
# -----------------------------
enhanced_system_prompt = f"""
{system_message}
You are Medini Intelligence AI Agent.
You have:
- long-term memory
- web search capability
- contextual reasoning
MEMORY:
{memory_context}
TOOL RESULTS:
{tool_context}
Use the information intelligently.
"""
# -----------------------------
# BUILD MESSAGE HISTORY
# -----------------------------
messages = [
{
"role": "system",
"content": enhanced_system_prompt,
}
]
if history:
messages.extend(history)
messages.append(
{
"role": "user",
"content": message
}
)
# -----------------------------
# STREAM RESPONSE
# -----------------------------
response = ""
try:
stream = client.chat.completions.create(
messages=messages,
model=MODEL_NAME,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
)
for chunk in stream:
token = ""
if (
chunk.choices
and hasattr(chunk.choices[0].delta, "content")
and chunk.choices[0].delta.content
):
token = chunk.choices[0].delta.content
response += token
yield response
except Exception as e:
yield f"Error: {str(e)}"
return
# -----------------------------
# SAVE MEMORY
# -----------------------------
save_memory(message, response)
# -----------------------------
# GRADIO UI
# -----------------------------
chatbot = gr.ChatInterface(
fn=agent_respond,
type="messages",
additional_inputs=[
gr.Textbox(
value="You are Medini Intelligence AI Agent.",
label="System Message",
),
gr.Slider(
minimum=1,
maximum=4096,
value=1024,
step=1,
label="Max Tokens",
),
gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.7,
step=0.1,
label="Temperature",
),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p",
),
],
)
# -----------------------------
# APP LAYOUT
# -----------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# Medini Intelligence AI Agent
### Features
- Conversational AI
- Memory
- Web Search
- Tool Use
- Autonomous Reasoning
"""
)
chatbot.render()
# -----------------------------
# RUN APP
# -----------------------------
if __name__ == "__main__":
demo. launch(
server_name="0.0.0.0",
server_port=7860
)
|