Danialebrat commited on
Commit
32585a6
·
1 Parent(s): 10458ab

- Adding multiple reasoning models

Browse files
Config_files/message_system_config.json CHANGED
@@ -21,7 +21,10 @@
21
  "AI_phrases_singeo": ["your voice deserves more"],
22
  "header_limit": 30,
23
  "message_limit": 110,
24
- "LLM_models": ["4o-mini", "gpt-4o", "gpt-4.1-nano", "gpt-4.1-mini", "gpt-3.5-turbo", "o4-mini", "o1"]
 
 
 
25
  }
26
 
27
 
 
21
  "AI_phrases_singeo": ["your voice deserves more"],
22
  "header_limit": 30,
23
  "message_limit": 110,
24
+ "LLM_models": ["gpt-4o-mini", "gpt-4o", "gpt-4.1-mini", "gpt-3.5-turbo", "o1", "o4-mini", "o1-mini", "o3-mini"],
25
+ "openai_models": ["gpt-4o-mini", "gpt-4o", "gpt-4.1-nano", "gpt-3.5-turbo", "gpt-4.1-mini"],
26
+ "reasoning": ["o1", "o4-mini", "o1-mini", "o3-mini"],
27
+ "ollama_models": ["deepseek-r1:1.5b", "gemma3:4b", "deepseek-r1:7b", "gemma3:4b"]
28
  }
29
 
30
 
Messaging_system/CoreConfig.py CHANGED
@@ -23,6 +23,7 @@ class CoreConfig:
23
  self.api_key = None # will be set by user
24
  self.model = "gpt-4o" # default -> will be set by user
25
  self.temperature = 0.7
 
26
 
27
  # will be set by user
28
  self.CTA = None
@@ -84,6 +85,8 @@ class CoreConfig:
84
 
85
  def set_llm_model(self, model):
86
  self.model = model
 
 
87
 
88
  # --------------------------------------------------------------
89
  # --------------------------------------------------------------
 
23
  self.api_key = None # will be set by user
24
  self.model = "gpt-4o" # default -> will be set by user
25
  self.temperature = 0.7
26
+ self.reasoning_model=False
27
 
28
  # will be set by user
29
  self.CTA = None
 
85
 
86
  def set_llm_model(self, model):
87
  self.model = model
88
+ if self.model in self.config_file["reasoning"]:
89
+ self.reasoning_model = True
90
 
91
  # --------------------------------------------------------------
92
  # --------------------------------------------------------------
Messaging_system/LLM.py CHANGED
@@ -15,13 +15,10 @@ import re
15
  class LLM:
16
  def __init__(self, Core):
17
  self.Core = Core
18
-
19
-
20
  self.model = None
21
  self.model_type = "openai" # valid values -> ["openai", "ollama"]
22
  self.client = None
23
  self.connect_to_llm()
24
- self.reasoning = {}
25
 
26
 
27
  def get_response(self, prompt, instructions):
@@ -39,98 +36,19 @@ class LLM:
39
  connect to selected llm -> ollama or openai connection
40
  :return:
