generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Orchestration] OSS Generator Improvements (#158)
* Simplifications and cleanup * Remove parsing for streaming
- Loading branch information
Showing
29 changed files
with
262 additions
and
2,616 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 16 additions & 53 deletions
69
orchestration/src/main/java/com/sap/ai/sdk/orchestration/LLMModuleResultDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,45 @@ | ||
package com.sap.ai.sdk.orchestration; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.sap.ai.sdk.orchestration.client.model.LLMModuleResult; | ||
import com.sap.ai.sdk.orchestration.client.model.LLMModuleResultStreaming; | ||
import com.sap.ai.sdk.orchestration.client.model.LLMModuleResultSynchronous; | ||
import java.io.IOException; | ||
import java.io.Serial; | ||
import javax.annotation.Nonnull; | ||
import lombok.val; | ||
|
||
/** | ||
* A deserializer for {@link LLMModuleResult} that determines the concrete implementation based on | ||
* the structure of the JSON object. | ||
*/ | ||
public class LLMModuleResultDeserializer extends StdDeserializer<LLMModuleResult> { | ||
class LLMModuleResultDeserializer extends StdDeserializer<LLMModuleResult> { | ||
// checkstyle requires a serialVersionUid since StdDeserializer implements Serializable | ||
@Serial private static final long serialVersionUID = 1L; | ||
|
||
public LLMModuleResultDeserializer() { | ||
/** Default constructor. */ | ||
LLMModuleResultDeserializer() { | ||
super(LLMModuleResult.class); | ||
} | ||
|
||
/** | ||
* Deserialize the JSON object into one of the subtypes of the base type. | ||
* | ||
* <ul> | ||
* <li>If elements of "choices" array contains "delta", deserialize into {@link | ||
* LLMModuleResultStreaming}. | ||
* <li>Otherwise, deserialize into {@link LLMModuleResultSynchronous}. | ||
* </ul> | ||
* Always deserialize into {@link LLMModuleResultSynchronous} since streaming isn't supported yet. | ||
* | ||
* @param parser The JSON parser. | ||
* @param context The deserialization context. | ||
* @return The deserialized object. | ||
* @throws IOException If an I/O error occurs. | ||
*/ | ||
@Nonnull | ||
@Override | ||
public LLMModuleResult deserialize(JsonParser parser, @Nonnull DeserializationContext context) | ||
public LLMModuleResult deserialize( | ||
@Nonnull final JsonParser parser, @Nonnull final DeserializationContext context) | ||
throws IOException { | ||
val mapper = (ObjectMapper) parser.getCodec(); | ||
val rootNode = mapper.readTree(parser); | ||
|
||
// Check if the target type is a concrete class | ||
JavaType targetType = context.getContextualType(); | ||
if (targetType != null && !LLMModuleResult.class.equals(targetType.getRawClass())) { | ||
return delegateToDefaultDeserializer(parser, context, targetType); | ||
} | ||
|
||
// Custom deserialization logic for LLMModuleResult interface | ||
var mapper = (ObjectMapper) parser.getCodec(); | ||
var rootNode = mapper.readTree(parser); | ||
Class<? extends LLMModuleResult> concreteClass = LLMModuleResultSynchronous.class; | ||
|
||
// Inspect the "choices" field | ||
var choicesNode = rootNode.get("choices"); | ||
if (choicesNode != null && choicesNode.isArray()) { | ||
var firstChoice = (JsonNode) choicesNode.get(0); | ||
if (firstChoice != null && firstChoice.has("delta")) { | ||
concreteClass = LLMModuleResultStreaming.class; | ||
} | ||
} | ||
|
||
// Create a new parser for the root node | ||
var rootParser = rootNode.traverse(mapper); | ||
rootParser.nextToken(); // Advance to the first token | ||
|
||
// Use the default deserializer for the concrete class | ||
return delegateToDefaultDeserializer(rootParser, context, mapper.constructType(concreteClass)); | ||
} | ||
|
||
/** | ||
* Delegate deserialization to the default deserializer for the given concrete type. | ||
* | ||
* @param parser The JSON parser. | ||
* @param context The deserialization context. | ||
* @param concreteType The concrete type to deserialize into. | ||
* @return The deserialized object. | ||
* @throws IOException If an I/O error occurs. | ||
*/ | ||
private LLMModuleResult delegateToDefaultDeserializer( | ||
JsonParser parser, DeserializationContext context, JavaType concreteType) throws IOException { | ||
var defaultDeserializer = context.findRootValueDeserializer(concreteType); | ||
return (LLMModuleResult) defaultDeserializer.deserialize(parser, context); | ||
return mapper.readValue(rootNode.toString(), LLMModuleResultSynchronous.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.