Spaces:
Sleeping
Sleeping
File size: 51,020 Bytes
7644eac |
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 |
"""
Model orchestrator for the AI Learning Path Generator.
Handles interactions with language models and embeddings.
"""
from langchain.prompts import PromptTemplate, ChatPromptTemplate
from src.utils.observability import get_observability_manager, estimate_cost
from src.utils.cache import cache, cached
from src.utils.helpers import optimize_prompt, count_tokens, estimate_api_cost
from src.utils.config import (
OPENAI_API_KEY,
DEEPSEEK_API_KEY, # Kept for legacy compatibility
OPENROUTER_API_KEY, # OpenRouter support
DEFAULT_PROVIDER,
DEFAULT_MODEL,
OPENROUTER_FREE_MODEL, # Free model from OpenRouter
MAX_TOKENS,
TEMPERATURE
)
from langchain.chains import LLMChain
from typing import List, Dict, Any, Optional, Union, TypeVar, Type
import json
import os
# Using Pydantic v1
import pydantic
from pydantic import BaseModel as PydanticBaseModel
# Import from langchain (older version compatible with Pydantic v1)
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
# For type hints
T = TypeVar('T', bound='BaseModel')
class BaseModel(PydanticBaseModel):
"""Base model using Pydantic v1."""
class Config:
arbitrary_types_allowed = True
# We'll use only OpenAI for now to make the application work
# Both providers will default to using OpenAI
# Import token optimization utilities for cost savings
# Import caching utilities to avoid repeated API calls
# Import observability utilities for LLM monitoring
class ModelOrchestrator:
"""
Manages AI model interactions with RAG capabilities.
"""
def __init__(self, api_key: Optional[str] = None, provider: Optional[str] = None):
print("--- ModelOrchestrator.__init__ started ---")
"""
Initialize the model orchestrator with RAG capabilities.
Args:
api_key: Optional API key (if not provided, will use from environment)
provider: Optional provider name ('openai', 'openrouter', or 'deepseek')
"""
self.provider = provider.lower() if provider else DEFAULT_PROVIDER
self.context = []
self.goal = None
self.planning_enabled = True
self.memory = []
# Set up API key based on selected provider
if self.provider == 'openai':
self.api_key = api_key or OPENAI_API_KEY
if not self.api_key:
raise ValueError(
"OpenAI API key is required. Please provide it or set the OPENAI_API_KEY environment variable.")
print(
"--- ModelOrchestrator.__init__: Preparing to initialize ChatOpenAI ---")
print(
f"--- ModelOrchestrator.__init__: API Key: {str(self.api_key)[:15]}..., Model: {DEFAULT_MODEL}, Temp: {TEMPERATURE}, Max Tokens: {MAX_TOKENS} ---")
# self.llm = ChatOpenAI(
# api_key=self.api_key,
# model_name=DEFAULT_MODEL,
# temperature=TEMPERATURE,
# max_tokens=MAX_TOKENS
# )
print("--- ModelOrchestrator.__init__: ChatOpenAI initialization SKIPPED ---")
print(
"--- ModelOrchestrator.__init__: Preparing to initialize OpenAI (base_llm) ---")
# self.base_llm = OpenAI(
# api_key=self.api_key,
# model_name=DEFAULT_MODEL,
# temperature=TEMPERATURE,
# max_tokens=MAX_TOKENS
# )
print(
"--- ModelOrchestrator.__init__: OpenAI (base_llm) initialization SKIPPED ---")
elif self.provider == 'deepseek':
self.api_key = api_key or DEEPSEEK_API_KEY
if not self.api_key:
raise ValueError(
"DeepSeek API key is required. Please provide it or set the DEEPSEEK_API_KEY environment variable.")
print("--- ModelOrchestrator.__init__: DeepSeek provider selected, client initialization SKIPPED for now ---")
elif self.provider == 'openrouter':
self.api_key = api_key or OPENROUTER_API_KEY
if not self.api_key:
raise ValueError(
"OpenRouter API key is required. Please provide it or set the OPENROUTER_API_KEY environment variable.")
print(
"--- ModelOrchestrator.__init__: OpenRouter provider selected (free models available) ---")
# Only OpenAI, OpenRouter and DeepSeek providers are supported now
# (OpenAI is the primary and recommended provider)
else:
raise ValueError(
f"Unsupported provider: {self.provider}. Use 'openai', 'openrouter', or 'deepseek'.")
# Track current model name
self.model_name = DEFAULT_MODEL
# Initialize observability manager
self.obs_manager = get_observability_manager()
# Override default model if DeepSeek provider is selected
if self.provider == 'deepseek':
# Allow environment variable override but default to the official DeepSeek chat model
self.model_name = os.getenv("DEEPSEEK_MODEL", "deepseek-chat")
print(
f"--- ModelOrchestrator.__init__: DeepSeek provider detected, using model: {self.model_name} ---")
# Initialize the language model based on provider
print("--- ModelOrchestrator.__init__: Calling init_language_model ---")
self.init_language_model()
print("--- ModelOrchestrator.__init__ finished (LLM initialized) ---")
def init_language_model(self, model_name: Optional[str] = None, temperature: Optional[float] = None):
print(
f"--- ModelOrchestrator.init_language_model started (provider: {self.provider}, model: {model_name or self.model_name}) ---")
"""
Initialize or switch the language model.
Args:
model_name: Name of the model to use
temperature: Temperature setting for the model
"""
# Update model name if provided
if model_name:
self.model_name = model_name
temp = temperature if temperature is not None else TEMPERATURE
# Initialize based on provider
try:
if self.provider == 'openai':
print(
f"--- ModelOrchestrator.init_language_model: Initializing ChatOpenAI for {self.provider} ---")
self.llm = ChatOpenAI(
openai_api_key=self.api_key,
model=self.model_name,
temperature=temp,
max_tokens=MAX_TOKENS,
)
print(
f"--- ModelOrchestrator.init_language_model: ChatOpenAI for {self.provider} initialized ---")
elif self.provider == 'openrouter':
print(
f"--- ModelOrchestrator.init_language_model: Initializing ChatOpenAI for OpenRouter ---")
# Use OpenRouter free model for this provider
model_to_use = OPENROUTER_FREE_MODEL
self.model_name = model_to_use # Update model name
# OpenRouter uses OpenAI-compatible API with different endpoint
self.llm = ChatOpenAI(
openai_api_key=self.api_key,
openai_api_base="https://openrouter.ai/api/v1",
model=model_to_use,
temperature=temp,
max_tokens=MAX_TOKENS,
)
print(
f"--- ModelOrchestrator.init_language_model: ChatOpenAI for OpenRouter initialized with model: {model_to_use} ---")
elif self.provider == 'deepseek':
print(
f"--- ModelOrchestrator.init_language_model: Initializing ChatOpenAI for {self.provider} ---")
# DeepSeek uses OpenAI-compatible API
self.llm = ChatOpenAI(
openai_api_key=self.api_key,
openai_api_base="https://api.deepseek.com/v1",
model=self.model_name,
temperature=temp,
max_tokens=MAX_TOKENS,
)
print(
f"--- ModelOrchestrator.init_language_model: ChatOpenAI for DeepSeek initialized ---")
except Exception as e:
print(f"Error initializing language model: {str(e)}")
raise
def switch_provider(self, provider: str, api_key: Optional[str] = None, model_name: Optional[str] = None):
"""
Switch between AI providers.
Args:
provider: The provider to switch to ('openai' or 'deepseek')
api_key: Optional API key for the provider
model_name: Optional model name to use
Returns:
str: Status message indicating the provider and model in use
"""
try:
self.provider = provider.lower()
# Update API key if provided
if api_key:
self.api_key = api_key
elif self.provider == 'openai':
self.api_key = OPENAI_API_KEY
elif self.provider == 'deepseek':
self.api_key = DEEPSEEK_API_KEY
# OpenAI is the primary provider now
else:
raise ValueError(
f"Unsupported provider: {provider}. Use 'openai' or 'deepseek'.")
# Update model name if provided
if model_name:
self.model_name = model_name
# Re-initialize the language model
self.init_language_model()
return f"Switched to {self.provider} provider with model {self.model_name}"
except Exception as e:
error_msg = f"Error switching to provider {provider}: {str(e)}"
print(error_msg)
# Try to fallback to a working provider
if self.provider != 'openai':
print("Falling back to OpenAI provider")
return self.switch_provider('openai', OPENAI_API_KEY, model_name or DEFAULT_MODEL)
raise ValueError(error_msg) from e
def generate_response(
self,
prompt: str,
relevant_documents: Optional[List[str]] = None,
temperature: Optional[float] = None,
use_cache: bool = True # NEW: Enable caching by default
) -> str:
"""
Generate a text response from the language model.
Args:
prompt: The prompt for the model
relevant_documents: Optional list of relevant documents to add context
temperature: Optional override for model temperature
use_cache: Whether to use cached responses (default: True)
Returns:
The generated response as a string
"""
# Check cache first to save money! 💰
if use_cache:
cache_key = cache.cache_key(
"response",
prompt[:200], # First 200 chars of prompt
str(relevant_documents)[:100] if relevant_documents else "",
self.model_name,
temperature or TEMPERATURE
)
cached_response = cache.get(cache_key)
if cached_response:
print("💰 Using cached response - $0.00 cost!")
return cached_response
# Optimize prompt to reduce token usage and save money! 💰
full_prompt = optimize_prompt(
prompt, relevant_documents, max_tokens=4000)
# Log token count and estimated cost for monitoring
input_token_count = count_tokens(full_prompt, self.model_name)
estimated_input_cost = estimate_api_cost(
input_token_count, self.model_name)
print(
f"💰 Token count: {input_token_count} (~${estimated_input_cost:.4f} input cost)")
try:
# Set up the temperature
temp = temperature if temperature is not None else TEMPERATURE
print("DEBUG: About to make OpenAI API call using direct implementation...")
import time
from src.direct_openai import generate_completion
try:
start_time = time.time()
print(f"DEBUG: Using model: {self.model_name}")
print(f"DEBUG: Prompt length: {len(full_prompt)} chars")
# Use our direct implementation that bypasses the client library
response_text = generate_completion(
prompt=full_prompt,
system_message="You are an expert educational AI assistant that specializes in creating personalized learning paths.",
model=self.model_name,
temperature=temp,
max_tokens=MAX_TOKENS,
timeout=120
)
latency_ms = (time.time() - start_time) * 1000
print(f"DEBUG: API call completed in {latency_ms:.2f}ms")
# Estimate output tokens and total cost
output_token_count = count_tokens(
response_text, self.model_name) if response_text else 0
total_cost = estimate_cost(
self.model_name, input_token_count, output_token_count)
# Log to observability platform (LangSmith + W&B)
self.obs_manager.log_llm_call(
prompt=full_prompt,
response=response_text,
model=self.model_name,
metadata={
"temperature": temp,
"max_tokens": MAX_TOKENS,
"provider": self.provider,
"cached": False
},
latency_ms=latency_ms,
token_count=input_token_count + output_token_count,
cost=total_cost
)
# Cache the response for future use (save money!)
if use_cache and response_text:
# Cache for 24 hours
cache.set(cache_key, response_text, ttl=86400)
return response_text
except Exception as e:
print(f"DEBUG: API call failed: {str(e)}")
raise
except Exception as e:
error_msg = f"Error generating response: {str(e)}"
print(error_msg)
# Try to extract more detailed error information
try:
import traceback
error_traceback = traceback.format_exc()
print(f"Error traceback:\n{error_traceback}")
# Check if it's an OpenAI API error
if hasattr(e, 'response') and hasattr(e.response, 'json'):
error_data = e.response.json()
print(f"OpenAI API Error: {error_data}")
error_msg += f"\nAPI Error: {error_data.get('error', {}).get('message', str(e))}"
except Exception as inner_e:
print(f"Error while processing error: {str(inner_e)}")
raise ValueError(error_msg) from e
def generate_response_stream(
self,
prompt: str,
relevant_documents: Optional[List[str]] = None,
temperature: Optional[float] = None,
):
"""
Generate streaming response for real-time output.
Why streaming:
- Users see progress immediately
- Perceived performance is better
- Same cost as regular response!
- Better UX = happier users
Args:
prompt: The prompt for the model
relevant_documents: Optional list of relevant documents to add context
temperature: Optional override for model temperature
Yields:
Chunks of response text as they arrive
"""
# Optimize prompt to reduce costs
full_prompt = optimize_prompt(
prompt, relevant_documents, max_tokens=4000)
# Log token count
token_count = count_tokens(full_prompt, self.model_name)
estimated_cost = estimate_api_cost(token_count, self.model_name)
print(
f"💰 Streaming - Token count: {token_count} (~${estimated_cost:.4f} input cost)")
temp = temperature if temperature is not None else TEMPERATURE
try:
from openai import OpenAI
client = OpenAI(api_key=OPENAI_API_KEY)
stream = client.chat.completions.create(
model=self.model_name,
messages=[
{"role": "system", "content": "You are an expert educational AI assistant that specializes in creating personalized learning paths."},
{"role": "user", "content": full_prompt}
],
temperature=temp,
max_tokens=MAX_TOKENS,
stream=True # Enable streaming!
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
print(f"Streaming error: {str(e)}")
yield f"Error: {str(e)}"
def generate_structured_response(
self,
prompt: str,
output_schema: str,
relevant_documents: Optional[List[str]] = None,
temperature: Optional[float] = None,
use_cache: bool = True # NEW: Enable caching by default
) -> str:
"""
Generate a structured response that follows a specific schema.
Args:
prompt: The prompt for the model
output_schema: The schema instructions for the output
relevant_documents: Optional list of relevant documents to add context
temperature: Optional override for model temperature
use_cache: Whether to use cached responses (default: True)
Returns:
The generated response as a JSON string
"""
# Check cache first to save money! 💰
if use_cache:
cache_key = cache.cache_key(
"structured",
prompt[:200], # First 200 chars of prompt
output_schema[:100], # First 100 chars of schema
str(relevant_documents)[:100] if relevant_documents else "",
self.model_name,
temperature or 0.2
)
cached_response = cache.get(cache_key)
if cached_response:
print("💰 Using cached structured response - $0.00 cost!")
return cached_response
# Determine if this is a learning path generation
is_learning_path = 'LearningPath' in output_schema
# Prepare the prompt with schema instructions and emphasize required fields
required_fields_reminder = ""
if is_learning_path:
required_fields_reminder = """
IMPORTANT: Your response MUST include ALL of these required fields:
- title: String title of the learning path
- description: Detailed description of the learning path
- topic: Main topic of study
- expertise_level: Starting expertise level
- learning_style: Preferred learning style
- time_commitment: Weekly time commitment
- duration_weeks: Total duration in weeks (integer)
- goals: List of learning goals and objectives
- milestones: List of learning milestones
- prerequisites: List of prerequisites for this path
- total_hours: Total estimated hours (integer)
For each milestone, you MUST include:
- title: Short title for the milestone
- description: Detailed description
- estimated_hours: Estimated hours to complete (integer)
- resources: List of recommended learning resources
- skills_gained: List of skills gained after completion
"""
schema_prompt = f"""
{prompt}
Your response should follow this schema format:
{output_schema}
{required_fields_reminder}
Please provide a valid JSON response that strictly follows this schema.
Do not include any explanatory text outside the JSON structure.
"""
# Optimize prompt with context to reduce token usage 💰
full_prompt = optimize_prompt(
schema_prompt, relevant_documents, max_tokens=6000)
# Log token count and estimated cost
token_count = count_tokens(full_prompt, self.model_name)
estimated_cost = estimate_api_cost(token_count, self.model_name)
print(
f"💰 Structured response - Token count: {token_count} (~${estimated_cost:.4f} input cost)")
# Set up the temperature - lower for structured outputs
temp = temperature if temperature is not None else 0.2
# Use our direct implementation that bypasses the client library
import time
import requests
import traceback
response_text = None
try:
start_time = time.time()
print(
f"DEBUG: Generating structured response using provider: {self.provider}, model: {self.model_name}")
print(f"DEBUG: Prompt length: {len(full_prompt)} chars")
# Print the first 200 chars of the prompt for debugging
print(f"DEBUG: Prompt preview: {full_prompt[:200]}...")
# Print API key details for debugging (safely)
if self.provider == 'openai':
api_key = OPENAI_API_KEY
if api_key:
print(
f"DEBUG: Using OpenAI API key starting with: {api_key[:5]}{'*' * 10}")
else:
print("DEBUG: WARNING - No OpenAI API key found!")
elif self.provider == 'deepseek':
api_key = DEEPSEEK_API_KEY
if api_key:
print(
f"DEBUG: Using DeepSeek API key starting with: {api_key[:5]}{'*' * 10}")
else:
print("DEBUG: WARNING - No DeepSeek API key found!")
# OpenAI is the primary provider now
if self.provider == 'openai':
from src.direct_openai import generate_completion
print("Attempting to generate OpenAI completion...")
response_text = generate_completion(
prompt=full_prompt,
system_message="You are an expert AI assistant that specializes in generating structured responses following specified schemas. Always include all required fields in your JSON response.",
model=self.model_name,
temperature=temp,
max_tokens=MAX_TOKENS,
timeout=300 # Increase timeout for reliability
)
print(
f"Successfully generated completion with {len(response_text) if response_text else 0} characters")
elif self.provider == 'openrouter':
# OpenRouter uses OpenAI-compatible API via direct_openai with custom endpoint
from openai import OpenAI as OpenAIClient
print("Attempting to generate OpenRouter completion...")
client = OpenAIClient(
api_key=self.api_key,
base_url="https://openrouter.ai/api/v1"
)
# Use free model if not specified
model_to_use = self.model_name if self.model_name else OPENROUTER_FREE_MODEL
try:
completion = client.chat.completions.create(
model=model_to_use,
messages=[
{"role": "system", "content": "You are an expert AI assistant that specializes in generating structured responses following specified schemas. Always include all required fields in your JSON response."},
{"role": "user", "content": full_prompt}
],
temperature=temp,
max_tokens=MAX_TOKENS,
timeout=300
)
response_text = completion.choices[0].message.content
print(
f"Successfully generated OpenRouter completion with {len(response_text) if response_text else 0} characters")
except Exception as e:
print(f"Error calling OpenRouter API: {e}")
response_text = None
elif self.provider == 'deepseek':
response_text = self._deepseek_completion(
full_prompt,
temp,
system_message="You are an expert AI assistant that specializes in generating structured responses following specified schemas. Always include all required fields in your JSON response."
)
# OpenAI is the primary provider now
else:
raise ValueError(f"Unknown provider: {self.provider}")
print(
f"DEBUG: API call completed in {time.time() - start_time:.2f} seconds")
if response_text:
print(
f"DEBUG: Received response with length: {len(response_text)} chars")
print(f"DEBUG: Response preview: {response_text[:100]}...")
else:
print("DEBUG: WARNING - Received empty response from API")
if is_learning_path:
# Return a fallback learning path
return self._create_fallback_learning_path()
else:
# Return a fallback generic response
return json.dumps({
"summary": "Sorry, I encountered an error retrieving information.",
"key_concepts": ["Error occurred while processing your request"],
"learning_path": ["Please try again with a different query"],
"resources": [],
"code_examples": [],
"advanced_topics": []
})
except Exception as e:
print(f"DEBUG: Structured response generation failed: {str(e)}")
print(traceback.format_exc())
if is_learning_path:
# Return a fallback learning path
return self._create_fallback_learning_path()
else:
# Return a fallback generic response
return json.dumps({
"summary": f"Sorry, I encountered an error: {str(e)}",
"key_concepts": ["Unable to extract structured information"],
"learning_path": ["Please try asking in a different way"],
"resources": [],
"code_examples": [],
"advanced_topics": [],
"career_applications": []
})
# Extract JSON from the response
try:
# Try to find JSON in the response (may be enclosed in ```json blocks)
if "```json" in response_text:
json_start = response_text.find("```json") + 7
json_end = response_text.find("```", json_start)
json_str = response_text[json_start:json_end].strip()
elif "```" in response_text:
json_start = response_text.find("```") + 3
json_end = response_text.find("```", json_start)
json_str = response_text[json_start:json_end].strip()
else:
json_str = response_text.strip()
# Validate JSON
data = json.loads(json_str)
# If expecting a learning path but received a list or wrong type, fallback
if is_learning_path and not isinstance(data, dict):
print(
"DEBUG: Expected learning path dict but received different type, returning fallback path.")
return self._create_fallback_learning_path()
# For learning paths, validate that all required fields are present
if is_learning_path:
required_fields = [
'title', 'description', 'topic', 'expertise_level',
'learning_style', 'time_commitment', 'duration_weeks',
'goals', 'milestones', 'prerequisites', 'total_hours'
]
missing_fields = [
field for field in required_fields if field not in data]
if missing_fields:
print(
f"DEBUG: Missing required fields in learning path: {missing_fields}")
# If any fields are missing, add them with default values
for field in missing_fields:
if field == 'title':
data['title'] = data.get(
'topic', 'Learning Path') + ' Learning Path'
elif field == 'description':
data[
'description'] = f"A comprehensive learning path for {data.get('topic', 'the requested topic')}."
elif field == 'topic':
data['topic'] = data.get(
'title', 'General Learning').replace(' Learning Path', '')
elif field == 'expertise_level':
data['expertise_level'] = 'beginner'
elif field == 'learning_style':
data['learning_style'] = 'visual'
elif field == 'time_commitment':
data['time_commitment'] = 'moderate'
elif field == 'duration_weeks':
data['duration_weeks'] = 8
elif field == 'goals':
data['goals'] = [
f"Master {data.get('topic', 'the subject')}"]
elif field == 'milestones':
data['milestones'] = [{
'title': 'Getting Started',
'description': f"Introduction to {data.get('topic', 'the subject')}",
'estimated_hours': 10,
'resources': [{'name': 'Online Documentation', 'url': '', 'type': 'documentation'}],
'skills_gained': [f"Basic {data.get('topic', 'subject')} knowledge"]
}]
elif field == 'prerequisites':
data['prerequisites'] = ['None']
elif field == 'total_hours':
data['total_hours'] = 40
# Also check that each milestone has the required fields
if 'milestones' in data and isinstance(data['milestones'], list):
milestone_required_fields = [
'title', 'description', 'estimated_hours', 'resources', 'skills_gained']
for i, milestone in enumerate(data['milestones']):
milestone_missing_fields = [
field for field in milestone_required_fields if field not in milestone]
if milestone_missing_fields:
print(
f"DEBUG: Missing required fields in milestone {i+1}: {milestone_missing_fields}")
# Add missing fields with default values
for field in milestone_missing_fields:
if field == 'title':
milestone['title'] = f"Milestone {i+1}"
elif field == 'description':
milestone['description'] = f"A key learning milestone in this path."
elif field == 'estimated_hours':
milestone['estimated_hours'] = 10
elif field == 'resources':
milestone['resources'] = [
{'name': 'Online Resource', 'url': '', 'type': 'article'}]
elif field == 'skills_gained':
milestone['skills_gained'] = [
f"Skills related to {data.get('topic', 'the subject')}"]
# Cache the successful response for future use (save money!)
json_result = json.dumps(data)
if use_cache:
# Cache for 24 hours
cache.set(cache_key, json_result, ttl=86400)
return json_result
except Exception as e:
print(f"DEBUG: Error parsing initial JSON: {str(e)}")
# First cleanup attempt - remove markdown code block wrappers
cleaned_response = response_text.strip()
# Remove ```json...``` or ```...``` markdown wrappers
import re
markdown_match = re.search(
r'```(?:json)?\s*(.*?)\s*```', response_text, re.DOTALL)
if markdown_match:
cleaned_response = markdown_match.group(1).strip()
print(f"DEBUG: Extracted content from markdown code block")
# Remove common text prefixes
for prefix in ["+", "-", "*", "#", "Response:", "JSON:", "Here's", "```", "```json"]:
if cleaned_response.startswith(prefix):
cleaned_response = cleaned_response[len(prefix):].strip()
try:
# Try to parse the cleaned response
data = json.loads(cleaned_response)
print(f"DEBUG: Successfully parsed cleaned JSON")
return json.dumps(data)
except Exception as e2:
print(f"DEBUG: Error parsing cleaned JSON: {str(e2)}")
# Second attempt - find the main JSON object (start with first { and match closing })
try:
first_brace = cleaned_response.find('{')
if first_brace != -1:
# Count braces to find the matching closing brace
brace_count = 0
end_pos = first_brace
for i in range(first_brace, len(cleaned_response)):
if cleaned_response[i] == '{':
brace_count += 1
elif cleaned_response[i] == '}':
brace_count -= 1
if brace_count == 0:
end_pos = i + 1
break
potential_json = cleaned_response[first_brace:end_pos]
print(
f"DEBUG: Extracted JSON from position {first_brace} to {end_pos} ({len(potential_json)} chars)")
data = json.loads(potential_json)
print(f"DEBUG: Successfully parsed extracted JSON")
return json.dumps(data)
except Exception as e3:
print(f"DEBUG: Error in brace matching: {str(e3)}")
# Return a fallback JSON as last resort instead of raising an exception
print("DEBUG: Returning fallback JSON structure due to parsing failure")
return json.dumps({
"summary": "Failed to parse the AI's response. The content might not be in the expected JSON format.",
"key_concepts": ["JSON parsing error"],
"learning_path": ["Please try a different query or check the AI provider's output directly if possible."],
"resources": [],
"code_examples": [],
"advanced_topics": [],
"error_details": "The AI's response could not be successfully parsed as JSON after multiple attempts."
})
return json.dumps({
"summary": f"I processed your request but encountered a formatting issue. Your question was about: {response_text[:100]}...",
"key_concepts": ["Unable to extract structured information"],
"learning_path": ["Please try asking in a different way"],
"resources": [],
"code_examples": [],
"advanced_topics": [],
"career_applications": []
})
def _deepseek_completion(self, prompt: str, temperature: float, system_message: str = None):
"""Call DeepSeek API for chat completion.
The helper explicitly adds a **system** message reminding the model to comply with the
schema and strictly return JSON. We have observed that without this guard-rail the
DeepSeek model occasionally omits required fields which later causes Pydantic
validation failures. Passing a clear system prompt greatly increases response
reliability.
"""
import requests
import traceback
import json
import time
api_key = DEEPSEEK_API_KEY
url = "https://api.deepseek.com/v1/chat/completions"
system_msg = (
system_message
or "You are an expert AI assistant that MUST output ONLY valid JSON strictly "
"following the user's schema instructions. Do not add any commentary, markdown "
"code fences or explanations."
)
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload_base = {
"model": self.model_name if hasattr(self, "model_name") else "deepseek-chat",
"temperature": temperature or 0.2,
"max_tokens": MAX_TOKENS,
}
def _post(messages):
start = time.time()
pl = {**payload_base, "messages": messages}
print(
f"DEBUG: DeepSeek request with {len(json.dumps(pl))} chars payload, "
f"messages={len(messages)}"
)
resp = requests.post(url, headers=headers, json=pl, timeout=150)
resp.raise_for_status()
data = resp.json()
content = data["choices"][0]["message"]["content"]
print(
f"DEBUG: DeepSeek response in {time.time()-start:.2f}s with "
f"{len(content)} chars"
)
return content
try:
# 1st attempt – full prompt
messages = [
{"role": "system", "content": system_msg},
{"role": "user", "content": prompt},
]
response_text = _post(messages)
# Quick JSON sanity check; if it fails we'll retry with a reduced prompt.
try:
json.loads(response_text.strip("`"))
return response_text
except Exception:
print(
"DEBUG: DeepSeek response not valid JSON, retrying with simplified instructions...")
# 2nd attempt – simplified prompt focusing on schema only
simple_prompt = (
"Provide ONLY the JSON that matches the schema. Do not wrap it in anything."
)
messages_retry = [
{"role": "system", "content": system_msg},
{"role": "user", "content": prompt + "\n\n" + simple_prompt},
]
return _post(messages_retry)
except Exception as e:
print(f"DEBUG: DeepSeek API call failed: {str(e)}")
print(traceback.format_exc())
raise
def _create_fallback_learning_path(self):
"""
Create a fallback learning path with default values when generation fails.
"""
import datetime
import uuid
fallback_path = {
"id": str(uuid.uuid4()),
"title": "General Learning Path",
"description": "A default learning path created when specific generation failed.",
"topic": "General Topic",
"expertise_level": "beginner",
"learning_style": "visual",
"time_commitment": "moderate",
"duration_weeks": 8,
"goals": ["Build foundational knowledge", "Develop practical skills"],
"milestones": [
{
"title": "Getting Started",
"description": "Introduction to the fundamentals.",
"estimated_hours": 10,
"resources": [
{"name": "Online Documentation",
"url": "", "type": "documentation"}
],
"skills_gained": ["Basic knowledge"]
},
{
"title": "Core Concepts",
"description": "Understanding core principles and practices.",
"estimated_hours": 15,
"resources": [
{"name": "Online Tutorial", "url": "", "type": "tutorial"}
],
"skills_gained": ["Fundamental concepts"]
}
],
"prerequisites": ["None"],
"total_hours": 25,
"created_at": datetime.datetime.now().isoformat()
}
return json.dumps(fallback_path)
def analyze_difficulty(self, content: str) -> float:
"""
Analyze the difficulty level of educational content.
Args:
content: The content to analyze
Returns:
Difficulty score between 0 (easiest) and 1 (hardest)
"""
prompt = f"""
Analyze the following educational content and rate its difficulty level on a scale from 0 to 1,
where 0 is very basic (elementary level) and 1 is extremely advanced (expert/PhD level).
Content:
{content[:1000]}...
Consider factors like:
- Technical vocabulary and jargon
- Complexity of concepts
- Prerequisites required to understand
- Density of information
Return only a numeric score between 0 and 1 with up to 2 decimal places.
"""
response = self.generate_response(prompt, temperature=0.1)
# Extract the numeric score
try:
# Look for patterns like "0.75" or "Difficulty: 0.75"
import re
matches = re.findall(r"([0-9]\.[0-9]{1,2})", response)
if matches:
score = float(matches[0])
return max(0.0, min(1.0, score)) # Ensure between 0 and 1
# If no decimal found, look for whole numbers
matches = re.findall(r"^([0-9])$", response)
if matches:
score = float(matches[0])
return max(0.0, min(1.0, score)) # Ensure between 0 and 1
return 0.5 # Default to middle difficulty
except Exception:
return 0.5 # Default to middle difficulty
def generate_resource_recommendations(
self,
topic: str,
learning_style: str,
expertise_level: str,
count: int = 5
) -> List[Dict[str, Any]]:
"""
Generate tailored resource recommendations for a topic.
Args:
topic: The topic to find resources for
learning_style: Preferred learning style
expertise_level: User's expertise level
count: Number of resources to recommend
Returns:
List of resource dictionaries
"""
prompt = f"""
Generate {count} learning resources for someone studying {topic}.
Their learning style is {learning_style} and their expertise level is {expertise_level}.
IMPORTANT: All resources MUST be in English only. Do not include resources in Portuguese, Spanish, or any other language.
For each resource, include:
1. Title (in English)
2. Type (video, article, book, interactive, course, documentation, podcast, project)
3. Description (1-2 sentences in English)
4. Difficulty level (beginner, intermediate, advanced, expert)
5. Estimated time to complete (in minutes or hours)
6. URL (create a realistic but fictional URL if needed)
Provide the response as a JSON array of resource objects. All text fields must be in English.
"""
response = self.generate_structured_response(
prompt=prompt,
output_schema="""
[
{
"title": "string",
"type": "string",
"description": "string",
"difficulty": "string",
"time_estimate": "string",
"url": "string"
}
]
""",
temperature=0.7
)
try:
resources = json.loads(response)
return resources
except Exception:
# Fallback to empty list on parsing error
return []
def generate_path(self, topic: str, expertise_level: str, learning_style: str, context: List[str] = None) -> str:
"""
Generate a learning path based on user preferences and context using RAG.
Args:
topic: The learning topic
expertise_level: User's expertise level
learning_style: User's preferred learning style
context: Optional context to consider
Returns:
Generated learning path
"""
# Combine provided context with stored context
full_context = self.context + (context or [])
# Plan if planning is enabled
if self.planning_enabled and hasattr(self, '_plan_path_generation'):
self._plan_path_generation(
topic, expertise_level, learning_style, full_context)
# Generate path with context
prompt = f"""Generate a learning path for the following topic:
Topic: {topic}
Expertise Level: {expertise_level}
Learning Style: {learning_style}
Context:
{' '.join(full_context)}
Previous answers:
{' '.join(self.memory)}
Generate a structured learning path with milestones and resources.
"""
path = self._generate_text(prompt)
# Store path in memory
self.memory.append(
f"Generated path for {topic} with {expertise_level} level and {learning_style} style")
return path
def generate_answer(self, question: str, context: Optional[List[str]] = None, temperature: Optional[float] = None) -> str:
"""
Generate an answer to a question using RAG and agentic behavior.
Args:
question: The question to answer
context: Optional context to consider
temperature: Optional temperature for response generation
Returns:
Generated answer
"""
# Combine provided context with stored context
full_context = self.context + (context or [])
# Plan if planning is enabled
if self.planning_enabled and hasattr(self, '_plan_answer_generation'):
self._plan_answer_generation(question, full_context)
# Generate answer with context
prompt = f"""Answer the following question based on the provided context:
Context:
{' '.join(full_context)}
Question: {question}"""
# Store question in memory
self.memory.append(f"Question: {question}")
# Generate and return the answer
return self.generate_response(prompt, relevant_documents=full_context, temperature=temperature)
def _plan_answer_generation(self, question: str, context: List[str]) -> None:
"""
Plan the answer generation process.
Args:
question: The question to answer
context: Context information
"""
# Analyze the question to determine the best approach
question_lower = question.lower()
# Determine if we need more context
if len(context) < 2 and not any(keyword in question_lower for keyword in ["what", "how", "why", "when", "where", "who"]):
self.context.append("Need more context for this question")
# Determine the type of question
if "how" in question_lower:
self.context.append("This is a procedural question")
elif "why" in question_lower:
self.context.append("This is an explanatory question")
elif "what" in question_lower:
self.context.append("This is a definitional question")
elif "compare" in question_lower or "difference" in question_lower:
self.context.append("This is a comparative question")
def _plan_path_generation(self, topic: str, expertise_level: str, learning_style: str, context: List[str]) -> None:
"""
Plan the learning path generation process.
Args:
topic: The learning topic
expertise_level: User's expertise level
learning_style: User's preferred learning style
context: Context information
"""
# Determine the appropriate depth and breadth based on expertise level
if expertise_level == "beginner":
self.context.append("Focus on fundamentals and basic concepts")
elif expertise_level == "intermediate":
self.context.append(
"Include practical applications and case studies")
elif expertise_level == "advanced":
self.context.append(
"Include advanced techniques and research papers")
# Adjust for learning style
if learning_style == "visual":
self.context.append("Prioritize video resources and diagrams")
elif learning_style == "auditory":
self.context.append("Prioritize podcasts and audio lectures")
elif learning_style == "reading":
self.context.append("Prioritize books and articles")
elif learning_style == "kinesthetic":
self.context.append("Prioritize hands-on projects and exercises")
|