renamed from app/utils.py
Browse files
app/utils.py → core/generation_utils.py
RENAMED
@@ -1,16 +1,9 @@
|
|
1 |
import json
|
2 |
-
import re
|
3 |
-
import sys
|
4 |
-
from turtle import color
|
5 |
from typing import Generator
|
6 |
-
from textwrap import dedent
|
7 |
-
from litellm.types.utils import ModelResponse
|
8 |
from pydantic import ValidationError
|
9 |
from core.llms.base_llm import BaseLLM
|
10 |
from core.prompts import cot
|
11 |
from core.types import ThoughtSteps, ThoughtStepsDisplay
|
12 |
-
from core.prompts import REVIEW_PROMPT, SYSTEM_PROMPT ,FINAL_ANSWER_PROMPT, HELPFUL_ASSISTANT_PROMPT
|
13 |
-
import os
|
14 |
import time
|
15 |
from core.utils import parse_with_fallback
|
16 |
from termcolor import colored
|
@@ -25,7 +18,7 @@ from tenacity import retry, stop_after_attempt, wait_incrementing
|
|
25 |
|
26 |
|
27 |
|
28 |
-
@retry(stop=stop_after_attempt(3), wait=wait_incrementing())
|
29 |
def cot_or_da_func(problem: str, llm: BaseLLM = None, **kwargs) -> COTorDAPromptOutput:
|
30 |
|
31 |
cot_decision_message = [
|
@@ -45,13 +38,6 @@ def cot_or_da_func(problem: str, llm: BaseLLM = None, **kwargs) -> COTorDAPrompt
|
|
45 |
return cot_or_da
|
46 |
|
47 |
|
48 |
-
def get_system_prompt(decision: Decision) -> str:
|
49 |
-
if decision == Decision.CHAIN_OF_THOUGHT:
|
50 |
-
return cot.SYSTEM_PROMPT
|
51 |
-
elif decision == Decision.DIRECT_ANSWER:
|
52 |
-
return HELPFUL_ASSISTANT_PROMPT
|
53 |
-
else:
|
54 |
-
raise ValueError(f"Invalid decision: {decision}")
|
55 |
|
56 |
def set_system_message(messages: list[dict], system_prompt: str) -> list[dict]:
|
57 |
#check if any system message already exists
|
@@ -75,7 +61,7 @@ def generate_answer(messages: list[dict], max_steps: int = 20, llm: BaseLLM = No
|
|
75 |
|
76 |
system_prompt += f" , {cot.SYSTEM_PROMPT_EXAMPLE_JSON}"
|
77 |
review_prompt += f" , {cot.REVIEW_PROMPT_EXAMPLE_JSON}"
|
78 |
-
final_answer_prompt += f" , {cot.
|
79 |
|
80 |
MESSAGES = set_system_message(messages, system_prompt)
|
81 |
|
@@ -99,12 +85,12 @@ def generate_answer(messages: list[dict], max_steps: int = 20, llm: BaseLLM = No
|
|
99 |
if thought.is_final_answer and not thought.next_step and not force_max_steps:
|
100 |
break
|
101 |
|
102 |
-
MESSAGES.append({"role": "user", "content": f"{
|
103 |
|
104 |
time.sleep(sleeptime)
|
105 |
|
106 |
# Get the final answer after all thoughts are processed
|
107 |
-
MESSAGES += [{"role": "user", "content": f"{final_answer_prompt}
|
108 |
|
109 |
raw_final_answers = llm.chat(messages=MESSAGES, **kwargs)
|
110 |
final_answer = raw_final_answers.choices[0].message.content
|
|
|
1 |
import json
|
|
|
|
|
|
|
2 |
from typing import Generator
|
|
|
|
|
3 |
from pydantic import ValidationError
|
4 |
from core.llms.base_llm import BaseLLM
|
5 |
from core.prompts import cot
|
6 |
from core.types import ThoughtSteps, ThoughtStepsDisplay
|
|
|
|
|
7 |
import time
|
8 |
from core.utils import parse_with_fallback
|
9 |
from termcolor import colored
|
|
|
18 |
|
19 |
|
20 |
|
21 |
+
@retry(stop=stop_after_attempt(3), wait=wait_incrementing(increment=1000))
|
22 |
def cot_or_da_func(problem: str, llm: BaseLLM = None, **kwargs) -> COTorDAPromptOutput:
|
23 |
|
24 |
cot_decision_message = [
|
|
|
38 |
return cot_or_da
|
39 |
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def set_system_message(messages: list[dict], system_prompt: str) -> list[dict]:
|
43 |
#check if any system message already exists
|
|
|
61 |
|
62 |
system_prompt += f" , {cot.SYSTEM_PROMPT_EXAMPLE_JSON}"
|
63 |
review_prompt += f" , {cot.REVIEW_PROMPT_EXAMPLE_JSON}"
|
64 |
+
final_answer_prompt += f" , {cot.FINAL_ANSWER_EXAMPLE_JSON}"
|
65 |
|
66 |
MESSAGES = set_system_message(messages, system_prompt)
|
67 |
|
|
|
85 |
if thought.is_final_answer and not thought.next_step and not force_max_steps:
|
86 |
break
|
87 |
|
88 |
+
MESSAGES.append({"role": "user", "content": f"{review_prompt} {thought.critic}"})
|
89 |
|
90 |
time.sleep(sleeptime)
|
91 |
|
92 |
# Get the final answer after all thoughts are processed
|
93 |
+
MESSAGES += [{"role": "user", "content": f"{final_answer_prompt}"}]
|
94 |
|
95 |
raw_final_answers = llm.chat(messages=MESSAGES, **kwargs)
|
96 |
final_answer = raw_final_answers.choices[0].message.content
|