-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazureai.py
65 lines (48 loc) · 1.8 KB
/
azureai.py
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
"""
Azure AI support for golem
"""
# pylint: disable=broad-exception-caught, too-many-arguments, too-many-positional-arguments, too-many-locals
from util import http_request, fatal, lookup_variable
def ask_azureai(
url, api_key, messages, temperature, seed, top_p, max_tokens, logprobs, top_logprobs
):
"""
Make a request to the Azure AI Inference API.
"""
# Use the Azure AI Model Inference API. For
# Meta-Llama-3-70B-Instruct and other Marketplace models N.B. This
# is different to the Azure OpenAI API.
# https://learn.microsoft.com/en-us/azure/ai-studio/reference/reference-model-inference-api
if url is None:
url = lookup_variable("AZUREAI_ENDPOINT_URL")
if api_key is None:
api_key = lookup_variable("AZUREAI_ENDPOINT_KEY")
url = f"{url}chat/completions"
headers = {
"Authorization": api_key,
"Content-Type": "application/json",
}
json_data = {"messages": messages}
if temperature is not None:
json_data["temperature"] = temperature
if seed is not None:
json_data["seed"] = seed
if top_p is not None:
json_data["top_p"] = top_p
if max_tokens is not None:
json_data["max_tokens"] = max_tokens
if logprobs is not None:
json_data["logprobs"] = bool(logprobs)
if top_logprobs is not None:
json_data["top_logprobs"] = top_logprobs
request = None
response = None
try:
request, response = http_request(url, headers, json_data)
response = response.json()
answer = response["choices"][0]["message"]["content"]
provider = "azureai"
model = response["model"]
except Exception as e:
fatal(f"EXCEPTION: {e} REQUEST: {request} RESPONSE: {response}")
return request, response, answer, provider, model