@@ -204,8 +204,7 @@ def fix_generated_code(self, code, code_language, fix_instructions=""):
204
204
max_output_tokens = self .max_output_tokens ,
205
205
top_k = self .top_k ,
206
206
top_p = self .top_p ,
207
- stop_sequences = [],
208
- safety_settings = [{"category" :"HARM_CATEGORY_DEROGATORY" ,"threshold" :1 },{"category" :"HARM_CATEGORY_TOXICITY" ,"threshold" :1 },{"category" :"HARM_CATEGORY_VIOLENCE" ,"threshold" :2 },{"category" :"HARM_CATEGORY_SEXUAL" ,"threshold" :2 },{"category" :"HARM_CATEGORY_MEDICAL" ,"threshold" :2 },{"category" :"HARM_CATEGORY_DANGEROUS" ,"threshold" :2 }],
207
+ stop_sequences = []
209
208
)
210
209
211
210
if palm_completion :
@@ -225,4 +224,64 @@ def fix_generated_code(self, code, code_language, fix_instructions=""):
225
224
logger .error ("Error in code fixing: Please enter a valid code and language." )
226
225
except Exception as exception :
227
226
st .toast (f"Error in code fixing: { exception } " , icon = "❌" )
228
- logger .error (f"Error in code fixing: { traceback .format_exc ()} " )
227
+ logger .error (f"Error in code fixing: { traceback .format_exc ()} " )
228
+
229
+ def convert_generated_code (self , code , code_language ):
230
+ """
231
+ Function to convert the generated code to a different language using the palm API.
232
+ """
233
+ try :
234
+ # Check for valid code
235
+ if not code or len (code ) == 0 :
236
+ logger .error ("Error in code conversion: Please enter a valid code." )
237
+ return
238
+
239
+ logger .info (f"Converting code" )
240
+ if code and len (code ) > 0 :
241
+ logger .info (f"Converting code { code [:100 ]} ... to language { code_language } " )
242
+
243
+ # Improved instructions template
244
+ template = f"""
245
+ Task: Convert the code snippet provided below to the { code_language } programming language, following the given instructions:
246
+
247
+ { code }
248
+
249
+ Instructions for Conversion:
250
+ 1. Identify the functionality of the original code.
251
+ 2. Translate the code into the { code_language } programming language, maintaining the same functionality.
252
+ 3. Verify that the converted code is displayed in the output.
253
+
254
+ Please make sure only the converted code should be included in the output.
255
+ """
256
+
257
+ # Prompt Templates
258
+ code_template = template
259
+
260
+ # LLM Chains definition
261
+ # Create a chain that generates the code
262
+ palm_completion = palm .generate_text (
263
+ model = self .model ,
264
+ prompt = code_template ,
265
+ temperature = self .temperature ,
266
+ max_output_tokens = self .max_output_tokens ,
267
+ top_k = self .top_k ,
268
+ top_p = self .top_p ,
269
+ stop_sequences = []
270
+ )
271
+
272
+ if palm_completion :
273
+ # Extracted code from the palm completion
274
+ code = palm_completion .result
275
+ extracted_code = self .utils .extract_code (code )
276
+
277
+ # Check if the code or extracted code is not empty or null
278
+ if not code or not extracted_code :
279
+ raise Exception ("Error: Generated code or extracted code is empty or null." )
280
+ else :
281
+ return extracted_code
282
+ else :
283
+ raise Exception ("Error in code conversion: Please enter a valid code." )
284
+ else :
285
+ logger .error ("Error in code conversion: Please enter a valid code and language." )
286
+ except Exception as exception :
287
+ logger .error (f"Error in code conversion: { traceback .format_exc ()} " )
0 commit comments