Skip to content

Commit

Permalink
refactor(ai): Improve modularization of AI logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Jan 7, 2025
1 parent 3981a66 commit 6393ae6
Show file tree
Hide file tree
Showing 51 changed files with 408 additions and 259 deletions.
33 changes: 33 additions & 0 deletions arconia-ai/arconia-ai-client/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id 'code-quality-conventions'
id 'java-conventions'
id 'sbom-conventions'
id 'release-conventions'
}

repositories {
maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
implementation project(":arconia-ai:arconia-ai-tools")

implementation "org.slf4j:slf4j-api"
implementation "org.springframework:spring-context"

compileOnly "org.springframework.ai:spring-ai-core:${springAiVersion}"

testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation "org.springframework.ai:spring-ai-core:${springAiVersion}"
}

publishing {
publications {
mavenJava(MavenPublication) {
pom {
name = "Arconia AI Client"
description = "Arconia AI Client."
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.arconia.ai.core.client;
package io.arconia.ai.client;

import java.nio.charset.Charset;
import java.util.List;
Expand All @@ -22,7 +22,7 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import io.arconia.ai.core.tools.ToolCallback;
import io.arconia.ai.tools.ToolCallback;

/**
* A {@link ChatClient} enhanced for more advanced features.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.arconia.ai.core.client;
package io.arconia.ai.client;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -33,10 +33,10 @@
import org.springframework.util.MimeType;
import org.springframework.util.StringUtils;

import io.arconia.ai.core.client.advisor.CallAdvisor;
import io.arconia.ai.core.client.advisor.StreamAdvisor;
import io.arconia.ai.core.tools.ToolCallback;
import io.arconia.ai.core.tools.ToolCallbacks;
import io.arconia.ai.client.advisor.CallAdvisor;
import io.arconia.ai.client.advisor.StreamAdvisor;
import io.arconia.ai.tools.ToolCallback;
import io.arconia.ai.tools.ToolCallbacks;

/**
* Default implementation of {@link ArconiaChatClient} based on {@link DefaultChatClient}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.arconia.ai.core.client;
package io.arconia.ai.client;

import java.io.IOException;
import java.nio.charset.Charset;
Expand All @@ -21,9 +21,9 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import io.arconia.ai.core.client.DefaultArconiaChatClient.DefaultArconiaChatClientRequestSpec;
import io.arconia.ai.core.tools.ToolCallback;
import io.arconia.ai.core.tools.ToolCallbacks;
import io.arconia.ai.client.DefaultArconiaChatClient.DefaultArconiaChatClientRequestSpec;
import io.arconia.ai.tools.ToolCallback;
import io.arconia.ai.tools.ToolCallbacks;

/**
* Default implementation of {@link ArconiaChatClient.ArconiaBuilder}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.arconia.ai.core.client.advisor;
package io.arconia.ai.client.advisor;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.arconia.ai.core.client.advisor;
package io.arconia.ai.client.advisor;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@NonNullApi
@NonNullFields
package io.arconia.ai.core.tools.aot;
package io.arconia.ai.client.advisor;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@NonNullApi
@NonNullFields
package io.arconia.ai.core.client;
package io.arconia.ai.client;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion arconia-ai/arconia-ai-mcp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repositories {
}

dependencies {
implementation project(":arconia-ai:arconia-ai-core")
implementation project(":arconia-ai:arconia-ai-tools")

implementation "org.slf4j:slf4j-api"
implementation "org.springframework:spring-context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,84 @@
package io.arconia.ai.mcp.tools;

import java.util.Map;
import java.util.function.Function;

import com.fasterxml.jackson.core.type.TypeReference;

import org.springframework.ai.mcp.client.McpSyncClient;
import org.springframework.ai.mcp.spec.McpSchema;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.lang.Nullable;

import io.arconia.ai.core.tools.ToolCallback;
import io.arconia.ai.core.tools.json.JsonParser;
import io.arconia.ai.core.tools.metadata.DefaultToolMetadata;
import io.arconia.ai.core.tools.metadata.ToolMetadata;
import io.arconia.ai.tools.ToolCallback;
import io.arconia.ai.tools.definition.ToolDefinition;
import io.arconia.ai.tools.json.JsonParser;

/**
* A {@link ToolCallback} for handling calls to MCP tools.
*/
public class McpToolCallback implements ToolCallback {

private final ToolMetadata toolMetadata;
private static final Function<String,Map<String,Object>> DEFAULT_TOOL_INPUT_PARSER =
toolInput -> JsonParser.fromJson(toolInput, new TypeReference<>() {});

private final ToolDefinition toolDefinition;
private final McpSyncClient mcpClient;
private final Function<String,Map<String,Object>> toolInputParser;

public McpToolCallback(McpSchema.Tool tool, McpSyncClient mcpClient) {
this.toolMetadata = DefaultToolMetadata.builder()
public McpToolCallback(McpSchema.Tool tool, McpSyncClient mcpClient, @Nullable Function<String,Map<String,Object>> toolInputParser) {
this.toolDefinition = ToolDefinition.builder()
.name(tool.name())
.description(tool.description())
.inputTypeSchema(JsonParser.toJson(tool.inputSchema()))
.build();
this.mcpClient = mcpClient;
this.toolInputParser = toolInputParser != null ? toolInputParser : DEFAULT_TOOL_INPUT_PARSER;
}

@Override
public ToolMetadata getToolMetadata() {
return this.toolMetadata;
public ToolDefinition getToolDefinition() {
return toolDefinition;
}

@Override
public String call(String toolInput) {
Map<String, Object> arguments = JsonParser.fromJson(toolInput, new TypeReference<>() {});
Map<String, Object> arguments = toolInputParser.apply(toolInput);
McpSchema.CallToolResult response = this.mcpClient.callTool(new McpSchema.CallToolRequest(this.getName(), arguments));
return ModelOptionsUtils.toJsonString(response.content());
}

public static Builder builder() {
return new Builder();
}

public static class Builder {

private McpSchema.Tool tool;
private McpSyncClient mcpClient;
private Function<String,Map<String,Object>> toolInputParser;

private Builder() {}

public Builder tool(McpSchema.Tool tool) {
this.tool = tool;
return this;
}

public Builder mcpClient(McpSyncClient mcpClient) {
this.mcpClient = mcpClient;
return this;
}

public Builder toolInputParser(Function<String,Map<String,Object>> toolInputParser) {
this.toolInputParser = toolInputParser;
return this;
}

public McpToolCallback build() {
return new McpToolCallback(tool, mcpClient, toolInputParser);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.springframework.ai.mcp.client.McpSyncClient;
import org.springframework.util.Assert;

import io.arconia.ai.core.tools.ToolCallback;
import io.arconia.ai.core.tools.ToolCallbackProvider;
import io.arconia.ai.core.tools.util.ToolUtils;
import io.arconia.ai.tools.ToolCallback;
import io.arconia.ai.tools.ToolCallbackProvider;
import io.arconia.ai.tools.util.ToolUtils;

/**
* A {@link ToolCallbackProvider} that builds {@link ToolCallback} instances from MCP
Expand All @@ -32,7 +32,10 @@ public ToolCallback[] getToolCallbacks() {
.flatMap(mcpClient -> mcpClient.listTools()
.tools()
.stream()
.map(tool -> new McpToolCallback(tool, mcpClient)))
.map(tool -> McpToolCallback.builder()
.tool(tool)
.mcpClient(mcpClient)
.build()))
.toArray(ToolCallback[]::new);

validateToolCallbacks(toolCallbacks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import org.springframework.ai.mcp.client.McpAsyncClient;
import org.springframework.ai.mcp.client.McpSyncClient;
import org.springframework.ai.model.function.FunctionCallback;

import io.arconia.ai.core.tools.ToolCallback;
import io.arconia.ai.tools.ToolCallback;

/**
* Provides {@link ToolCallback} instances for tools defined in different sources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ dependencies {
annotationProcessor 'org.springframework.boot:spring-boot-autoconfigure-processor'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

api project(":arconia-ai:arconia-ai-core")
api project(":arconia-ai:arconia-ai-client")
api project(":arconia-ai:arconia-ai-tools")

implementation "org.slf4j:slf4j-api"
implementation "org.springframework.boot:spring-boot-starter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import io.arconia.ai.core.client.ArconiaChatClient;
import io.arconia.ai.client.ArconiaChatClient;

@AutoConfiguration
public class ArconiaChatClientAutoConfiguration {
Expand Down
Loading

0 comments on commit 6393ae6

Please sign in to comment.