orpiske commented on code in PR #14928:
URL: https://github.com/apache/camel/pull/14928#discussion_r1689866714


##########
components/camel-ai/camel-langchain4j-web-search/pom.xml:
##########
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>camel-ai-parent</artifactId>
+    <groupId>org.apache.camel</groupId>
+    <version>4.8.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>camel-langchain4j-web-search</artifactId>
+  <packaging>jar</packaging>
+  <name>Camel :: LangChain4j :: Web Search</name>

Review Comment:
   Cosmetic stuff: we recently had a pr that changed the name of all `ai` 
components to `Camel :: AI :: stuff`



##########
components/camel-ai/camel-langchain4j-web-search/src/main/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchProducer.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.langchain4j.web.search;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import dev.langchain4j.web.search.WebSearchEngine;
+import dev.langchain4j.web.search.WebSearchOrganicResult;
+import dev.langchain4j.web.search.WebSearchRequest;
+import dev.langchain4j.web.search.WebSearchResults;
+import org.apache.camel.Exchange;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+
+public class LangChain4jWebSearchProducer extends DefaultProducer {
+
+    private WebSearchEngine webSearchEngine;
+
+    public LangChain4jWebSearchProducer(LangChain4jWebSearchEndpoint endpoint) 
{
+        super(endpoint);
+    }
+
+    @Override
+    public LangChain4jWebSearchEndpoint getEndpoint() {
+        return (LangChain4jWebSearchEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        // check if there's a custom WebSearchRequest -- advanced
+        WebSearchRequest webSearchRequest = 
getEndpoint().getConfiguration().getWebSearchRequest();
+
+        // build a Web Search Request
+        if (webSearchRequest == null) {
+            final String searchTerms = 
exchange.getMessage().getMandatoryBody(String.class);
+            webSearchRequest = WebSearchRequest.builder()
+                    .searchTerms(searchTerms)
+                    
.maxResults(getEndpoint().getConfiguration().getMaxResults())
+                    .language(getEndpoint().getConfiguration().getLanguage())
+                    
.geoLocation(getEndpoint().getConfiguration().getGeoLocation())
+                    .startPage(getEndpoint().getConfiguration().getStartPage())
+                    
.startIndex(getEndpoint().getConfiguration().getStartIndex())
+                    
.additionalParams(getEndpoint().getConfiguration().getAdditionalParams())
+                    .build();
+        }
+
+        // perform the request
+        WebSearchResults webSearchResults = 
webSearchEngine.search(webSearchRequest);
+
+        // exrtact the list
+        List<WebSearchOrganicResult> resultList = webSearchResults.results();
+
+        // compute the response
+        computeResponse(resultList, exchange, webSearchRequest.maxResults());
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        this.webSearchEngine = 
getEndpoint().getConfiguration().getWebSearchEngine();
+        ObjectHelper.notNull(webSearchEngine, "webSearchEngine");
+    }
+
+    /**
+     * Computes the response of the web search based on the configuration and 
input results.
+     *
+     * @param webSearchOrganicResults the list of WebSearchOrganicResult 
objects to process
+     * @param exchange                the Apache Camel Exchange object
+     * @param maxResults              maxResults
+     */
+    private void computeResponse(List<WebSearchOrganicResult> 
webSearchOrganicResults, Exchange exchange, Integer maxResults) {
+        // return a single object as a response
+        if (maxResults == 1) {
+            switch (getEndpoint().getConfiguration().getResultType()) {
+                case LANGCHAIN4J_WEB_SEARCH_ORGNAIC_RESULT -> 
exchange.getIn().setBody(webSearchOrganicResults.get(0));

Review Comment:
   I think there's a typo here in "organic"



##########
components/camel-ai/camel-langchain4j-web-search/src/test/java/org/apache/camel/component/langchain4j/web/search/LangChain4jGoogleWebSearchEngineIT.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.langchain4j.web.search;
+
+import java.util.List;
+
+import dev.langchain4j.web.search.WebSearchEngine;
+import 
dev.langchain4j.web.search.google.customsearch.GoogleCustomWebSearchEngine;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+// Instruction to generate one 
https://developers.google.com/custom-search/v1/overview
+@EnabledIfEnvironmentVariable(named = "GOOGLE_API_KEY", matches = ".*")
+// Instructions to create a Google search engine 
https://developers.google.com/custom-search/docs/tutorial/creatingcse
+@EnabledIfEnvironmentVariable(named = "GOOGLE_SEARCH_ENGINE_ID", matches = 
".*")
+public class LangChain4jGoogleWebSearchEngineIT extends CamelTestSupport {
+    public static final String WEB_SEARCH_URI = 
"langchain4j-web-search:googleTest";
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        WebSearchEngine googleSearchEngine = 
GoogleCustomWebSearchEngine.builder()
+                .apiKey(System.getenv("GOOGLE_API_KEY"))
+                .csi(System.getenv("GOOGLE_SEARCH_ENGINE_ID"))
+                .logRequests(true)
+                .logResponses(true)
+                .build();
+
+        context.getRegistry().bind("web-search-engine", googleSearchEngine);
+
+        return context;
+    }
+
+    @Test
+    void testSimpleSearch() {
+
+        String uri = String.format("%s?resultType=%s", WEB_SEARCH_URI, 
LangChain4jWebSearchResultType.SNIPPET);
+        Exchange result = fluentTemplate.to(uri)
+                .withBody("Who won the European Cup in 2024?")
+                .request(Exchange.class);
+
+        assertNotNull(result);

Review Comment:
   Please add a message for the assertion (this and others)



##########
components/camel-ai/camel-langchain4j-web-search/src/test/java/org/apache/camel/component/langchain4j/web/search/LangChain4jGoogleWebSearchEngineIT.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.langchain4j.web.search;
+
+import java.util.List;
+
+import dev.langchain4j.web.search.WebSearchEngine;
+import 
dev.langchain4j.web.search.google.customsearch.GoogleCustomWebSearchEngine;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+// Instruction to generate one 
https://developers.google.com/custom-search/v1/overview
+@EnabledIfEnvironmentVariable(named = "GOOGLE_API_KEY", matches = ".*")
+// Instructions to create a Google search engine 
https://developers.google.com/custom-search/docs/tutorial/creatingcse
+@EnabledIfEnvironmentVariable(named = "GOOGLE_SEARCH_ENGINE_ID", matches = 
".*")
+public class LangChain4jGoogleWebSearchEngineIT extends CamelTestSupport {
+    public static final String WEB_SEARCH_URI = 
"langchain4j-web-search:googleTest";
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        WebSearchEngine googleSearchEngine = 
GoogleCustomWebSearchEngine.builder()
+                .apiKey(System.getenv("GOOGLE_API_KEY"))
+                .csi(System.getenv("GOOGLE_SEARCH_ENGINE_ID"))
+                .logRequests(true)
+                .logResponses(true)
+                .build();
+
+        context.getRegistry().bind("web-search-engine", googleSearchEngine);
+
+        return context;
+    }

Review Comment:
   This API is becoming more restricted with 4.8 and, in the future, 4.9. I'd 
suggest using 
   
   `protected void bindToRegistry(Registry registry) throws Exception {`
   
   instead.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to