|
15 | 15 | */ |
16 | 16 | package io.agentscope.extensions.model.ollama; |
17 | 17 |
|
| 18 | +import com.fasterxml.jackson.core.type.TypeReference; |
18 | 19 | import io.agentscope.core.formatter.Formatter; |
| 20 | +import io.agentscope.core.formatter.JsonSchema; |
| 21 | +import io.agentscope.core.formatter.ResponseFormat; |
19 | 22 | import io.agentscope.core.message.Msg; |
20 | 23 | import io.agentscope.core.model.ChatModelBase; |
21 | 24 | import io.agentscope.core.model.ChatResponse; |
|
29 | 32 | import io.agentscope.core.model.transport.HttpTransportFactory; |
30 | 33 | import io.agentscope.core.model.transport.OkHttpTransport; |
31 | 34 | import io.agentscope.core.model.transport.ProxyConfig; |
| 35 | +import io.agentscope.core.util.JsonException; |
| 36 | +import io.agentscope.core.util.JsonUtils; |
32 | 37 | import io.agentscope.extensions.model.ollama.dto.OllamaMessage; |
33 | 38 | import io.agentscope.extensions.model.ollama.dto.OllamaRequest; |
34 | 39 | import io.agentscope.extensions.model.ollama.dto.OllamaResponse; |
|
37 | 42 | import io.agentscope.extensions.model.ollama.options.OllamaOptions; |
38 | 43 | import java.time.Instant; |
39 | 44 | import java.util.List; |
| 45 | +import java.util.Map; |
40 | 46 | import org.slf4j.Logger; |
41 | 47 | import org.slf4j.LoggerFactory; |
42 | 48 | import reactor.core.publisher.Flux; |
@@ -135,6 +141,70 @@ public String getModelName() { |
135 | 141 | return this.modelName; |
136 | 142 | } |
137 | 143 |
|
| 144 | + @Override |
| 145 | + public GenerateOptions getDefaultOptions() { |
| 146 | + return withSchemaProjection(defaultOptions.toGenerateOptions(), defaultOptions.getFormat()); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public GenerateOptions resolveOptions(GenerateOptions requestOptions) { |
| 151 | + OllamaOptions effective = |
| 152 | + requestOptions == null ? defaultOptions : defaultOptions.merge(requestOptions); |
| 153 | + GenerateOptions converted = |
| 154 | + withSchemaProjection(effective.toGenerateOptions(), effective.getFormat()); |
| 155 | + // The raw format loses the schema wrapper (and its name), so an explicit request |
| 156 | + // format is merged back over the projection. |
| 157 | + return requestOptions != null && requestOptions.getResponseFormat() != null |
| 158 | + ? GenerateOptions.mergeOptions(requestOptions, converted) |
| 159 | + : converted; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Mirrors the send path's effective {@code format} semantics: the request only clears the |
| 164 | + * default schema when it maps to a non-null format (json_schema, json_object, or a raw |
| 165 | + * {@code format} body parameter), exactly as {@code fromGenerateOptions} and |
| 166 | + * {@code additionalBodyParams} behave before the default merge. The converted options keep |
| 167 | + * every mapped field; an object-shaped format is additionally projected as the response |
| 168 | + * format the budget prices. |
| 169 | + */ |
| 170 | + private static GenerateOptions withSchemaProjection(GenerateOptions converted, Object format) { |
| 171 | + ResponseFormat schemaFormat = objectSchemaFormat(format); |
| 172 | + if (schemaFormat == null) { |
| 173 | + return converted; |
| 174 | + } |
| 175 | + return GenerateOptions.mergeOptions( |
| 176 | + GenerateOptions.builder().responseFormat(schemaFormat).build(), converted); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Recognizes a raw {@code format} whose JSON representation is an object, i.e. the schema the |
| 181 | + * provider actually sends. Values like {@code "json"}, arrays or scalars carry no schema and |
| 182 | + * are left to the send path untouched. |
| 183 | + */ |
| 184 | + private static ResponseFormat objectSchemaFormat(Object format) { |
| 185 | + Map<String, Object> schema = toSchemaMap(format); |
| 186 | + return schema == null |
| 187 | + ? null |
| 188 | + : ResponseFormat.jsonSchema(JsonSchema.builder().schema(schema).build()); |
| 189 | + } |
| 190 | + |
| 191 | + private static Map<String, Object> toSchemaMap(Object format) { |
| 192 | + if (format instanceof Map<?, ?> schemaMap) { |
| 193 | + @SuppressWarnings("unchecked") |
| 194 | + Map<String, Object> schema = (Map<String, Object>) schemaMap; |
| 195 | + return schema; |
| 196 | + } |
| 197 | + if (format == null) { |
| 198 | + return null; |
| 199 | + } |
| 200 | + try { |
| 201 | + return JsonUtils.getJsonCodec() |
| 202 | + .convertValue(format, new TypeReference<Map<String, Object>>() {}); |
| 203 | + } catch (JsonException e) { |
| 204 | + return null; |
| 205 | + } |
| 206 | + } |
| 207 | + |
138 | 208 | @Override |
139 | 209 | public boolean supportsToolChoiceSpecific() { |
140 | 210 | return true; |
|
0 commit comments