| import replicate |
| import google.generativeai as genai |
| import os |
| import requests |
| import boto3 |
| import sagemaker |
| import json |
| import subprocess |
| import time |
|
|
| |
| |
| def GPT3_5(preprompt, prompt, client): |
| |
| messages = [ |
| {"role": "system", "content": preprompt}, |
| {"role": "user", "content": prompt}, |
| ] |
|
|
| while True: |
| try: |
| response = client.chat.completions.create( |
| model="gpt-3.5-turbo", |
| messages=messages, |
| ) |
| |
| |
| return response.choices[0].message.content |
| except Exception as e: |
| s = str(e) |
| if "Rate limit is exceeded" in s: |
| time.sleep(30) |
| continue |
| else: |
| return "" |
|
|
| def GPT4(preprompt, prompt, client): |
| |
| |
| messages = [ |
| {"role": "system", "content": preprompt}, |
| {"role": "user", "content": prompt}, |
| ] |
| while True: |
| try: |
| response = client.chat.completions.create( |
| model="gpt-4-turbo", |
| messages=messages, |
| ) |
|
|
| |
| |
| return response.choices[0].message.content |
| except Exception as e: |
| s = str(e) |
| if "Rate limit is exceeded" in s: |
| time.sleep(30) |
| continue |
| else: |
| return "" |
|
|
| def Codellama7b(preprompt, prompt): |
| for i in range(2): |
| try: |
| output = replicate.run( |
| "meta/codellama-7b-instruct:aac3ab196f8a75729aab9368cd45ea6ad3fc793b6cda93b1ded17299df369332", |
| input={ |
| "top_k": 250, |
| "top_p": 0.95, |
| "prompt": prompt, |
| "max_tokens": 500, |
| "temperature": 0.95, |
| "system_prompt": preprompt, |
| "repeat_penalty": 1.1, |
| "presence_penalty": 0, |
| "frequency_penalty": 0, |
| }, |
| ) |
| output_string = "" |
| for item in output: |
| output_string += item |
| print(output_string) |
| return output_string |
| except Exception as e: |
| s = str(e) |
| print(s) |
| if "status: 502" in s or "Prediction interrupted" in s: |
| time.sleep(10) |
| return "" |
|
|
|
|
| def Codellama13b(preprompt, prompt): |
| for i in range(2): |
| try: |
| output = replicate.run( |
| "meta/codellama-13b-instruct:a5e2d67630195a09b96932f5fa541fe64069c97d40cd0b69cdd91919987d0e7f", |
| input={ |
| "top_k": 250, |
| "top_p": 0.95, |
| "prompt": prompt, |
| "max_tokens": 500, |
| "temperature": 0.95, |
| "system_prompt": preprompt, |
| "repeat_penalty": 1.1, |
| "presence_penalty": 0, |
| "frequency_penalty": 0, |
| }, |
| ) |
| output_string = "" |
| for item in output: |
| output_string += item |
| print(output_string) |
| return output_string |
| except Exception as e: |
| s = str(e) |
| print(s) |
| if "status: 502" in s or "Prediction interrupted" in s: |
| time.sleep(10) |
| return "" |
|
|
|
|
| def Codellama34b(preprompt, prompt): |
| for i in range(2): |
| try: |
| output = replicate.run( |
| "meta/codellama-34b-instruct:eeb928567781f4e90d2aba57a51baef235de53f907c214a4ab42adabf5bb9736", |
| input={ |
| "top_k": 250, |
| "top_p": 0.95, |
| "prompt": prompt, |
| "max_tokens": 500, |
| "temperature": 0.95, |
| "system_prompt": preprompt, |
| "repeat_penalty": 1.1, |
| "presence_penalty": 0, |
| "frequency_penalty": 0, |
| }, |
| ) |
| output_string = "" |
| for item in output: |
| output_string += item |
| print(output_string) |
| return output_string |
| except Exception as e: |
| s = str(e) |
| print(s) |
| if "status: 502" in s or "Prediction interrupted" in s: |
| time.sleep(10) |
| return "" |
|
|
| |
| def Codegen16b(preprompt, prompt): |
| API_URL = "https://api-inference.huggingface.co/models/gpt2" |
| HF_API_TOKEN = os.environ['HF_API_TOKEN '] |
| headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} |
| response = requests.post(API_URL, headers=headers, json=prompt) |
|
|
| return response.json() |
|
|
| |
| def Wizardcoder34b(preprompt, prompt): |
| for i in range(2): |
| try: |
| output = replicate.run( |
| "rhamnett/wizardcoder-34b-v1.0:bae902bd8a4032fcf2295523b38da90aae7cc8ca2260e7ca9b8434a981d32278", |
| input={ |
| "top_k": 250, |
| "top_p": 0.95, |
| "prompt": prompt, |
| "max_tokens": 500, |
| "temperature": 0.95, |
| "system_prompt": preprompt, |
| "repeat_penalty": 1.1, |
| "presence_penalty": 0, |
| "frequency_penalty": 0, |
| }, |
| ) |
| output_string = "" |
| for item in output: |
| output_string += item |
| print(output_string) |
| return output_string |
| except Exception as e: |
| s = str(e) |
| print(s) |
| if "status: 502" in s or "Prediction interrupted" in s: |
| time.sleep(10) |
| return "" |
| |
|
|
| |
|
|
| |
| def Wizardcoder33b(preprompt, prompt): |
| for i in range(2): |
| try: |
| output = replicate.run( |
| "lucataco/wizardcoder-33b-v1.1-gguf:bbf93cee2c2b446f0ff426ae81a9b61c5ebd8972a21f734fe035513b6fafe615", |
| input={ |
| "top_k": 250, |
| "top_p": 0.95, |
| "prompt": prompt, |
| "max_tokens": 500, |
| "temperature": 0.95, |
| "system_prompt": preprompt, |
| "repeat_penalty": 1.1, |
| "presence_penalty": 0, |
| "frequency_penalty": 0, |
| }, |
| ) |
| output_string = "" |
| for item in output: |
| output_string += item |
| print(output_string) |
| return output_string |
| except Exception as e: |
| s = str(e) |
| print(s) |
| if "status: 502" in s or "Prediction interrupted" in s: |
| time.sleep(10) |
| return "" |
|
|
| def Magicoder_S_CL_7B(preprompt, prompt): |
| while True: |
| try: |
| my_session = boto3.session.Session(profile_name=os.environ["AWS_ROLE_NAME"]) |
| session = sagemaker.Session(my_session) |
| |
| endpoint_name = os.environ["MAGICODER_SAGEMAKER_ENDPOINT"] |
| predictor = sagemaker.predictor.RealTimePredictor(endpoint_name=endpoint_name, sagemaker_session=session) |
| MAGICODER_PROMPT = """ |
| You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable responses to user instructions. |
| |
| @@ Instruction |
| {} |
| |
| @@ Response |
| """.format(preprompt + "\n" + prompt) |
| input_data = { |
| "inputs": MAGICODER_PROMPT, |
| 'parameters': {"stop": ["<|endoftext|>", "</s>"], "max_new_tokens": 1000} |
| } |
| payload = json.dumps(input_data).encode("utf-8") |
| result = predictor.predict(payload, initial_args={'ContentType': 'application/json'}) |
| json_data = json.loads(result.decode('utf-8')) |
| generated_text = json_data[0]['generated_text'].split("@@ Response")[1] |
| return generated_text |
| except Exception as e: |
| s = str(e) |
| if "Your invocation timed out" in s: |
| print("Error encoutered. Considered model unable to output correct answer: ", s) |
| return "" |
| else: |
| print("Error: ", s) |
| raise e |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| def gemini(preprompt, prompt): |
| genai.configure(api_key=os.environ['GOOGLE_API_KEY']) |
| |
| |
| |
|
|
| safety_settings = [ |
| { |
| "category": "HARM_CATEGORY_DANGEROUS", |
| "threshold": "BLOCK_NONE", |
| }, |
| { |
| "category": "HARM_CATEGORY_HARASSMENT", |
| "threshold": "BLOCK_NONE", |
| }, |
| { |
| "category": "HARM_CATEGORY_HATE_SPEECH", |
| "threshold": "BLOCK_NONE", |
| }, |
| { |
| "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", |
| "threshold": "BLOCK_NONE", |
| }, |
| { |
| "category": "HARM_CATEGORY_DANGEROUS_CONTENT", |
| "threshold": "BLOCK_NONE", |
| }, |
| ] |
|
|
| model = genai.GenerativeModel('gemini-pro') |
| |
| while True: |
| try: |
| response = model.generate_content(preprompt + "\n" + prompt, safety_settings=safety_settings) |
| break |
| except Exception as e: |
| s = str(e) |
| if "Resource has been exhausted" in s: |
| print(s) |
| print("^ Resource has been exhausted. Please wait for 100 seconds.") |
| time.sleep(100) |
| |
| |
| |
| |
| |
| |
| try: |
| res = response.candidates[0].content.parts[0].text |
| return res |
| except: |
| return "" |
|
|
| |
|
|
| |
|
|
| |