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


##########
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:
   Are we 100% sure we won't get and IndexOutOfBoundsException?



##########
components/camel-ai/camel-langchain4j-web-search/src/main/docs/langchain4j-web-search-component.adoc:
##########
@@ -0,0 +1,159 @@
+= LangChain4j Web Search Component
+:doctitle: LangChain4j Web Search
+:shortname: langchain4j-web-search
+:artifactid: camel-langchain4j-web-search
+:description: LangChain4j Web Search Engine
+:since: 4.8
+:supportlevel: Preview
+:tabs-sync-option:
+:component-header: Only producer is supported
+//Manually maintained attributes
+:group: AI
+:camel-spring-boot-name: langchain4j-web-search
+
+*Since Camel {since}*
+
+*{component-header}*
+
+The LangChain4j Web Search component provides support to perform a web search 
using the https://docs.langchain4j.dev/[LangChain4j] Web Search Engines.
+
+Maven users will need to add the following dependency to their `pom.xml`
+for this component:
+
+[source,xml]
+----
+<dependency>
+    <groupId>org.apache.camel</groupId>
+    <artifactId>camel-langchain4j-web-search</artifactId>
+    <version>x.x.x</version>
+    <!-- use the same version as your Camel core version -->
+</dependency>
+----
+
+
+== URI format
+
+[source]
+----
+langchain4j-web-search:searchId[?options]
+----
+
+Where *searchId* can be any string to uniquely identify the endpoint
+
+
+// component-configure options: START
+
+// component-configure options: END
+
+// component options: START
+include::partial$component-configure-options.adoc[]
+include::partial$component-endpoint-options.adoc[]
+// component options: END
+
+// endpoint options: START
+
+// endpoint options: END
+
+// component headers: START
+include::partial$component-endpoint-headers.adoc[]
+// component headers: END
+
+include::spring-boot:partial$starter.adoc[]
+
+
+== Using a specific Web Search Engine
+The Camel LangChain4j web search component provides an abstraction for 
interacting with various types of Web Search Engines supported by 
https://github.com/langchain4j/langchain4j[LangChain4j].
+
+To integrate with a specific Web Search Engine, users should follow these 
steps:
+
+=== Example of integrating with Tavily
+Add the dependency for LangChain4j Tavily Web Search Engine support :
+
+[source,xml]
+----
+<dependency>

Review Comment:
   This could be injected also by looking at the route or the configuration.



##########
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:
   We are passing the maxResult parameter, but I would add a guard check for 
looking at the size.



-- 
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