Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bom/camel-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,11 @@
<artifactId>camel-langchain4j-chat</artifactId>
<version>4.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-core</artifactId>
<version>4.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-embeddings</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions catalog/camel-allcomponents/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,11 @@
<artifactId>camel-langchain4j-chat</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-embeddings</artifactId>
Expand Down
5 changes: 2 additions & 3 deletions components/camel-ai/camel-langchain4j-chat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@
<artifactId>camel-support</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>${langchain4j-version}</version>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-core</artifactId>
</dependency>

<!-- for testing -->
Expand Down
73 changes: 73 additions & 0 deletions components/camel-ai/camel-langchain4j-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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.7.0-SNAPSHOT</version>
</parent>

<artifactId>camel-langchain4j-core</artifactId>
<packaging>jar</packaging>
<name>Camel :: LangChain4j :: Core</name>
<description>LangChain4j Core</description>

<properties>
<firstVersion>4.7.0</firstVersion>
<label>ai</label>
<title>LangChain4j Core</title>
<supportLevel>Experimental</supportLevel>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-support</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-core</artifactId>
<version>${langchain4j-version}</version>
</dependency>


<!-- test -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Generated by camel build tools - do NOT edit this file! */
package org.apache.camel.component.langchain4j.core;

import javax.annotation.processing.Generated;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.DeferredContextBinding;
import org.apache.camel.Exchange;
import org.apache.camel.TypeConversionException;
import org.apache.camel.TypeConverterLoaderException;
import org.apache.camel.spi.TypeConverterLoader;
import org.apache.camel.spi.TypeConverterRegistry;
import org.apache.camel.support.SimpleTypeConverter;
import org.apache.camel.support.TypeConverterSupport;
import org.apache.camel.util.DoubleMap;

/**
* Generated by camel build tools - do NOT edit this file!
*/
@Generated("org.apache.camel.maven.packaging.TypeConverterLoaderGeneratorMojo")
@SuppressWarnings("unchecked")
@DeferredContextBinding
public final class LangChain4jConverterLoader implements TypeConverterLoader, CamelContextAware {

private CamelContext camelContext;

public LangChain4jConverterLoader() {
}

@Override
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}

@Override
public CamelContext getCamelContext() {
return camelContext;
}

@Override
public void load(TypeConverterRegistry registry) throws TypeConverterLoaderException {
registerConverters(registry);
}

private void registerConverters(TypeConverterRegistry registry) {
addTypeConverter(registry, dev.langchain4j.data.document.Document.class, java.lang.String.class, false,
(type, exchange, value) -> org.apache.camel.component.langchain4j.core.LangChain4jConverter.toDocument((java.lang.String) value, exchange));
}

private static void addTypeConverter(TypeConverterRegistry registry, Class<?> toType, Class<?> fromType, boolean allowNull, SimpleTypeConverter.ConversionMethod method) {
registry.addTypeConverter(toType, fromType, new SimpleTypeConverter(allowNull, method));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by camel build tools - do NOT edit this file!
org.apache.camel.component.langchain4j.core.LangChain4jConverterLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by camel build tools - do NOT edit this file!
name=langchain4j-core
groupId=org.apache.camel
artifactId=camel-langchain4j-core
version=4.7.0-SNAPSHOT
projectName=Camel :: LangChain4j :: Core
projectDescription=LangChain4j Core
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"other": {
"kind": "other",
"name": "langchain4j-core",
"title": "LangChain4j Core",
"description": "LangChain4j Core",
"deprecated": false,
"firstVersion": "4.7.0",
"label": "ai",
"supportLevel": "Experimental",
"groupId": "org.apache.camel",
"artifactId": "camel-langchain4j-core",
"version": "4.7.0-SNAPSHOT"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.core;

public final class LangChain4j {
public static final String METADATA_PREFIX = "langchain4j.metadata.";
public static final int METADATA_PREFIX_LEN = METADATA_PREFIX.length();

private LangChain4j() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.core;

import java.util.HashMap;
import java.util.Map;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.Metadata;
import org.apache.camel.Converter;
import org.apache.camel.Exchange;

/**
* Converter methods to convert from / to LangChain4j types.
*/
@Converter(generateLoader = true)
public class LangChain4jConverter {

@Converter
public static Document toDocument(String value, Exchange exchange) {
return Document.document(
value,
toMetadata(exchange));
}

private static Metadata toMetadata(Exchange exchange) {
if (exchange == null) {
return new Metadata();
}
if (exchange.getMessage() == null) {
return new Metadata();
}

Map<String, Object> metadata = null;
Map<String, Object> headers = exchange.getMessage().getHeaders();

for (Map.Entry<String, Object> entry : headers.entrySet()) {
if (!entry.getKey().startsWith(LangChain4j.METADATA_PREFIX)) {
continue;
}

if (metadata == null) {
metadata = new HashMap<>();
}

metadata.put(
entry.getKey().substring(LangChain4j.METADATA_PREFIX_LEN),
entry.getValue());
}

return metadata != null ? Metadata.from(metadata) : new Metadata();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.core;

import dev.langchain4j.data.document.Document;
import org.apache.camel.Exchange;
import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class LangChain4jConverterTest extends CamelTestSupport {

@Test
public void testConverter() throws NoTypeConversionAvailableException {
Document doc = context.getTypeConverter().mandatoryConvertTo(Document.class, "foo");
assertThat(doc.text()).isEqualTo("foo");

Exchange exchange = new DefaultExchange(context);
exchange.getMessage().setHeader(LangChain4j.METADATA_PREFIX + "meta1", 1);
exchange.getMessage().setHeader(LangChain4j.METADATA_PREFIX + "meta2", "baz");
exchange.getMessage().setHeader("meta3", "ignored");

Document docWithMetadata = context.getTypeConverter().mandatoryConvertTo(
Document.class,
exchange,
"bar");

assertThat(docWithMetadata.text()).isEqualTo("bar");
assertThat(docWithMetadata.metadata().toMap())
.containsEntry("meta1", 1)
.containsEntry("meta2", "baz")
.doesNotContainKey("meta3");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------

appender.file.type = File
appender.file.name = file
appender.file.fileName = target/camel-langchain4j-core-test.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n

appender.out.type = Console
appender.out.name = out
appender.out.layout.type = PatternLayout
appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n

rootLogger.level = INFO
rootLogger.appenderRef.file.ref = file
# To log to stdout
#rootLogger.appenderRef.out.ref = out

logger.camel-langchain4j-core.name = org.apache.camel.component.langchain4j.core
logger.camel-langchain4j-core.level = DEBUG
5 changes: 2 additions & 3 deletions components/camel-ai/camel-langchain4j-embeddings/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@
<artifactId>camel-support</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-core</artifactId>
<version>${langchain4j-version}</version>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-core</artifactId>
</dependency>

<!-- test dependencies -->
Expand Down
1 change: 1 addition & 0 deletions components/camel-ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<modules>
<module>camel-chatscript</module>
<module>camel-djl</module>
<module>camel-langchain4j-core</module>
<module>camel-langchain4j-chat</module>
<module>camel-langchain4j-embeddings</module>
</modules>
Expand Down
Loading