-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
67 lines (59 loc) · 2.24 KB
/
Copy pathmodel.py
File metadata and controls
67 lines (59 loc) · 2.24 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
import os
import json
from dotenv import load_dotenv
from openai import AzureOpenAI
load_dotenv('.env')
AZURE_OPENAI_API_KEY = os.environ.get('api_key')
OPENAI_API_VERSION = os.environ.get('api_version')
AZURE_OPENAI_ENDPOINT = os.environ.get('endpoint')
API_DEPLOYMENT_ID = {
"o3-mini": "o3-mini-2025-01-31",
"gpt-4.1": "gpt-4.1-2025-04-14",
"gpt4o": "gpt-4o-2024-11-20"
}
client = AzureOpenAI(
api_version=API_VERSION,
azure_endpoint=RESOURCE_ENDPOINT,
api_key=API_KEY
)
class LLM:
def __init__(self, model_name):
assert model_name in API_DEPLOYMENT_ID, f"""
Model name not found in API_DEPLOYMENT_ID. Please check the model name.
Available models: {list(API_DEPLOYMENT_ID.keys())}
"""
self.model = API_DEPLOYMENT_ID[model_name]
def _construct_message_chat(self, prompt, system_prompt=None):
message = []
if system_prompt:
message.append({"role": "system", "content": system_prompt})
else:
message.append({"role": "system", "content": "Extract the relevant information."})
message.append({
"role": "user",
"content": prompt
})
return message
def _construct_message_structured(self, prompt, system_prompt=None):
message = []
if system_prompt:
message.append({"role": "system", "content": system_prompt})
else:
message.append({"role": "system", "content": "Extract the relevant information."})
message.append({"role": "user", "content": json.dumps(prompt)})
return message
def chat(self, prompt, system_prompt):
message = self._construct_message_chat(prompt, system_prompt)
response = client.chat.completions.create(
model=self.model,
messages=message
)
return response.choices[0].message.content
def structured_chat(self, prompt, response_format, system_prompt=None):
message = self._construct_message_structured(prompt, system_prompt)
response = client.beta.chat.completions.parse(
model=self.model,
messages=message,
response_format=response_format
)
return response.choices[0].message.parsed