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
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,36 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt;
package schemacrawler.tools.command.aichat;

import java.util.logging.Level;
import java.util.logging.Logger;
import io.github.sashirestela.openai.SimpleOpenAI;
import schemacrawler.tools.command.chatgpt.options.ChatGPTCommandOptions;
import schemacrawler.tools.command.aichat.options.AiChatCommandOptions;
import schemacrawler.tools.executable.BaseSchemaCrawlerCommand;

/** SchemaCrawler command plug-in. */
public final class ChatGPTCommand extends BaseSchemaCrawlerCommand<ChatGPTCommandOptions> {
public final class AiChatCommand extends BaseSchemaCrawlerCommand<AiChatCommandOptions> {

private static final Logger LOGGER = Logger.getLogger(ChatGPTCommand.class.getName());
private static final Logger LOGGER = Logger.getLogger(AiChatCommand.class.getName());

static final String COMMAND = "chatgpt";
static final String COMMAND = "aichat";

protected ChatGPTCommand() {
protected AiChatCommand() {
super(COMMAND);
}

@Override
public void checkAvailability() throws RuntimeException {
LOGGER.log(Level.FINE, "Looking for OPENAI_API_KEY environmental variable");
SimpleOpenAI.builder().apiKey(System.getenv("OPENAI_API_KEY")).build();
}

@Override
public void execute() {
try (ChatGPTConsole chatGPTConsole =
new ChatGPTConsole(commandOptions, catalog, connection); ) {
chatGPTConsole.console();
try (AiChatConsole aiChatConsole =
new AiChatConsole(commandOptions, catalog, connection); ) {
aiChatConsole.console();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,37 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt;
package schemacrawler.tools.command.aichat;

import static schemacrawler.tools.executable.commandline.PluginCommand.newPluginCommand;
import schemacrawler.schemacrawler.exceptions.ExecutionRuntimeException;
import schemacrawler.tools.command.chatgpt.options.ChatGPTCommandOptions;
import schemacrawler.tools.command.chatgpt.options.ChatGPTCommandOptionsBuilder;
import schemacrawler.tools.command.aichat.options.AiChatCommandOptions;
import schemacrawler.tools.command.aichat.options.AiChatCommandOptionsBuilder;
import schemacrawler.tools.executable.BaseCommandProvider;
import schemacrawler.tools.executable.commandline.PluginCommand;
import schemacrawler.tools.options.Config;
import schemacrawler.tools.options.OutputOptions;
import us.fatehi.utility.property.PropertyName;

/** SchemaCrawler command plug-in for ChatGPT. */
public final class ChatGPTCommandProvider extends BaseCommandProvider {
/** SchemaCrawler command plug-in for AI chat. */
public final class AiChatCommandProvider extends BaseCommandProvider {

public static final String DESCRIPTION_HEADER = "SchemaCrawler ChatGPT integration";
public static final String DESCRIPTION_HEADER = "SchemaCrawler AI chat integration";

public ChatGPTCommandProvider() {
super(new PropertyName(ChatGPTCommand.COMMAND, DESCRIPTION_HEADER));
public AiChatCommandProvider() {
super(new PropertyName(AiChatCommand.COMMAND, DESCRIPTION_HEADER));
}

@Override
public PluginCommand getCommandLineCommand() {
final PluginCommand pluginCommand =
newPluginCommand(ChatGPTCommand.COMMAND, "** " + DESCRIPTION_HEADER);
newPluginCommand(AiChatCommand.COMMAND, "** " + DESCRIPTION_HEADER);
pluginCommand
.addOption("api-key", String.class, "OpenAI API key")
.addOption(
"api-key:env", String.class, "OpenAI API key, from an environmental variable value")
.addOption(
"model", String.class, "ChatGPT model", "Optional, defaults to 'chatgpt-3.5-turbo'")
"model", String.class, "AI chat model", "Optional, defaults to 'gpt-4o-mini'")
.addOption(
"timeout",
Integer.class,
Expand All @@ -70,22 +70,22 @@ public PluginCommand getCommandLineCommand() {
.addOption(
"use-metadata",
Boolean.class,
"Allow sharing of database metadata with OpenAI to enhance chat responses",
"Allow sharing of database metadata with AI model to enhance chat responses",
"Optional, defaults to false");
return pluginCommand;
}

@Override
public ChatGPTCommand newSchemaCrawlerCommand(final String command, final Config config) {
if (!ChatGPTCommand.COMMAND.equals(command)) {
public AiChatCommand newSchemaCrawlerCommand(final String command, final Config config) {
if (!AiChatCommand.COMMAND.equals(command)) {
throw new IllegalArgumentException("Cannot support command, " + command);
}

try {
final ChatGPTCommandOptions options =
ChatGPTCommandOptionsBuilder.builder().fromConfig(config).toOptions();
final AiChatCommandOptions options =
AiChatCommandOptionsBuilder.builder().fromConfig(config).toOptions();

final ChatGPTCommand scCommand = new ChatGPTCommand();
final AiChatCommand scCommand = new AiChatCommand();
scCommand.setCommandOptions(options);
return scCommand;
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt;
package schemacrawler.tools.command.aichat;

import static schemacrawler.tools.command.chatgpt.utility.ChatGPTUtility.isExitCondition;
import static schemacrawler.tools.command.chatgpt.utility.ChatGPTUtility.printResponse;
import static schemacrawler.tools.command.aichat.utility.AiChatUtility.isExitCondition;
import static schemacrawler.tools.command.aichat.utility.AiChatUtility.printResponse;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -52,19 +52,19 @@
import io.github.sashirestela.openai.domain.chat.ChatRequest;
import io.github.sashirestela.openai.domain.chat.ChatRequest.Modality;
import schemacrawler.schema.Catalog;
import schemacrawler.tools.command.chatgpt.embeddings.QueryService;
import schemacrawler.tools.command.chatgpt.options.ChatGPTCommandOptions;
import schemacrawler.tools.command.chatgpt.utility.ChatGPTUtility;
import schemacrawler.tools.command.chatgpt.utility.ChatHistory;
import schemacrawler.tools.command.aichat.embeddings.QueryService;
import schemacrawler.tools.command.aichat.options.AiChatCommandOptions;
import schemacrawler.tools.command.aichat.utility.AiChatUtility;
import schemacrawler.tools.command.aichat.utility.ChatHistory;
import us.fatehi.utility.string.StringFormat;

public final class ChatGPTConsole implements AutoCloseable {
public final class AiChatConsole implements AutoCloseable {

private static final Logger LOGGER = Logger.getLogger(ChatGPTConsole.class.getCanonicalName());
private static final Logger LOGGER = Logger.getLogger(AiChatConsole.class.getCanonicalName());

private static final String PROMPT = String.format("%nPrompt: ");

private final ChatGPTCommandOptions commandOptions;
private final AiChatCommandOptions commandOptions;
private final FunctionExecutor functionExecutor;
private final SimpleOpenAI service;
private final QueryService queryService;
Expand All @@ -73,18 +73,18 @@ public final class ChatGPTConsole implements AutoCloseable {
private final Catalog catalog;
private final Connection connection;

public ChatGPTConsole(
final ChatGPTCommandOptions commandOptions,
public AiChatConsole(
final AiChatCommandOptions commandOptions,
final Catalog catalog,
final Connection connection) {

this.commandOptions = requireNonNull(commandOptions, "ChatGPT options not provided");
this.commandOptions = requireNonNull(commandOptions, "AI chat options not provided");
this.catalog = requireNonNull(catalog, "No catalog provided");
this.connection = requireNonNull(connection, "No connection provided");

functionExecutor = ChatGPTUtility.newFunctionExecutor();
functionExecutor = AiChatUtility.newFunctionExecutor();

service = SimpleOpenAI.builder().apiKey(System.getenv("OPENAI_API_KEY")).build();
service = SimpleOpenAI.builder().apiKey(commandOptions.getApiKey()).build();

queryService = new QueryService(service);
queryService.addTables(catalog.getTables());
Expand All @@ -96,7 +96,7 @@ public ChatGPTConsole(
@Override
public void close() {}

/** Simple REPL for the SchemaCrawler ChatGPT integration. */
/** Simple REPL for the SchemaCrawler AI chat integration. */
public void console() {
try (final Scanner scanner = new Scanner(System.in)) {
while (true) {
Expand All @@ -112,7 +112,7 @@ public void console() {
}

/**
* Send prompt to ChatGPT API and get completions.
* Send prompt to AI chat API and get completions.
*
* @param prompt Input prompt.
*/
Expand Down Expand Up @@ -155,7 +155,7 @@ private List<ChatMessage> complete(final String prompt) {
Level.INFO,
new StringFormat(
"Function call: %s(%s)", functionCall.getName(), functionCall.getArguments()));
final String returnString = ChatGPTUtility.execute(functionCall, catalog, connection);
final String returnString = AiChatUtility.execute(functionCall, catalog, connection);
completions.add(ToolMessage.of(returnString, toolCall.getId()));
// Add to chat history
chatHistory.add(ToolMessage.of(returnString, toolCall.getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt;
package schemacrawler.tools.command.aichat;

import static schemacrawler.tools.command.chatgpt.FunctionDefinition.FunctionType.USER;
import static schemacrawler.tools.command.aichat.FunctionDefinition.FunctionType.USER;
import us.fatehi.utility.property.PropertyName;

public interface FunctionDefinition<P extends FunctionParameters> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt;
package schemacrawler.tools.command.aichat;

import java.sql.Connection;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt;
package schemacrawler.tools.command.aichat;

import io.github.sashirestela.openai.common.function.Functional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt;
package schemacrawler.tools.command.aichat;

import java.util.function.Supplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import static java.util.Objects.requireNonNull;
import schemacrawler.schema.NamedObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import java.util.Collections;
import java.util.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import java.util.ArrayList;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import static java.util.Objects.requireNonNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import java.util.ArrayList;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.embeddings;
package schemacrawler.tools.command.aichat.embeddings;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.functions;
package schemacrawler.tools.command.aichat.functions;

import java.io.StringWriter;
import java.util.Collection;
Expand All @@ -36,9 +36,9 @@
import schemacrawler.schema.Catalog;
import schemacrawler.schema.Schema;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.tools.command.chatgpt.FunctionParameters;
import schemacrawler.tools.command.chatgpt.FunctionReturn;
import schemacrawler.tools.command.chatgpt.utility.ConnectionDatabaseConnectionSource;
import schemacrawler.tools.command.aichat.FunctionParameters;
import schemacrawler.tools.command.aichat.FunctionReturn;
import schemacrawler.tools.command.aichat.utility.ConnectionDatabaseConnectionSource;
import schemacrawler.tools.executable.SchemaCrawlerExecutable;
import schemacrawler.tools.options.Config;
import schemacrawler.tools.options.OutputOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.functions;
package schemacrawler.tools.command.aichat.functions;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.PropertyNamingStrategies.KebabCaseStrategy;
import schemacrawler.tools.command.chatgpt.FunctionDefinition;
import schemacrawler.tools.command.chatgpt.FunctionParameters;
import schemacrawler.tools.command.aichat.FunctionDefinition;
import schemacrawler.tools.command.aichat.FunctionParameters;

public abstract class AbstractFunctionDefinition<P extends FunctionParameters>
implements FunctionDefinition<P> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.functions;
package schemacrawler.tools.command.aichat.functions;

import java.sql.Connection;
import java.util.Objects;
Expand All @@ -35,8 +35,8 @@
import static java.util.Objects.requireNonNull;
import schemacrawler.schema.Catalog;
import schemacrawler.schemacrawler.exceptions.ExecutionRuntimeException;
import schemacrawler.tools.command.chatgpt.FunctionExecutor;
import schemacrawler.tools.command.chatgpt.FunctionParameters;
import schemacrawler.tools.command.aichat.FunctionExecutor;
import schemacrawler.tools.command.aichat.FunctionParameters;
import us.fatehi.utility.property.PropertyName;

public abstract class AbstractFunctionExecutor<P extends FunctionParameters>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
========================================================================
*/

package schemacrawler.tools.command.chatgpt.functions;
package schemacrawler.tools.command.aichat.functions;

public final class DatabaseObjectDescriptionFunctionDefinition
extends AbstractFunctionDefinition<DatabaseObjectDescriptionFunctionParameters> {
Expand Down
Loading
Loading