41
  """
42
- openai_models = ["4o-mini", "gpt-4o", "gpt-4.1-nano", "gpt-3.5-turbo", "gpt-4.1-mini"]
43
- reasoning = ["o1", "o4-mini"]
44
- ollama_models = ["deepseek-r1:1.5b", "gemma3:4b", "deepseek-r1:7b", "gemma3:4b"]
45
 
46
- if self.Core.model in openai_models:
47
  self.model_type = "openai"
48
- if self.Core.model in reasoning:
49
- self.reasoning= {"effort": "medium"}
50
 
51
- if self.Core.model in ollama_models:
52
  self.model_type = "ollama"
53
  self.client = ollama.Client()
54
 
55
  self.model = self.Core.model
56
 
57
 
58
- def get_message_openai(self, prompt, instructions, max_retries=4):
59
-
60
-
61
- openai.api_key = self.Core.api_key
62
- client = OpenAI(api_key=self.Core.api_key)
63
-
64
- for attempt in range(max_retries):
65
- try:
66
- response = client.responses.create(
67
- model=self.Core.model,
68
- input=[{"role": "system", "content": instructions},
69
- {"role": "user", "content": prompt}],
70
- text={
71
- "format": {
72
- "type": "json_object"
73
- }
74
- },
75
- reasoning=self.reasoning,
76
- max_output_tokens=500,
77
- temperature=self.Core.temperature,
78
- top_p=1,
79
- tools=[],
80
- )
81
-
82
- tokens = {
83
- 'prompt_tokens': response.usage.prompt_tokens,
84
- 'completion_tokens': response.usage.completion_tokens,
85
- 'total_tokens': response.usage.total_tokens
86
- }
87
-
88
- try:
89
- content = response.choices[0].message.content
90
-
91
- # Extract JSON code block
92
-
93
- output = json.loads(content)
94
-
95
- if 'message' not in output or 'header' not in output:
96
- print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
97
- continue # Continue to next attempt
98
-
99
- else:
100
- if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
101
- output["message"].strip()) > self.Core.config_file["message_limit"]:
102
- print(
103
- f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
104
- continue
105
-
106
- # validating the JSON
107
- self.Core.total_tokens['prompt_tokens'] += tokens['prompt_tokens']
108
- self.Core.total_tokens['completion_tokens'] += tokens['completion_tokens']
109
- self.Core.temp_token_counter += tokens['total_tokens']
110
- return output
111
-
112
- except json.JSONDecodeError:
113
- print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
114
-
115
- except openai.APIConnectionError as e:
116
- print("The server could not be reached")
117
- print(e.__cause__) # an underlying Exception, likely raised within httpx.
118
- except openai.RateLimitError as e:
119
- print("A 429 status code was received; we should back off a bit.")
120
- except openai.APIStatusError as e:
121
- print("Another non-200-range status code was received")
122
- print(e.status_code)
123
- print(e.response)
124
-
125
- print("Max retries exceeded. Returning empty response.")
126
- return None
127
-
128
-
129
-
130
  # def get_message_openai(self, prompt, instructions, max_retries=4):
131
- # """
132
- # sending the prompt to openai LLM and get back the response
133
- # """
134
  #
135
  # openai.api_key = self.Core.api_key
136
  # client = OpenAI(api_key=self.Core.api_key)
@@ -139,14 +57,12 @@ class LLM:
139
  # try:
140
  # response = client.chat.completions.create(
141
  # model=self.Core.model,
 
142
  # response_format={"type": "json_object"},
143
- # messages=[
144
- # {"role": "system", "content": instructions},
145
- # {"role": "user", "content": prompt}
146
- # ],
147
- # max_tokens=500,
148
- # n=1,
149
  # temperature=self.Core.temperature,
 
150
  # )
151
  #
152
  # tokens = {
@@ -195,6 +111,88 @@ class LLM:
195
  # print("Max retries exceeded. Returning empty response.")
196
  # return None
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  # ======================================================================
199
 
200
  def get_message_ollama(self, prompt, instructions, max_retries=10):
@@ -255,24 +253,6 @@ class LLM:
255
 
256
  # ======================================================================
257
 
258
- # def preprocess_and_parse_json(self, response):
259
- # # Remove any leading/trailing whitespace and newlines
260
- # if response.startswith('```json') and response.endswith('```'):
261
- # response = response[len('```json'):-len('```')].strip()
262
- #
263
- # # Parse the cleaned response into a JSON object
264
- # try:
265
- # json_object = json.loads(response)
266
- # return json_object
267
- # except json.JSONDecodeError as e:
268
- # print(f"Failed to parse JSON: {e}")
269
- # return None
270
-
271
- # =====================================================================
272
-
273
- import re
274
- import json
275
-
276
  def preprocess_and_parse_json(self, response: str):
277
  """
278
  Cleans an LLM response by removing <think> tags and extracting JSON
 
15
  class LLM:
16
  def __init__(self, Core):
17
  self.Core = Core
 
 
18
  self.model = None
19
  self.model_type = "openai" # valid values -> ["openai", "ollama"]
20
  self.client = None
21
  self.connect_to_llm()
 
22
 
23
 
24
  def get_response(self, prompt, instructions):
 
36
  connect to selected llm -> ollama or openai connection
37
  :return:
38
  """
 
 
 
