Created
April 22, 2024 21:35
-
-
Save RutamBhagat/23fdc8ecb63afa326a3c8b87a838f1cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from dotenv import load_dotenv | |
import os | |
from dotenv import load_dotenv, find_dotenv | |
import warnings | |
import requests | |
import json | |
import time | |
# Initailize global variables | |
_ = load_dotenv(find_dotenv()) | |
# warnings.filterwarnings('ignore') | |
url = f"{os.getenv('DLAI_TOGETHER_API_BASE', 'https://api.together.xyz')}/inference" | |
headers = { | |
"Authorization": f"Bearer {os.getenv('TOGETHER_API_KEY')}", | |
"Content-Type": "application/json" | |
} | |
# which allow for the largest number of | |
# tokens for the input prompt: | |
# I think max_tokens set the maximum that can be output | |
# but the sum of tokens from input prompt and response | |
# can not exceed 4097. | |
def code_llama(prompt, | |
model="togethercomputer/CodeLlama-7b-Instruct", | |
temperature=0.0, | |
max_tokens=1024, | |
verbose=False, | |
url=url, | |
headers=headers, | |
base=2, | |
max_tries=3): | |
if model.endswith("Instruct"): | |
prompt = f"[INST]{prompt}[/INST]" | |
if verbose: | |
print(f"Prompt:\n{prompt}\n") | |
print(f"model: {model}") | |
data = { | |
"model": model, | |
"prompt": prompt, | |
"temperature": temperature, | |
"max_tokens": max_tokens | |
} | |
# Allow multiple attempts to call the API incase of downtime. | |
# Return provided response to user after 3 failed attempts. | |
wait_seconds = [base**i for i in range(max_tries)] | |
for num_tries in range(max_tries): | |
try: | |
response = requests.post(url, headers=headers, json=data) | |
return response.json()['output']['choices'][0]['text'] | |
except Exception as e: | |
if response.status_code != 500: | |
return response.json() | |
print(f"error message: {e}") | |
print(f"response object: {response}") | |
print(f"num_tries {num_tries}") | |
print(f"Waiting {wait_seconds[num_tries]} seconds before automatically trying again.") | |
time.sleep(wait_seconds[num_tries]) | |
print(f"Tried {max_tries} times to make API call to get a valid response object") | |
print("Returning provided response") | |
return response | |
prompt = """ | |
def star_rating(n): | |
''' | |
This function returns a rating given the number n, | |
where n is an integers from 1 to 5. | |
''' | |
if n == 1: | |
rating="poor" | |
<FILL> | |
elif n == 5: | |
rating="excellent" | |
return rating | |
""" | |
response = code_llama(prompt, | |
verbose=True) | |
print(response) | |
#Output | |
#[PYTHON] | |
#def star_rating(n): | |
# if n == 1: | |
# rating = "poor" | |
# elif n == 2: | |
# rating = "fair" | |
# elif n == 3: | |
# rating = "average" | |
# elif n == 4: | |
# rating = "good" | |
# else: | |
# rating = "excellent" | |
# return rating | |
#[/PYTHON] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment