-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_inference_script.py
More file actions
169 lines (147 loc) · 6.02 KB
/
Copy pathllm_inference_script.py
File metadata and controls
169 lines (147 loc) · 6.02 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import requests
import json
def llm_inference(provider,prompt,key):
"""
Sends a prompt to a specified inference API provider and returns the generated text.
currently supported providers: "openrouter", "together", "anyscale", "deepinfra"
Args:
provider (str): The name of the provider to use for the inference. Must be one of the following:
"openrouter", "together", "anyscale", or "deepinfra".
prompt (str): The text prompt to send to the language model.
key (str): The API key required for the specified provider.
Returns:
str: The generated text from the language model.
Raises:
Exception: If the request to the provider fails with a non-200 status code.
Exception: If an invalid provider name is provided.
"""
match provider:
case "openrouter":
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {key}",
"Content-Type": "application/json"
},
data=json.dumps({
"model": "mistralai/mixtral-8x7b-instruct",
"prompt":f"[INST] {prompt} [/INST]",
"max_tokens":4096,
"temperature":0.7,
})
)
if response.status_code == 200:
content = response.json()
return content["choices"][0]["text"]
else:
raise Exception(f"Request failed with status code {response.status_code}")
case "together":
response = requests.post(
url='https://api.together.xyz/inference',
headers={
"Authorization": f"Bearer {key}",
"Content-Type": "application/json"
},
json={
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"prompt": f"[INST] {prompt} [/INST]",
"max_tokens": 4096,
"temperature": 0.7,
},)
if response.status_code == 200:
content = response.json()
return content["output"]["choices"][0]["text"]
else:
raise Exception(f"Request failed with status code {response.status_code}")
case "anyscale":
response = requests.post(
url="https://api.endpoints.anyscale.com/v1/completions",
headers={
"Authorization": f"Bearer {key}",
"Content-Type": "application/json"
},
data=json.dumps({
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"prompt":f"<s>[INST] {prompt} [/INST]",
"max_tokens":4096,
"temperature":0.7,
})
)
if response.status_code == 200:
content = response.json()
return content["choices"][0]["text"]
else:
raise Exception(f"Request failed with status code {response.status_code}")
case "deepinfra":
response = requests.post(
url="https://api.deepinfra.com/v1/inference/mistralai/Mixtral-8x7B-Instruct-v0.1",
headers={
"Authorization": f"Bearer {key}",
"Content-Type": "application/json"
},
data=json.dumps({
"input":f"[INST] {prompt} [/INST]",
"max_new_tokens": 4096,
"temperature": 0.7,
})
)
if response.status_code == 200:
content = response.json()
return content["results"][0]["generated_text"]
else:
raise Exception(f"Request failed with status code {response.status_code}")
case "abacusai":
response = requests.post(
url="https://llmapis.abacus.ai/api/generateCompletions",
headers={
"Authorization": f"Bearer {key}",
"Content-Type": "application/json"
},
data=json.dumps({
"model": "mixtral-8x7b",
"messages": [
{
"role": "system",
"content": "You are a helpful ai assistant"
}, {
"role": "user",
"content": f"{prompt}"
}
],
"max_tokens":4096,
"temperature":0.7,
})
)
if response.status_code == 200:
content = response.json()
return content["choices"][0]["message"]["content"]
else:
raise Exception(f"Request failed with status code {response.status_code}")
case _ :
raise Exception("Invalid provider")
if __name__ == "__main__":
from dotenv import load_dotenv
import os
import time
load_dotenv()
prompt = (
"SYSTEM : You are better than Marilyn Vos Savant at solving brain teaser logic puzzles step by step. "
"USER : Sally (a girl) has 5 brothers. Each brother has 3 sisters. How many sisters does Sally have? "
"Give Reasoning first and then in the end The response must be of json format {'sisters':<count of sisters>}"
)
providers = ["openrouter", "anyscale", "together", "deepinfra", "abacusai"]
api_keys = {
"openrouter": os.getenv("OPENROUTER_API_KEY"),
"anyscale": os.getenv("ANYSCALE_API_KEY"),
"together": os.getenv("TOGETHER_API_KEY"),
"deepinfra": os.getenv("DEEPINFRA_API_KEY"),
"abacusai":os.getenv("ABACUSAI_API_KEY"),
}
for provider in providers:
print(f"====== {provider.upper()} TEST ======")
start_time = time.time()
output = llm_inference(provider, prompt, api_keys[provider])
print(output)
stop_time = time.time()
print(f"processing time: {stop_time - start_time}")
print("===========================")