39
 
40
+ if self.Core.model in self.Core.config_file["openai_models"]:
41
  self.model_type = "openai"
 
 
42
 
43
+ if self.Core.model in self.Core.config_file["ollama_models"]:
44
  self.model_type = "ollama"
45
  self.client = ollama.Client()
46
 
47
  self.model = self.Core.model
48
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # def get_message_openai(self, prompt, instructions, max_retries=4):
51
+ #
 
 
52
  #
53
  # openai.api_key = self.Core.api_key
54
  # client = OpenAI(api_key=self.Core.api_key)
 
57
  # try:
58
  # response = client.chat.completions.create(
59
  # model=self.Core.model,
60
+ # messages= [{"role": "system", "content": instructions},{"role": "user", "content": prompt}],
61
  # response_format={"type": "json_object"},
62
+ # reasoning_effort=self.reasoning,
63
+ # max_tokens=1000,
 
 
 
 
64
  # temperature=self.Core.temperature,
65
+ # tools=[]
66
  # )
67
  #
68
  # tokens = {
 
111
  # print("Max retries exceeded. Returning empty response.")
112
  # return None
113
 
114
+
115
+
116
+ def get_message_openai(self, prompt, instructions, max_retries=4):
117
+ """
118
+ sending the prompt to openai LLM and get back the response
119
+ """
120
+
121
+ openai.api_key = self.Core.api_key
122
+ client = OpenAI(api_key=self.Core.api_key)
123
+
124
+ for attempt in range(max_retries):
125
+ try:
126
+ if self.Core.reasoning_model:
127
+ response = client.chat.completions.create(
128
+ model=self.Core.model,
129
+ response_format={"type": "json_object"},
130
+ messages=[
131
+ {"role": "system", "content": instructions},
132
+ {"role": "user", "content": prompt}
133
+ ],
134
+ reasoning_effort="medium",
135
+ n=1,
136
+ )
137
+
138
+ else:
139
+ response = client.chat.completions.create(
140
+ model=self.Core.model,
141
+ response_format={"type": "json_object"},
142
+ messages=[
143
+ {"role": "system", "content": instructions},
144
+ {"role": "user", "content": prompt}
145
+ ],
146
+ n=1,
147
+ temperature=self.Core.temperature
148
+ )
149
+
150
+ tokens = {
151
+ 'prompt_tokens': response.usage.prompt_tokens,
152
+ 'completion_tokens': response.usage.completion_tokens,
153
+ 'total_tokens': response.usage.total_tokens
154
+ }
155
+
156
+ try:
157
+ content = response.choices[0].message.content
158
+
159
+ # Extract JSON code block
160
+
161
+ output = json.loads(content)
162
+
163
+ if 'message' not in output or 'header' not in output:
164
+ print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
165
+ continue # Continue to next attempt
166
+
167
+ else:
168
+ if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
169
+ output["message"].strip()) > self.Core.config_file["message_limit"]:
170
+ print(
171
+ f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
172
+ continue
173
+
174
+ # validating the JSON
175
+ self.Core.total_tokens['prompt_tokens'] += tokens['prompt_tokens']
176
+ self.Core.total_tokens['completion_tokens'] += tokens['completion_tokens']
177
+ self.Core.temp_token_counter += tokens['total_tokens']
178
+ return output
179
+
180
+ except json.JSONDecodeError:
181
+ print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
182
+
183
+ except openai.APIConnectionError as e:
184
+ print("The server could not be reached")
185
+ print(e.__cause__) # an underlying Exception, likely raised within httpx.
186
+ except openai.RateLimitError as e:
187
+ print("A 429 status code was received; we should back off a bit.")
188
+ except openai.APIStatusError as e:
189
+ print("Another non-200-range status code was received")
190
+ print(e.status_code)
191
+ print(e.response)
192
+
193
+ print("Max retries exceeded. Returning empty response.")
194
+ return None
195
+
196
  # ======================================================================
197
 
198
  def get_message_ollama(self, prompt, instructions, max_retries=10):
 
253
 
254
  # ======================================================================
255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  def preprocess_and_parse_json(self, response: str):
257
  """
258
  Cleans an LLM response by removing <think> tags and extracting JSON
Messaging_system/PromptEng.py CHANGED
@@ -2,6 +2,7 @@
2
  This is the prompt engineering layer to modifty the prompt for better perfromance
3
  """
4
  import openai
 
5
  from openai import OpenAI
6
 
7
 
@@ -18,8 +19,25 @@ class PromptEngine:
18
  :return:
19
  """
20
 
21
- new_prompt = self.get_llm_response(prompt)
22
- return new_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  # ============================================================
25
  def llm_instructions(self):
@@ -39,20 +57,33 @@ class PromptEngine:
39
 
40
  openai.api_key = self.Core.api_key
41
  client = OpenAI(api_key=self.Core.api_key)
 
 
42
 
43
  for attempt in range(max_retries):
44
  try:
45
- response = client.chat.completions.create(
46
- model=self.Core.model,
47
- response_format={"type": "text"},
48
- messages=[
49
- {"role": "system", "content": self.llm_instructions()},
50
- {"role": "user", "content": prompt}
51
- ],
52
- max_tokens=1000,
53
- n=1,
54
- temperature=self.Core.temperature,
55
- )
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  tokens = {
58
  'prompt_tokens': response.usage.prompt_tokens,
 
2
  This is the prompt engineering layer to modifty the prompt for better perfromance
3
  """
4
  import openai
5
+ from fontTools.ttLib.tables.ttProgram import instructions
6
  from openai import OpenAI
7
 
8
 
 
19
  :return:
20
  """
21
 
22
+ new_prompt = f"""
23
+
24
+ Modify below prompt following best prompt engineering methods. return only the new prompt as a text.
25
+ modify the prompt and instructions in <original_prompt> tag to maximimize better results by providing the new prompt.
26
+
27
+ ### Original prompt
28
+
29
+ <original_prompt>
30
+
31
+ {prompt}
32
+
33
+ </original_prompt>
34
+
35
+ output the new prompt as text without any additional information.
36
+
37
+ """
38
+
39
+ final_prompt = self.get_llm_response(new_prompt)
40
+ return final_prompt
41
 
42
  # ============================================================
43
  def llm_instructions(self):
 
57
 
58
  openai.api_key = self.Core.api_key
59
  client = OpenAI(api_key=self.Core.api_key)
60
+ reasoning = self.Core.reasoning_model
61
+ system_prompt = self.llm_instructions()
62
 
63
  for attempt in range(max_retries):
64
  try:
65
+ if reasoning:
66
+ response = client.chat.completions.create(
67
+ model=self.Core.model,
68
+ response_format={"type": "text"},
69
+ messages=[
70
+ {"role": "system", "content": system_prompt},
71
+ {"role": "user", "content": prompt}
72
+ ],
73
+ reasoning_effort="medium",
74
+ n=1,
75
+ )
76
+ else:
77
+ response = client.chat.completions.create(
78
+ model=self.Core.model,
79
+ response_format={"type": "text"},
80
+ messages=[
81
+ {"role": "system", "content": system_prompt},
82
+ {"role": "user", "content": prompt}
83
+ ],
84
+ n=1,
85
+ temperature=self.Core.temperature
86
+ )
87
 
88
  tokens = {
89
  'prompt_tokens': response.usage.prompt_tokens,
messaging_main_test.py CHANGED
@@ -164,8 +164,10 @@ if __name__ == "__main__":
164
  segment_name = "no_recent_activity"
165
  permes = Permes()
166
 
 
 
167
  users_message = permes.create_personalize_messages(session=session,
168
- model="gemma3:4b",
169
  users=users,
170
  brand=brand,
171
  config_file=config_file,
 
164
  segment_name = "no_recent_activity"
165
  permes = Permes()
166
 
167
+ # o3-mini o1-mini o4-mini o1
168
+
169
  users_message = permes.create_personalize_messages(session=session,
170
+ model="o4-mini",
171
  users=users,
172
  brand=brand,
173
  config_file=config_file,