mirror of
https://github.com/ION606/ML-pipeline.git
synced 2026-05-14 21:06:54 +00:00
added rudamentary search and code execution
This commit is contained in:
+186
@@ -0,0 +1,186 @@
|
||||
import debug as debugMod
|
||||
from search import perform_web_search
|
||||
import ollama
|
||||
import conversation_store
|
||||
conversation_store.initialize_db()
|
||||
|
||||
# models: better: qwen2.5-coder:14b, faster: phi3 (but worse), with more processing power: deepseek-r1:32b
|
||||
MODEL_NAMES = {
|
||||
"classification": "dolphin3:8b", # Best for structured tasks
|
||||
"simple": "phi3:latest", # phi3:mini
|
||||
"medium": "llama3:8b-instruct-q8_0",
|
||||
"complex": "deepseek-coder:33b-instruct-q4_K_M"
|
||||
}
|
||||
|
||||
|
||||
def classify_task(query: str) -> str:
|
||||
# Use a tiny model to classify the task
|
||||
prompt = f"""Classify this query into one of these categories:
|
||||
- "simple": greetings, yes/no, basic facts
|
||||
- "medium": summarization, simple coding
|
||||
- "complex": advanced coding, data analysis, multi-step reasoning
|
||||
|
||||
Query: {query}
|
||||
Return ONLY the category name (e.g., "simple")."""
|
||||
|
||||
toPassIn = ""
|
||||
for i in range(3):
|
||||
response = ollama.chat(model=MODEL_NAMES["classification"], messages=[
|
||||
{"role": "user", "content": prompt + toPassIn}])
|
||||
task_type = response["message"]["content"].strip().lower()
|
||||
if (task_type in MODEL_NAMES.keys()):
|
||||
return task_type
|
||||
else:
|
||||
toPassIn += f"\nthe last response '{response}' was incorrect (AKA not one of {MODEL_NAMES.keys()}), try again and pick one of these based on the above"
|
||||
|
||||
return 'complex'
|
||||
|
||||
|
||||
def generate_prompt(query, web_context, local_context, user_context, response_context, onlyRules=False):
|
||||
prompt = f"""
|
||||
**Strict Response Rules**
|
||||
1. Greetings & Casual Queries:
|
||||
- For greetings (e.g. "good morning", "hello"):
|
||||
* Respond with ONLY a short friendly acknowledgment
|
||||
* NEVER explain why you can't chat casually
|
||||
* Example: "Good morning! How can I assist you today?"
|
||||
|
||||
2. Technical Responses:
|
||||
- Generate code ONLY if:
|
||||
* User explicitly requests technical help
|
||||
* Local file context exists for data analysis tasks
|
||||
- Keep code explanations concise (1-2 sentences max)
|
||||
|
||||
3. Web Search Policy:
|
||||
- NEVER search for greetings/casual conversation
|
||||
- Search only when:
|
||||
* Technical info is needed
|
||||
* Local data is insufficient
|
||||
|
||||
4. Formatting:
|
||||
- NO markdown/bullets in casual responses
|
||||
- NO internal system references (e.g. "Technilopia Forum")
|
||||
- NO justification of rules to users
|
||||
- NEVER include the user's question unless explicitly asked to do so
|
||||
- NEVER include previous responses
|
||||
|
||||
{f'Local File Context: {local_context}' if local_context else ''}
|
||||
"""
|
||||
|
||||
if onlyRules:
|
||||
return prompt
|
||||
|
||||
prompt = f"""
|
||||
Context Sources:\n
|
||||
{f'[WEB] {web_context}' if web_context else ''}\n
|
||||
{f'[LOCAL FILE] {local_context}' if local_context else ''}\n
|
||||
{f'[USER CONTEXT] {user_context}' if user_context else ''}\n
|
||||
\n[PREVIOUS RESPONSES] {response_context}\n
|
||||
Question: {query}
|
||||
|
||||
{prompt}
|
||||
"""
|
||||
return prompt
|
||||
|
||||
|
||||
def call_ollama_and_print(task_type, prompt, silent=False):
|
||||
if silent:
|
||||
response = ollama.chat(model=MODEL_NAMES[task_type], messages=[
|
||||
{"role": "user", "content": prompt}])
|
||||
debugMod.log("RAG query response received")
|
||||
return response
|
||||
|
||||
full_response = ""
|
||||
print("\nAI Response: ", end="", flush=True) # Start response line
|
||||
|
||||
# Stream the response
|
||||
stream = ollama.chat(
|
||||
model=MODEL_NAMES[task_type],
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
stream=True
|
||||
)
|
||||
|
||||
for chunk in stream:
|
||||
content = chunk.get('message', {}).get('content', '')
|
||||
print(content, end="", flush=True) # Stream to terminal
|
||||
full_response += content
|
||||
|
||||
print() # Newline after streaming
|
||||
debugMod.log("RAG query response received")
|
||||
return full_response
|
||||
|
||||
|
||||
def multi_choice_query(query, options: list[str], task_type: str, web_context="", local_context="", user_context="", silent=False):
|
||||
attempts = 0
|
||||
max_attempts = 3
|
||||
inds = list(range(len(options)))
|
||||
valid_range = f"0-{len(inds) - 1}"
|
||||
last_error = ""
|
||||
|
||||
debugMod.log(
|
||||
f"Multi-choice query with options: {', '.join([f'{i}: {opt}' for i, opt in enumerate(options)])}")
|
||||
|
||||
while attempts < max_attempts:
|
||||
prompt = f"""Return ONLY the numeric index ({valid_range}) for the best option. Invalid responses will be rejected.
|
||||
|
||||
Available Options:
|
||||
{"\n".join([f"{i}: {option}" for i, option in enumerate(options)])}
|
||||
|
||||
Question: {query}
|
||||
|
||||
Context Sources:
|
||||
{f'[WEB] {web_context}' if web_context else ''}
|
||||
{f'[LOCAL] {local_context}' if local_context else ''}
|
||||
{f'[USER] {user_context}' if user_context else ''}
|
||||
|
||||
{generate_prompt(query, web_context, local_context, user_context, onlyRules=True)}
|
||||
- You MUST return a SINGLE INTEGER between {valid_range}
|
||||
- DO NOT include explanations or punctuation"""
|
||||
|
||||
if last_error:
|
||||
prompt += f"\n\nPrevious invalid response: {last_error}"
|
||||
|
||||
try:
|
||||
content = call_ollama_and_print(task_type, prompt, silent)
|
||||
debugMod.log(f"Multi-choice response: {content}", wrapped=True)
|
||||
|
||||
# Strict validation
|
||||
if not content.isdigit():
|
||||
raise ValueError(f"Non-numeric response: {content}")
|
||||
|
||||
ind = int(content)
|
||||
|
||||
if 0 <= ind < len(options):
|
||||
debugMod.log(f"Valid choice selected: {ind} ({options[ind]})")
|
||||
return options[ind]
|
||||
|
||||
raise IndexError(f"Index {ind} out of range {valid_range}")
|
||||
|
||||
except (ValueError, IndexError) as e:
|
||||
last_error = str(e)
|
||||
debugMod.log(f"Validation failed: {last_error}")
|
||||
attempts += 1
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
debugMod.log(f"Unexpected error: {str(e)}")
|
||||
attempts += 1
|
||||
continue
|
||||
|
||||
# Fallback to safest option after all attempts
|
||||
debugMod.log(f"All attempts failed. Defaulting to first option")
|
||||
return options[0]
|
||||
|
||||
|
||||
def rag_query(query, task_type: str = None, web_context="", local_context="", user_context="", response_context="", silent=False):
|
||||
# Model selection logic
|
||||
task_type = classify_task(query) if not task_type else task_type
|
||||
|
||||
debugMod.log(f"Generating {task_type} RAG query with query: {query}")
|
||||
prompt = generate_prompt(
|
||||
query, web_context, local_context, user_context, response_context)
|
||||
|
||||
response = call_ollama_and_print(task_type, prompt, silent)
|
||||
|
||||
# if it's not silent, then it'll return a string
|
||||
return response["message"]["content"] if silent else response
|
||||
Reference in New Issue
Block a user