Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ def get_session_id():
|
|
17 |
|
18 |
# π§ STaR Algorithm Implementation
|
19 |
class SelfTaughtReasoner:
|
20 |
-
def __init__(self, model_engine="
|
21 |
self.model_engine = model_engine
|
22 |
self.prompt_examples = [] # Initialize with an empty list
|
23 |
self.iterations = 0
|
@@ -35,52 +35,45 @@ class SelfTaughtReasoner:
|
|
35 |
'Answer': answer
|
36 |
})
|
37 |
|
38 |
-
def construct_prompt(self, problem: str, include_answer: bool = False, answer: str = "") ->
|
39 |
"""
|
40 |
π Constructs the prompt for the OpenAI API call.
|
|
|
41 |
"""
|
42 |
-
|
43 |
for example in self.prompt_examples:
|
44 |
-
|
45 |
-
prompt += f"Rationale: {example['Rationale']}\n"
|
46 |
-
prompt += f"Answer: {example['Answer']}\n\n"
|
47 |
|
48 |
-
|
|
|
49 |
if include_answer:
|
50 |
-
|
51 |
-
|
52 |
-
return
|
53 |
|
54 |
def generate_rationale_and_answer(self, problem: str) -> Tuple[str, str]:
|
55 |
"""
|
56 |
-
π€ Generates a rationale and answer for a given problem.
|
57 |
"""
|
58 |
-
|
59 |
try:
|
60 |
-
response = openai.
|
61 |
-
|
62 |
-
|
63 |
max_tokens=150,
|
64 |
-
temperature=0.7
|
65 |
-
top_p=1,
|
66 |
-
frequency_penalty=0,
|
67 |
-
presence_penalty=0,
|
68 |
-
stop=["\n\n", "Problem:", "Answer:"]
|
69 |
)
|
70 |
-
rationale = response.choices[0].
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
76 |
max_tokens=10,
|
77 |
-
temperature=0
|
78 |
-
top_p=1,
|
79 |
-
frequency_penalty=0,
|
80 |
-
presence_penalty=0,
|
81 |
-
stop=["\n", "\n\n", "Problem:"]
|
82 |
)
|
83 |
-
answer = answer_response.choices[0].
|
84 |
return rationale, answer
|
85 |
except Exception as e:
|
86 |
st.error(f"β Error generating rationale and answer: {e}")
|
|
|
17 |
|
18 |
# π§ STaR Algorithm Implementation
|
19 |
class SelfTaughtReasoner:
|
20 |
+
def __init__(self, model_engine="gpt-3.5-turbo"):
|
21 |
self.model_engine = model_engine
|
22 |
self.prompt_examples = [] # Initialize with an empty list
|
23 |
self.iterations = 0
|
|
|
35 |
'Answer': answer
|
36 |
})
|
37 |
|
38 |
+
def construct_prompt(self, problem: str, include_answer: bool = False, answer: str = "") -> List[dict]:
|
39 |
"""
|
40 |
π Constructs the prompt for the OpenAI API call.
|
41 |
+
Converts examples into the new chat format, where each example is a user message.
|
42 |
"""
|
43 |
+
messages = []
|
44 |
for example in self.prompt_examples:
|
45 |
+
messages.append({"role": "system", "content": f"Problem: {example['Problem']}\nRationale: {example['Rationale']}\nAnswer: {example['Answer']}\n"})
|
|
|
|
|
46 |
|
47 |
+
messages.append({"role": "user", "content": f"Problem: {problem}\nRationale:"})
|
48 |
+
|
49 |
if include_answer:
|
50 |
+
messages.append({"role": "system", "content": f"Answer: {answer}"})
|
51 |
+
|
52 |
+
return messages
|
53 |
|
54 |
def generate_rationale_and_answer(self, problem: str) -> Tuple[str, str]:
|
55 |
"""
|
56 |
+
π€ Generates a rationale and answer for a given problem using openai.ChatCompletion.create.
|
57 |
"""
|
58 |
+
messages = self.construct_prompt(problem)
|
59 |
try:
|
60 |
+
response = openai.ChatCompletion.create(
|
61 |
+
model=self.model_engine,
|
62 |
+
messages=messages,
|
63 |
max_tokens=150,
|
64 |
+
temperature=0.7
|
|
|
|
|
|
|
|
|
65 |
)
|
66 |
+
rationale = response.choices[0].message['content'].strip()
|
67 |
+
|
68 |
+
# Now generate the answer using the rationale
|
69 |
+
messages.append({"role": "system", "content": f"Rationale: {rationale}\nAnswer:"})
|
70 |
+
answer_response = openai.ChatCompletion.create(
|
71 |
+
model=self.model_engine,
|
72 |
+
messages=messages,
|
73 |
max_tokens=10,
|
74 |
+
temperature=0
|
|
|
|
|
|
|
|
|
75 |
)
|
76 |
+
answer = answer_response.choices[0].message['content'].strip()
|
77 |
return rationale, answer
|
78 |
except Exception as e:
|
79 |
st.error(f"β Error generating rationale and answer: {e}")
|