|
| 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.component.langchain4j.agent.integration; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import dev.langchain4j.model.chat.ChatModel; |
| 22 | +import dev.langchain4j.model.chat.request.ResponseFormat; |
| 23 | +import dev.langchain4j.model.chat.request.json.JsonRawSchema; |
| 24 | +import dev.langchain4j.model.chat.request.json.JsonSchema; |
| 25 | +import org.apache.camel.builder.RouteBuilder; |
| 26 | +import org.apache.camel.component.langchain4j.agent.api.Agent; |
| 27 | +import org.apache.camel.component.langchain4j.agent.api.AgentConfiguration; |
| 28 | +import org.apache.camel.component.langchain4j.agent.api.AgentWithoutMemory; |
| 29 | +import org.apache.camel.component.mock.MockEndpoint; |
| 30 | +import org.apache.camel.test.infra.ollama.services.OllamaService; |
| 31 | +import org.apache.camel.test.infra.ollama.services.OllamaServiceFactory; |
| 32 | +import org.apache.camel.test.junit6.CamelTestSupport; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.condition.DisabledIfSystemProperty; |
| 35 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 36 | + |
| 37 | +import static dev.langchain4j.model.chat.request.ResponseFormatType.JSON; |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 40 | + |
| 41 | +/** |
| 42 | + * Integration test for structured outputs with JSON Schema using the langchain4j-agent component. |
| 43 | + */ |
| 44 | +@DisabledIfSystemProperty(named = "ci.env.name", matches = ".*", disabledReason = "Requires too much network resources") |
| 45 | +public class LangChain4jAgentStructuredOutputIT extends CamelTestSupport { |
| 46 | + |
| 47 | + private static final String PERSON_SCHEMA = """ |
| 48 | + { |
| 49 | + "type": "object", |
| 50 | + "properties": { |
| 51 | + "name": { |
| 52 | + "type": "string", |
| 53 | + "description": "The person's full name" |
| 54 | + }, |
| 55 | + "age": { |
| 56 | + "type": "integer", |
| 57 | + "description": "The person's age in years" |
| 58 | + }, |
| 59 | + "occupation": { |
| 60 | + "type": "string", |
| 61 | + "description": "The person's job or profession" |
| 62 | + } |
| 63 | + }, |
| 64 | + "required": ["name", "age", "occupation"], |
| 65 | + "additionalProperties": false |
| 66 | + } |
| 67 | + """; |
| 68 | + |
| 69 | + private static final String TEST_PROMPT |
| 70 | + = "Generate information about a software engineer named Alice who is 30 years old."; |
| 71 | + |
| 72 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 73 | + |
| 74 | + protected ChatModel chatModel; |
| 75 | + |
| 76 | + @RegisterExtension |
| 77 | + static OllamaService OLLAMA = OllamaServiceFactory.createSingletonService(); |
| 78 | + |
| 79 | + @Override |
| 80 | + protected void setupResources() throws Exception { |
| 81 | + super.setupResources(); |
| 82 | + |
| 83 | + JsonRawSchema jsonRawSchema = JsonRawSchema.from(PERSON_SCHEMA); |
| 84 | + ResponseFormat responseFormat = ResponseFormat.builder() |
| 85 | + .type(JSON) |
| 86 | + .jsonSchema(JsonSchema.builder() |
| 87 | + .name("person_schema") |
| 88 | + .rootElement(jsonRawSchema) |
| 89 | + .build()) |
| 90 | + .build(); |
| 91 | + |
| 92 | + chatModel = ModelHelper.loadChatModel(OLLAMA, responseFormat); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testStructuredOutputWithJsonSchema() throws Exception { |
| 97 | + MockEndpoint mockEndpoint = getMockEndpoint("mock:result"); |
| 98 | + mockEndpoint.expectedMessageCount(1); |
| 99 | + |
| 100 | + String response = template.requestBody("direct:structured-output", TEST_PROMPT, String.class); |
| 101 | + |
| 102 | + mockEndpoint.assertIsSatisfied(); |
| 103 | + |
| 104 | + assertNotNull(response); |
| 105 | + JsonNode jsonResponse = objectMapper.readTree(response); |
| 106 | + |
| 107 | + assertThat(jsonResponse.has("name")).isTrue(); |
| 108 | + assertThat(jsonResponse.has("age")).isTrue(); |
| 109 | + assertThat(jsonResponse.has("occupation")).isTrue(); |
| 110 | + |
| 111 | + assertThat(jsonResponse.get("name").asText()).contains("Alice"); |
| 112 | + assertThat(jsonResponse.get("age").asInt()).isEqualTo(30); |
| 113 | + assertThat(jsonResponse.get("occupation").asText().toLowerCase()).containsAnyOf("engineer", "developer"); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected RouteBuilder createRouteBuilder() { |
| 118 | + AgentConfiguration configuration = new AgentConfiguration() |
| 119 | + .withChatModel(chatModel); |
| 120 | + |
| 121 | + Agent agent = new AgentWithoutMemory(configuration); |
| 122 | + context.getRegistry().bind("structuredOutputAgent", agent); |
| 123 | + |
| 124 | + return new RouteBuilder() { |
| 125 | + @Override |
| 126 | + public void configure() { |
| 127 | + from("direct:structured-output") |
| 128 | + .to("langchain4j-agent:structured-output?agent=#structuredOutputAgent") |
| 129 | + .to("json-validator:classpath:person-schema.json") |
| 130 | + .to("mock:result"); |
| 131 | + } |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments