|
98 | 98 | from PIL import Image
|
99 | 99 | import pillow_heif
|
100 | 100 |
|
| 101 | +# Enable tracemalloc for memory usage |
| 102 | +import tracemalloc |
| 103 | +tracemalloc.start() |
| 104 | + |
101 | 105 | # Ensure pillow_heif is properly registered with PIL
|
102 | 106 | pillow_heif.register_heif_opener()
|
103 | 107 |
|
@@ -544,7 +548,7 @@ async def get_weather(location):
|
544 | 548 | return response.text
|
545 | 549 | else:
|
546 | 550 | return "Unable to fetch weather for %s" % location
|
547 |
| - |
| 551 | + |
548 | 552 | # Function - Get stock price for company
|
549 | 553 | async def get_stock(company):
|
550 | 554 | if ALPHA_KEY == "alpha_key":
|
@@ -712,6 +716,7 @@ async def home(format: str = None):
|
712 | 716 | "Alpha Vantage API Key (ALPHA_KEY)": "************" if ALPHA_KEY != "" else "Not Set",
|
713 | 717 | "Toxicity Threshold (TOXIC_THRESHOLD)": TOXIC_THRESHOLD,
|
714 | 718 | "Extra Body Parameters (EXTRA_BODY)": EXTRA_BODY,
|
| 719 | + "Thinking Mode (THINKING)": THINKING, |
715 | 720 | }
|
716 | 721 | if format == "json":
|
717 | 722 | return data
|
@@ -875,28 +880,37 @@ async def send_update(session_id):
|
875 | 880 | if client[session_id]["prompt"] == "":
|
876 | 881 | await sio.sleep(0.1)
|
877 | 882 | else:
|
878 |
| - if client[session_id]["cot"]: |
879 |
| - # Remember original prompt |
880 |
| - client[session_id]["cot_prompt"] = client[session_id]["prompt"] |
881 |
| - # Check to see if the prompt needs COT processing |
882 |
| - cot_check = expand_prompt(prompts["chain_of_thought_check"], {"prompt": client[session_id]["prompt"]}) |
883 |
| - debug("Running CoT check") |
884 |
| - # Ask LLM for answers |
885 |
| - response = await ask_llm(cot_check) |
886 |
| - if "a" in response.lower() or "d" in response.lower() or client[session_id]["cot_always"]: |
887 |
| - debug("Running deep thinking CoT to answer") |
888 |
| - # Build prompt for Chain of Thought and create copy of context |
889 |
| - cot_prompt = expand_prompt(prompts["chain_of_thought"], {"prompt": client[session_id]["prompt"]}) |
890 |
| - temp_context = client[session_id]["context"].copy() |
891 |
| - temp_context.append({"role": "user", "content": cot_prompt}) |
892 |
| - # Send thinking status to client and ask LLM for answer |
893 |
| - await sio.emit('update', {'update': 'Thinking... ', 'voice': 'ai'},room=session_id) |
894 |
| - answer = await ask_context(temp_context) |
895 |
| - await sio.emit('update', {'update': '\n\n', 'voice': 'ai'},room=session_id) |
896 |
| - # Load request for CoT conclusion into conversational thread |
897 |
| - cot_prompt = expand_prompt(prompts["chain_of_thought_summary"], {"context_str": answer, |
898 |
| - "prompt": client[session_id]["cot_prompt"]}) |
899 |
| - client[session_id]["prompt"] = cot_prompt |
| 883 | + # Check to see of CoT is enabled but not while processing a file/image |
| 884 | + client_cot = client[session_id]["cot"] |
| 885 | + client_image_data = client[session_id]["image_data"] |
| 886 | + client_visible = client[session_id]["visible"] |
| 887 | + if client_cot and not client_image_data and client_visible: |
| 888 | + try: |
| 889 | + # Remember original prompt |
| 890 | + client[session_id]["cot_prompt"] = client[session_id]["prompt"] |
| 891 | + # Check to see if the prompt needs COT processing |
| 892 | + cot_check = expand_prompt(prompts["chain_of_thought_check"], {"prompt": client[session_id]["prompt"]}) |
| 893 | + debug("Running CoT check") |
| 894 | + # Ask LLM for answers |
| 895 | + response = await ask_llm(cot_check) |
| 896 | + if "a" in response.lower() or "d" in response.lower() or client[session_id]["cot_always"]: |
| 897 | + debug("Running deep thinking CoT to answer") |
| 898 | + # Build prompt for Chain of Thought and create copy of context |
| 899 | + cot_prompt = expand_prompt(prompts["chain_of_thought"], {"prompt": client[session_id]["prompt"]}) |
| 900 | + temp_context = client[session_id]["context"].copy() |
| 901 | + temp_context.append({"role": "user", "content": cot_prompt}) |
| 902 | + # Send thinking status to client and ask LLM for answer |
| 903 | + await sio.emit('update', {'update': 'Thinking... ', 'voice': 'ai'},room=session_id) |
| 904 | + answer = await ask_context(temp_context) |
| 905 | + # Load request for CoT conclusion into conversational thread |
| 906 | + cot_prompt = expand_prompt(prompts["chain_of_thought_summary"], {"context_str": answer, |
| 907 | + "prompt": client[session_id]["cot_prompt"]}) |
| 908 | + client[session_id]["prompt"] = cot_prompt |
| 909 | + except Exception as erro: |
| 910 | + log(f"CoT error - continuing with original prompt: {erro}") |
| 911 | + await sio.emit('update', {'update': '\n\n', 'voice': 'ai'},room=session_id) |
| 912 | + else: |
| 913 | + client_cot = False |
900 | 914 | try:
|
901 | 915 | # Ask LLM for answers
|
902 | 916 | response= await ask(client[session_id]["prompt"],session_id)
|
@@ -931,7 +945,7 @@ async def send_update(session_id):
|
931 | 945 | client[session_id]["references"] = ""
|
932 | 946 | if not ONESHOT:
|
933 | 947 | # If COT mode replace CoT context in conversation thread with user prompt
|
934 |
| - if client[session_id]["cot"]: |
| 948 | + if client_cot: |
935 | 949 | client[session_id]["context"].pop()
|
936 | 950 | client[session_id]["context"].append({"role": "user", "content": client[session_id]["cot_prompt"]} )
|
937 | 951 | # Remember answer
|
|
0 commit comments