Skip to content

Commit

Permalink
Merge branch 'langchain4j:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhas-Koheda authored Dec 21, 2024
2 parents ed5afe0 + cb8421a commit 9b52b6e
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ AzureOpenAiChatModel openAiChatModel(Properties properties) {
.presencePenalty(chatModelProperties.presencePenalty())
.frequencyPenalty(chatModelProperties.frequencyPenalty())
.seed(chatModelProperties.seed())
.strictJsonSchema(chatModelProperties.strictJsonSchema())
.timeout(Duration.ofSeconds(chatModelProperties.timeout() == null ? 0 : chatModelProperties.timeout()))
.maxRetries(chatModelProperties.maxRetries())
.proxyOptions(ProxyOptions.fromConfiguration(Configuration.getGlobalConfiguration()))
.logRequestsAndResponses(chatModelProperties.logRequestsAndResponses() != null && chatModelProperties.logRequestsAndResponses())
.userAgentSuffix(chatModelProperties.userAgentSuffix())
.customHeaders(chatModelProperties.customHeaders());
.customHeaders(chatModelProperties.customHeaders())
.supportedCapabilities(chatModelProperties.supportedCapabilities());
if (chatModelProperties.nonAzureApiKey() != null) {
builder.nonAzureApiKey(chatModelProperties.nonAzureApiKey());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.langchain4j.azure.openai.spring;

import dev.langchain4j.model.chat.Capability;

import java.util.List;
import java.util.Map;
import java.util.Set;

record ChatModelProperties(

Expand All @@ -18,12 +21,13 @@ record ChatModelProperties(
Double presencePenalty,
Double frequencyPenalty,
Long seed,
String responseFormat,
Boolean strictJsonSchema,
Integer timeout, // TODO use Duration instead
Integer maxRetries,
Boolean logRequestsAndResponses,
String userAgentSuffix,
Map<String, String> customHeaders,
String nonAzureApiKey
String nonAzureApiKey,
Set<Capability> supportedCapabilities
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import dev.langchain4j.model.azure.AzureOpenAiStreamingChatModel;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.request.ResponseFormat;
import dev.langchain4j.model.chat.request.json.JsonArraySchema;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.chat.request.json.JsonSchema;
import dev.langchain4j.model.chat.request.json.JsonStringSchema;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.output.Response;
Expand All @@ -17,8 +23,12 @@
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import static dev.langchain4j.data.message.UserMessage.userMessage;
import static dev.langchain4j.model.chat.request.ResponseFormatType.JSON;
import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -53,6 +63,52 @@ void should_provide_chat_model(String deploymentName) {
});
}

class Person {

String name;
List<String> favouriteColors;
}

@ParameterizedTest(name = "Deployment name: {0}")
@CsvSource({
"gpt-4o-mini"
})
void should_provide_chat_model_with_json_schema(String deploymentName) {
contextRunner
.withPropertyValues(
"langchain4j.azure-open-ai.chat-model.api-key=" + AZURE_OPENAI_KEY,
"langchain4j.azure-open-ai.chat-model.endpoint=" + AZURE_OPENAI_ENDPOINT,
"langchain4j.azure-open-ai.chat-model.deployment-name=" + deploymentName,
"langchain4j.azure-open-ai.chat-model.strict-json-schema=true"
)
.run(context -> {

ChatLanguageModel chatLanguageModel = context.getBean(ChatLanguageModel.class);

ChatRequest chatRequest = ChatRequest.builder()
.messages(singletonList(userMessage("Julien likes blue, white and red")))
.responseFormat(ResponseFormat.builder()
.type(JSON)
.jsonSchema(JsonSchema.builder()
.name("Person")
.rootElement(JsonObjectSchema.builder()
.addStringProperty("name")
.addProperty("favouriteColors", JsonArraySchema.builder()
.items(new JsonStringSchema())
.build())
.required("name", "favouriteColors")
.build())
.build())
.build())
.build();

assertThat(chatLanguageModel).isInstanceOf(AzureOpenAiChatModel.class);
AiMessage aiMessage = chatLanguageModel.chat(chatRequest).aiMessage();
assertThat(aiMessage.text()).contains("{\"name\":\"Julien\",\"favouriteColors\":[\"blue\",\"white\",\"red\"]}");
assertThat(context.getBean(AzureOpenAiChatModel.class)).isSameAs(chatLanguageModel);
});
}

@ParameterizedTest(name = "Deployment name: {0}")
@CsvSource({
"gpt-3.5-turbo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OllamaChatModel ollamaChatModel(Properties properties) {
.numPredict(chatModelProperties.getNumPredict())
.stop(chatModelProperties.getStop())
.format(chatModelProperties.getFormat())
.supportedCapabilities(chatModelProperties.getSupportedCapabilities())
.timeout(chatModelProperties.getTimeout())
.maxRetries(chatModelProperties.getMaxRetries())
.customHeaders(chatModelProperties.getCustomHeaders())
Expand All @@ -50,6 +51,7 @@ OllamaStreamingChatModel ollamaStreamingChatModel(Properties properties) {
.numPredict(chatModelProperties.getNumPredict())
.stop(chatModelProperties.getStop())
.format(chatModelProperties.getFormat())
.supportedCapabilities(chatModelProperties.getSupportedCapabilities())
.timeout(chatModelProperties.getTimeout())
.customHeaders(chatModelProperties.getCustomHeaders())
.logRequests(chatModelProperties.getLogRequests())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.langchain4j.ollama.spring;

import dev.langchain4j.model.chat.Capability;
import lombok.Getter;
import lombok.Setter;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Getter
@Setter
Expand All @@ -21,6 +23,7 @@ class ChatModelProperties {
Integer numPredict;
List<String> stop;
String format;
Set<Capability> supportedCapabilities;
Duration timeout;
Integer maxRetries;
Map<String, String> customHeaders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.concurrent.CompletableFuture;

import static dev.langchain4j.model.chat.Capability.RESPONSE_FORMAT_JSON_SCHEMA;
import static java.lang.String.format;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -57,6 +58,25 @@ void should_provide_chat_model() {
});
}

@Test
void should_provide_chat_model_with_supported_capabilities() {
contextRunner
.withPropertyValues(
"langchain4j.ollama.chat-model.base-url=" + baseUrl(),
"langchain4j.ollama.chat-model.model-name=" + MODEL_NAME,
"langchain4j.ollama.chat-model.supportedCapabilities=RESPONSE_FORMAT_JSON_SCHEMA"
)
.run(context -> {

ChatLanguageModel chatLanguageModel = context.getBean(ChatLanguageModel.class);
assertThat(chatLanguageModel).isInstanceOf(OllamaChatModel.class);
assertThat(chatLanguageModel.supportedCapabilities()).contains(RESPONSE_FORMAT_JSON_SCHEMA);

assertThat(context.getBean(OllamaChatModel.class)).isSameAs(chatLanguageModel);
});
}


@Test
void should_provide_streaming_chat_model() {
contextRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ OpenAiEmbeddingModel openAiEmbeddingModel(Properties properties) {
.organizationId(embeddingModelProperties.organizationId())
.modelName(embeddingModelProperties.modelName())
.dimensions(embeddingModelProperties.dimensions())
.maxSegmentsPerBatch(embeddingModelProperties.maxSegmentsPerBatch())
.user(embeddingModelProperties.user())
.timeout(embeddingModelProperties.timeout())
.maxRetries(embeddingModelProperties.maxRetries())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ record EmbeddingModelProperties(
String organizationId,
String modelName,
Integer dimensions,
Integer maxSegmentsPerBatch,
String user,
Duration timeout,
Integer maxRetries,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.langchain4j.service.spring;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.agent.tool.ToolSpecifications;
import dev.langchain4j.exception.IllegalConfigurationException;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
Expand All @@ -9,19 +11,21 @@
import dev.langchain4j.model.moderation.ModerationModel;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.spring.event.AiServiceRegisteredEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.annotation.Bean;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

import static dev.langchain4j.exception.IllegalConfigurationException.illegalConfiguration;
import static dev.langchain4j.internal.Exceptions.illegalArgument;
Expand All @@ -31,7 +35,16 @@
import static dev.langchain4j.service.spring.AiServiceWiringMode.EXPLICIT;
import static java.util.Arrays.asList;

public class AiServicesAutoConfig {
public class AiServicesAutoConfig implements ApplicationEventPublisherAware {

private static final Logger log = LoggerFactory.getLogger(AiServicesAutoConfig.class);

private ApplicationEventPublisher eventPublisher;

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

@Bean
BeanFactoryPostProcessor aiServicesRegisteringBeanFactoryPostProcessor() {
Expand All @@ -46,7 +59,8 @@ BeanFactoryPostProcessor aiServicesRegisteringBeanFactoryPostProcessor() {
String[] retrievalAugmentors = beanFactory.getBeanNamesForType(RetrievalAugmentor.class);
String[] moderationModels = beanFactory.getBeanNamesForType(ModerationModel.class);

Set<String> tools = new HashSet<>();
Set<String> toolBeanNames = new HashSet<>();
List<ToolSpecification> toolSpecifications = new ArrayList<>();
for (String beanName : beanFactory.getBeanDefinitionNames()) {
try {
String beanClassName = beanFactory.getBeanDefinition(beanName).getBeanClassName();
Expand All @@ -56,7 +70,13 @@ BeanFactoryPostProcessor aiServicesRegisteringBeanFactoryPostProcessor() {
Class<?> beanClass = Class.forName(beanClassName);
for (Method beanMethod : beanClass.getDeclaredMethods()) {
if (beanMethod.isAnnotationPresent(Tool.class)) {
tools.add(beanName);
toolBeanNames.add(beanName);
try {
toolSpecifications.add(ToolSpecifications.toolSpecificationFrom(beanMethod));
} catch (Exception e) {
log.warn("Cannot convert %s.%s method annotated with @Tool into ToolSpecification"
.formatted(beanClass.getName(), beanMethod.getName()), e);
}
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -148,14 +168,18 @@ BeanFactoryPostProcessor aiServicesRegisteringBeanFactoryPostProcessor() {
if (aiServiceAnnotation.wiringMode() == EXPLICIT) {
propertyValues.add("tools", toManagedList(asList(aiServiceAnnotation.tools())));
} else if (aiServiceAnnotation.wiringMode() == AUTOMATIC) {
propertyValues.add("tools", toManagedList(tools));
propertyValues.add("tools", toManagedList(toolBeanNames));
} else {
throw illegalArgument("Unknown wiring mode: " + aiServiceAnnotation.wiringMode());
}

BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
registry.removeBeanDefinition(aiService);
registry.registerBeanDefinition(lowercaseFirstLetter(aiService), aiServiceBeanDefinition);

if (eventPublisher != null) {
eventPublisher.publishEvent(new AiServiceRegisteredEvent(this, aiServiceClass, toolSpecifications));
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.langchain4j.service.spring.event;

import dev.langchain4j.agent.tool.ToolSpecification;
import org.springframework.context.ApplicationEvent;

import java.util.List;

import static dev.langchain4j.internal.Utils.copyIfNotNull;

public class AiServiceRegisteredEvent extends ApplicationEvent {

private final Class<?> aiServiceClass;
private final List<ToolSpecification> toolSpecifications;

public AiServiceRegisteredEvent(Object source, Class<?> aiServiceClass, List<ToolSpecification> toolSpecifications) {
super(source);
this.aiServiceClass = aiServiceClass;
this.toolSpecifications = copyIfNotNull(toolSpecifications);
}

public Class<?> aiServiceClass() {
return aiServiceClass;
}

public List<ToolSpecification> toolSpecifications() {
return toolSpecifications;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package dev.langchain4j.service.spring.mode.automatic.withTools;

import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.service.spring.event.AiServiceRegisteredEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;

import java.util.List;

@SpringBootApplication
class AiServiceWithToolsApplication {
class AiServiceWithToolsApplication implements ApplicationListener<AiServiceRegisteredEvent> {

public static void main(String[] args) {
SpringApplication.run(AiServiceWithToolsApplication.class, args);
}

@Override
public void onApplicationEvent(AiServiceRegisteredEvent event) {
Class<?> aiServiceClass = event.aiServiceClass();
List<ToolSpecification> toolSpecifications = event.toolSpecifications();
for (int i = 0; i < toolSpecifications.size(); i++) {
System.out.printf("[%s]: [Tool-%s]: %s%n", aiServiceClass.getSimpleName(), i + 1, toolSpecifications.get(i));
}
}
}
Loading

0 comments on commit 9b52b6e

Please sign in to comment.