@@ -48,7 +48,7 @@ void AzureOpenAI::Initialize(const nlohmann::json& params)
4848 _params = ParamsDefinition.Parse (params);
4949}
5050
51- RequestData AzureOpenAI::FormatRequest (const Schema::IServer::LinearHistory& history, bool stream) const
51+ RequestData AzureOpenAI::FormatRequest (const Schema::IServer::LinearHistory& history, bool stream, const std::optional<std::vector<Schema::IServer::Tool>>& /* tools */ ) const
5252{
5353 RequestData data{};
5454 data.url = _params.url ;
@@ -67,55 +67,64 @@ RequestData AzureOpenAI::FormatRequest(const Schema::IServer::LinearHistory& his
6767 }
6868 for (const auto & message : history)
6969 {
70- auto messageJson = nlohmann::json::object ();
71- messageJson[" content" ] = nlohmann::json::array ();
72- if (message.get_role () == Schema::IServer::MessageRole::DEVELOPER)
70+ if (std::holds_alternative<Schema::IServer::ChatMessage>(message))
7371 {
74- messageJson[" role" ] = " system" ;
75- }
76- else if (message.get_role () == Schema::IServer::MessageRole::USER)
77- {
78- messageJson[" role" ] = " user" ;
79- }
80- else if (message.get_role () == Schema::IServer::MessageRole::ASSISTANT)
81- {
82- messageJson[" role" ] = " assistant" ;
83- }
84- else
85- {
86- continue ;
87- }
88- for (const auto & content : message.get_content ())
89- {
90- if (content.get_type () == Schema::IServer::Type::TEXT || content.get_type () == Schema::IServer::Type::REFUSAL)
72+ const auto & chatMessage = std::get<Schema::IServer::ChatMessage>(message);
73+ auto messageJson = nlohmann::json::object ();
74+ messageJson[" content" ] = nlohmann::json::array ();
75+ if (chatMessage.get_role () == Schema::IServer::ChatMessageRole::DEVELOPER)
76+ {
77+ messageJson[" role" ] = " system" ;
78+ }
79+ else if (chatMessage.get_role () == Schema::IServer::ChatMessageRole::USER)
9180 {
92- /* * Azure open ai does not seem to have a special REFUSAL message type */
93- messageJson[" content" ].push_back ({
94- {" type" , " text" },
95- {" text" , content.get_data ()}
96- });
81+ messageJson[" role" ] = " user" ;
9782 }
98- else if (content. get_type () == Schema::IServer::Type::IMAGE_URL )
83+ else if (chatMessage. get_role () == Schema::IServer::ChatMessageRole::ASSISTANT )
9984 {
100- messageJson[" content" ].push_back ({
101- {" type" , " image_url" },
102- {" image_url" , {
103- {" url" , content.get_data ()}
104- }}
105- });
85+ messageJson[" role" ] = " assistant" ;
10686 }
10787 else
10888 {
10989 continue ;
11090 }
91+ for (const auto & content : chatMessage.get_content ())
92+ {
93+ if (content.get_type () == Schema::IServer::MessageContentType::TEXT || content.get_type () == Schema::IServer::MessageContentType::REFUSAL)
94+ {
95+ /* * Azure open ai does not seem to have a special REFUSAL message type */
96+ messageJson[" content" ].push_back ({
97+ {" type" , " text" },
98+ {" text" , content.get_data ()}
99+ });
100+ }
101+ else if (content.get_type () == Schema::IServer::MessageContentType::IMAGE_URL)
102+ {
103+ messageJson[" content" ].push_back ({
104+ {" type" , " image_url" },
105+ {" image_url" , {
106+ {" url" , content.get_data ()}
107+ }}
108+ });
109+ }
110+ else
111+ {
112+ continue ;
113+ }
114+ }
115+ body[" messages" ].push_back (messageJson);
116+ }
117+ else if (std::holds_alternative<Schema::IServer::FunctionCallMessage>(message)
118+ || std::holds_alternative<Schema::IServer::FunctionCallOutputMessage>(message))
119+ {
120+ throw std::runtime_error (" AzureOpenAI does not support function call messages" );
111121 }
112- body[" messages" ].push_back (messageJson);
113122 }
114123 data.body = body.dump ();
115124 return data;
116125}
117126
118- Schema::IServer::MessageContent AzureOpenAI::ParseResponse (const std::string& responseString) const
127+ Schema::IServer::LinearHistory AzureOpenAI::ParseResponse (const std::string& responseString) const
119128{
120129 Schema::AzureOpenAI::BulkResponse response;
121130 try
@@ -131,14 +140,17 @@ Schema::IServer::MessageContent AzureOpenAI::ParseResponse(const std::string& re
131140 throw std::runtime_error (" No choices in response" );
132141 }
133142 const auto & choice = response.get_choices ().front ();
134- const auto & message = choice.get_message ();
143+ const auto & msg = choice.get_message ();
135144 Schema::IServer::MessageContent content;
136- content.set_type (message.get_refusal () ? Schema::IServer::Type::REFUSAL : Schema::IServer::Type::TEXT);
137- content.set_data (message.get_content ());
138- return content;
145+ content.set_type (msg.get_refusal () ? Schema::IServer::MessageContentType::REFUSAL : Schema::IServer::MessageContentType::TEXT);
146+ content.set_data (msg.get_content ());
147+ Schema::IServer::ChatMessage chatMessage;
148+ chatMessage.set_role (Schema::IServer::ChatMessageRole::ASSISTANT);
149+ chatMessage.set_content ({content});
150+ return Schema::IServer::LinearHistory{std::move (chatMessage)};
139151}
140152
141- std::optional<Schema::IServer::MessageContent > AzureOpenAI::ParseStreamResponse (const StreamResponse::Event& event) const
153+ std::optional<Schema::IServer::ChatCompletionSegment > AzureOpenAI::ParseStreamResponse (const StreamResponse::Event& event) const
142154{
143155 if (!event.value .has_value ())
144156 {
@@ -172,8 +184,5 @@ std::optional<Schema::IServer::MessageContent> AzureOpenAI::ParseStreamResponse(
172184 {
173185 return std::nullopt ;
174186 }
175- Schema::IServer::MessageContent content{};
176- content.set_type (Schema::IServer::Type::TEXT);
177- content.set_data (delta.get_content ().value ());
178- return content;
187+ return delta.get_content ().value ();
179188}
0 commit comments