|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.quarkus.component.openai.deployment; |
| 18 | + |
| 19 | +import java.util.Set; |
| 20 | +import java.util.stream.Collectors; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import com.openai.core.JsonField; |
| 24 | +import com.openai.core.JsonValue; |
| 25 | +import com.openai.models.chat.completions.ChatCompletion; |
| 26 | +import com.openai.models.chat.completions.ChatCompletionChunk; |
| 27 | +import io.quarkus.deployment.Capabilities; |
| 28 | +import io.quarkus.deployment.Capability; |
| 29 | +import io.quarkus.deployment.annotations.BuildProducer; |
| 30 | +import io.quarkus.deployment.annotations.BuildStep; |
| 31 | +import io.quarkus.deployment.builditem.CombinedIndexBuildItem; |
| 32 | +import io.quarkus.deployment.builditem.FeatureBuildItem; |
| 33 | +import io.quarkus.deployment.builditem.IndexDependencyBuildItem; |
| 34 | +import io.quarkus.deployment.builditem.RemovedResourceBuildItem; |
| 35 | +import io.quarkus.deployment.builditem.nativeimage.NativeImageResourcePatternsBuildItem; |
| 36 | +import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; |
| 37 | +import io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyBuildItem; |
| 38 | +import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild; |
| 39 | +import io.quarkus.jackson.deployment.IgnoreJsonDeserializeClassBuildItem; |
| 40 | +import io.quarkus.maven.dependency.ArtifactKey; |
| 41 | +import org.jboss.jandex.ClassInfo; |
| 42 | +import org.jboss.jandex.DotName; |
| 43 | +import org.jboss.jandex.Type; |
| 44 | + |
| 45 | +class OpenaiProcessor { |
| 46 | + private static final String FEATURE = "camel-openai"; |
| 47 | + |
| 48 | + @BuildStep |
| 49 | + FeatureBuildItem feature() { |
| 50 | + return new FeatureBuildItem(FEATURE); |
| 51 | + } |
| 52 | + |
| 53 | + @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) |
| 54 | + IndexDependencyBuildItem indexDependencies() { |
| 55 | + return new IndexDependencyBuildItem("com.openai", "openai-java-core"); |
| 56 | + } |
| 57 | + |
| 58 | + @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) |
| 59 | + void registerForReflection( |
| 60 | + Capabilities capabilities, |
| 61 | + CombinedIndexBuildItem combinedIndex, |
| 62 | + BuildProducer<IgnoreJsonDeserializeClassBuildItem> ignoredJsonDeserializeClass, |
| 63 | + BuildProducer<ReflectiveClassBuildItem> reflectiveClass, |
| 64 | + BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy, |
| 65 | + BuildProducer<NativeImageResourcePatternsBuildItem> nativeResourcePatterns) { |
| 66 | + |
| 67 | + reflectiveHierarchy.produce(ReflectiveHierarchyBuildItem |
| 68 | + .builder(Type.create(ChatCompletion.class)).ignoreNested(false) |
| 69 | + .build()); |
| 70 | + |
| 71 | + reflectiveHierarchy.produce(ReflectiveHierarchyBuildItem |
| 72 | + .builder(Type.create(ChatCompletionChunk.class)).ignoreNested(false) |
| 73 | + .build()); |
| 74 | + |
| 75 | + // Make quarkus-kotlin optional since not everything it provides is required |
| 76 | + if (capabilities.isMissing(Capability.KOTLIN)) { |
| 77 | + Stream.of(JsonField.class.getName(), JsonValue.class.getName()) |
| 78 | + .map(DotName::createSimple) |
| 79 | + .forEach(className -> { |
| 80 | + // Suppress quarkus-jackson adding its own reflective config for JsonDeserialize so we can add our own |
| 81 | + ignoredJsonDeserializeClass.produce(new IgnoreJsonDeserializeClassBuildItem(className)); |
| 82 | + reflectiveHierarchy.produce(ReflectiveHierarchyBuildItem |
| 83 | + .builder(Type.create(className, Type.Kind.CLASS)).ignoreNested(false) |
| 84 | + .build()); |
| 85 | + }); |
| 86 | + |
| 87 | + Set<String> openAIModelClassNames = combinedIndex.getIndex() |
| 88 | + .getKnownClasses() |
| 89 | + .stream() |
| 90 | + .map(ClassInfo::name) |
| 91 | + .map(DotName::toString) |
| 92 | + .filter(className -> className.startsWith("com.openai.models")) |
| 93 | + .collect(Collectors.toSet()); |
| 94 | + |
| 95 | + reflectiveClass.produce(ReflectiveClassBuildItem.builder(openAIModelClassNames.toArray(new String[0])) |
| 96 | + .fields() |
| 97 | + .methods() |
| 98 | + .build()); |
| 99 | + |
| 100 | + nativeResourcePatterns.produce(NativeImageResourcePatternsBuildItem.builder() |
| 101 | + .includeGlobs("META-INF/**/*.kotlin_module", |
| 102 | + "META-INF/services/kotlin.reflect.*", |
| 103 | + "**/*.kotlin_builtins") |
| 104 | + .build()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) |
| 109 | + RemovedResourceBuildItem excludeNativeImageDirectives() { |
| 110 | + // Remove all native-image directives from openai-java-core as it's mostly redundant & inaccurate for Quarkus |
| 111 | + return new RemovedResourceBuildItem( |
| 112 | + ArtifactKey.fromString("com.openai:openai-java-core"), |
| 113 | + Set.of( |
| 114 | + "META-INF/native-image/jni-config.json", |
| 115 | + "META-INF/native-image/predefined-classes-config.json", |
| 116 | + "META-INF/native-image/proxy-config.json", |
| 117 | + "META-INF/native-image/reflect-config.json", |
| 118 | + "META-INF/native-image/resource-config.json", |
| 119 | + "META-INF/native-image/serialization-config.json")); |
| 120 | + } |
| 121 | +} |
0 commit comments