4
4
5
5
from aisuite .provider import Provider
6
6
from aisuite .framework import ChatCompletionResponse
7
+ from aisuite .framework .message import Message , ChatCompletionMessageToolCall , Function
7
8
8
9
9
10
class AzureProvider (Provider ):
@@ -18,14 +19,35 @@ def __init__(self, **config):
18
19
)
19
20
20
21
def chat_completions_create (self , model , messages , ** kwargs ):
21
- url = f"https://{ model } .westus3.models.ai.azure.com/v1/chat/completions"
22
- url = f"https://{ self .base_url } /chat/completions"
23
- if self .base_url :
24
- url = f"{ self .base_url } /chat/completions"
22
+ url = f"{ self .base_url } /chat/completions"
25
23
26
24
# Remove 'stream' from kwargs if present
27
25
kwargs .pop ("stream" , None )
28
- data = {"messages" : messages , ** kwargs }
26
+
27
+ # Transform messages if they are Message objects
28
+ transformed_messages = []
29
+ for message in messages :
30
+ if isinstance (message , Message ):
31
+ transformed_messages .append (message .model_dump (mode = "json" ))
32
+ else :
33
+ transformed_messages .append (message )
34
+
35
+ # Prepare the request payload with transformed messages
36
+ data = {"messages" : transformed_messages }
37
+
38
+ # Add tools if provided
39
+ if "tools" in kwargs :
40
+ data ["tools" ] = kwargs ["tools" ]
41
+ # Remove from kwargs to avoid duplication
42
+ kwargs .pop ("tools" )
43
+
44
+ # Add tool_choice if provided
45
+ if "tool_choice" in kwargs :
46
+ data ["tool_choice" ] = kwargs ["tool_choice" ]
47
+ kwargs .pop ("tool_choice" )
48
+
49
+ # Add remaining kwargs
50
+ data .update (kwargs )
29
51
30
52
body = json .dumps (data ).encode ("utf-8" )
31
53
headers = {"Content-Type" : "application/json" , "Authorization" : self .api_key }
@@ -36,10 +58,32 @@ def chat_completions_create(self, model, messages, **kwargs):
36
58
result = response .read ()
37
59
resp_json = json .loads (result )
38
60
completion_response = ChatCompletionResponse ()
39
- # TODO: Add checks for fields being present in resp_json.
40
- completion_response .choices [0 ].message .content = resp_json ["choices" ][
41
- 0
42
- ]["message" ]["content" ]
61
+
62
+ # Process the response
63
+ choice = resp_json ["choices" ][0 ]
64
+ message = choice ["message" ]
65
+
66
+ # Set basic message content
67
+ completion_response .choices [0 ].message .content = message .get ("content" )
68
+ completion_response .choices [0 ].message .role = message .get (
69
+ "role" , "assistant"
70
+ )
71
+
72
+ # Handle tool calls if present
73
+ if "tool_calls" in message and message ["tool_calls" ] is not None :
74
+ tool_calls = []
75
+ for tool_call in message ["tool_calls" ]:
76
+ new_tool_call = ChatCompletionMessageToolCall (
77
+ id = tool_call ["id" ],
78
+ type = tool_call ["type" ],
79
+ function = {
80
+ "name" : tool_call ["function" ]["name" ],
81
+ "arguments" : tool_call ["function" ]["arguments" ],
82
+ },
83
+ )
84
+ tool_calls .append (new_tool_call )
85
+ completion_response .choices [0 ].message .tool_calls = tool_calls
86
+
43
87
return completion_response
44
88
45
89
except urllib .error .HTTPError as error :
0 commit comments