awacke1 commited on
Commit
a033cb4
Β·
verified Β·
1 Parent(s): a509ab0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -32
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="text-davinci-003"):
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 = "") -> str:
39
  """
40
  πŸ“ Constructs the prompt for the OpenAI API call.
 
41
  """
42
- prompt = ""
43
  for example in self.prompt_examples:
44
- prompt += f"Problem: {example['Problem']}\n"
45
- prompt += f"Rationale: {example['Rationale']}\n"
46
- prompt += f"Answer: {example['Answer']}\n\n"
47
 
48
- prompt += f"Problem: {problem}\n"
 
49
  if include_answer:
50
- prompt += f"Answer (as hint): {answer}\n"
51
- prompt += "Rationale:"
52
- return prompt
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
- prompt = self.construct_prompt(problem)
59
  try:
60
- response = openai.Completion.create(
61
- engine=self.model_engine,
62
- prompt=prompt,
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].text.strip()
71
- # πŸ“ Now generate the answer using the rationale
72
- prompt += f" {rationale}\nAnswer:"
73
- answer_response = openai.Completion.create(
74
- engine=self.model_engine,
75
- prompt=prompt,
 
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].text.strip()
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}")