Danialebrat commited on
Commit
690a763
·
1 Parent(s): 4de4951

adding Ollama.py

Browse files
local_llm/LocalLM.py → Messaging_system/Ollama.py RENAMED
@@ -18,23 +18,97 @@ class LocalLM:
18
  # return response.response
19
 
20
  def preprocess_and_parse_json(self, response):
 
21
  # Remove any leading/trailing whitespace and newlines
22
  if response.startswith('```json') and response.endswith('```'):
23
- cleaned_response = response[len('```json'):-len('```')].strip()
24
 
25
  # Parse the cleaned response into a JSON object
26
  try:
27
- json_object = json.loads(cleaned_response)
28
  return json_object
29
  except json.JSONDecodeError as e:
30
  print(f"Failed to parse JSON: {e}")
31
  return None
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def get_llm_response(self, prompt, mode, max_retries=10):
34
  """
35
  Send the prompt to the LLM and get back the response.
36
  Includes handling for GPU memory issues by clearing cache and waiting before retry.
37
  """
 
38
  for attempt in range(max_retries):
39
  try:
40
  # Try generating the response
 
18
  # return response.response
19
 
20
  def preprocess_and_parse_json(self, response):
21
+
22
  # Remove any leading/trailing whitespace and newlines
23
  if response.startswith('```json') and response.endswith('```'):
24
+ response = response[len('```json'):-len('```')].strip()
25
 
26
  # Parse the cleaned response into a JSON object
27
  try:
28
+ json_object = json.loads(response)
29
  return json_object
30
  except json.JSONDecodeError as e:
31
  print(f"Failed to parse JSON: {e}")
32
  return None
33
 
34
+
35
+ def get_llm_response(self, prompt, max_retries=4):
36
+ """
37
+ sending the prompt to the LLM and get back the response
38
+ """
39
+
40
+ openai.api_key = self.Core.api_key
41
+ instructions = self.llm_instructions()
42
+ client = OpenAI(api_key=self.Core.api_key)
43
+
44
+ for attempt in range(max_retries):
45
+ try:
46
+ response = client.chat.completions.create(
47
+ model=self.Core.model,
48
+ response_format={"type": "json_object"},
49
+ messages=[
50
+ {"role": "system", "content": instructions},
51
+ {"role": "user", "content": prompt}
52
+ ],
53
+ max_tokens=500,
54
+ n=1,
55
+ # temperature=self.Core.temperature,
56
+ temperature=0.7,
57
+ )
58
+
59
+ tokens = {
60
+ 'prompt_tokens': response.usage.prompt_tokens,
61
+ 'completion_tokens': response.usage.completion_tokens,
62
+ 'total_tokens': response.usage.total_tokens
63
+ }
64
+
65
+ try:
66
+ content = response.choices[0].message.content
67
+
68
+ # Extract JSON code block
69
+
70
+ output = json.loads(content)
71
+ # output = json.loads(response.choices[0].message.content)
72
+
73
+ if 'message' not in output or 'header' not in output:
74
+ print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
75
+ continue # Continue to next attempt
76
+
77
+ else:
78
+ if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
79
+ output["message"].strip()) > self.Core.config_file["message_limit"]:
80
+ print(
81
+ f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
82
+ continue
83
+
84
+ # validating the JSON
85
+ self.Core.total_tokens['prompt_tokens'] += tokens['prompt_tokens']
86
+ self.Core.total_tokens['completion_tokens'] += tokens['completion_tokens']
87
+ self.Core.temp_token_counter += tokens['total_tokens']
88
+ return output
89
+
90
+ except json.JSONDecodeError:
91
+ print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
92
+
93
+ except openai.APIConnectionError as e:
94
+ print("The server could not be reached")
95
+ print(e.__cause__) # an underlying Exception, likely raised within httpx.
96
+ except openai.RateLimitError as e:
97
+ print("A 429 status code was received; we should back off a bit.")
98
+ except openai.APIStatusError as e:
99
+ print("Another non-200-range status code was received")
100
+ print(e.status_code)
101
+ print(e.response)
102
+
103
+ print("Max retries exceeded. Returning empty response.")
104
+ return None
105
+
106
  def get_llm_response(self, prompt, mode, max_retries=10):
107
  """
108
  Send the prompt to the LLM and get back the response.
109
  Includes handling for GPU memory issues by clearing cache and waiting before retry.
110
  """
111
+
112
  for attempt in range(max_retries):
113
  try:
114
  # Try generating the response
messaging_main_test.py CHANGED
@@ -131,7 +131,7 @@ if __name__ == "__main__":
131
  number_of_samples = 3
132
 
133
  # number of messages to generate
134
- number_of_messages = 5
135
  instructionset = {
136
  1: "Be highly motivational positive and kind",
137
  2: "Be highly motivational positive and kind",
 
131
  number_of_samples = 3
132
 
133
  # number of messages to generate
134
+ number_of_messages = 3
135
  instructionset = {
136
  1: "Be highly motivational positive and kind",
137
  2: "Be highly motivational positive and kind",