-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
120 lines (100 loc) · 3.77 KB
/
Copy pathutils.py
File metadata and controls
120 lines (100 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
from dotenv import load_dotenv
import os
from dotenv import load_dotenv, find_dotenv
import warnings
import requests
import json
import time
import config
# 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 {config.OPEN_MODELS_KEY}",
"Content-Type": "application/json"
}
def llama(prompt,
add_inst=True,
model="togethercomputer/llama-2-7b-chat",
temperature=0.0,
max_tokens=1024,
verbose=False,
url=url,
headers=headers,
base=2, # number of seconds to wait
max_tries=3):
if add_inst:
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
def llama_chat(prompts,
responses,
model="togethercomputer/llama-2-7b-chat",
temperature=0.0,
max_tokens=1024,
verbose=False,
url=url,
headers=headers,
base=2,
max_tries=3
):
prompt = get_prompt_chat(prompts,responses)
# 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 = llama(prompt=prompt,
add_inst=False,
model=model,
temperature=temperature,
max_tokens=max_tokens,
verbose=verbose,
url=url,
headers=headers
)
return response
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
def get_prompt_chat(prompts, responses):
prompt_chat = f"<s>[INST] {prompts[0]} [/INST]"
for n, response in enumerate(responses):
prompt = prompts[n + 1]
prompt_chat += f"\n{response}\n </s><s>[INST] \n{ prompt }\n [/INST]"
return prompt_chat