-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSelectAILocalTest.java
More file actions
157 lines (140 loc) · 6.82 KB
/
Copy pathSelectAILocalTest.java
File metadata and controls
157 lines (140 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.example;
import com.oracle.bmc.ConfigFileReader;
import oracle.jdbc.pool.OracleDataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.oracle.OracleContainer;
import org.testcontainers.utility.MountableFile;
import javax.sql.DataSource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.time.Duration;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@Testcontainers
@EnabledIfEnvironmentVariable(named = "OCI_COMPARTMENT_ID", matches = ".+")
public class SelectAILocalTest {
private final static String CERTS_FILE = "https://objectstorage.us-phoenix-1.oraclecloud.com/p/KB63IAuDCGhz_azOVQ07Qa_mxL3bGrFh1dtsltreRJPbmb-VwsH2aQ4Pur2ADBMA/n/adwcdemo/b/CERTS/o/dbc_certs.tar";
private static final String WALLET_PASSWORD = "MyWalletPassword12345";
private final static String SYS_PASSWORD = "Welcome12345";
private static OracleDataSource ds;
/**
* The "full" image is required to run catcon.pl inside the container.
*/
static OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-free:23.26.3-full-faststart")
.withStartupTimeout(Duration.ofMinutes(5))
.withUsername("UNI")
.withPassword("StudentsSchemaPassword12345")
.withInitScript("students.sql")
.withEnv(Map.of("ORACLE_PASSWORD", SYS_PASSWORD,
"WALLET_PASSWORD", WALLET_PASSWORD,
"CERTS_FILE", CERTS_FILE));
@BeforeAll
static void setup() throws Exception {
oracleContainer.start();
System.out.println("Installing certificates and DBMS_CLOUD family of PL/SQL packages...");
// Download certificates, create the database wallet, and install DBMS_CLOUD.
oracleContainer.copyFileToContainer(MountableFile.forClasspathResource("selectai/init.sh"), "/tmp/init.sh");
var initResult = oracleContainer.execInContainer("bash", "/tmp/init.sh");
assertThat(initResult.getExitCode())
.withFailMessage(initResult.getStdout())
.isZero();
System.out.println("Configuring Oracle AI Database for outbound HTTPS connections (ACES)...");
oracleContainer.copyFileToContainer(MountableFile.forClasspathResource("selectai/dbms_cloud_aces.sql"), "/tmp/dbms_cloud_aces.sql");
var acesResult = oracleContainer.execInContainer("sqlplus", "sys / as sysdba", "@/tmp/dbms_cloud_aces.sql");
assertThat(acesResult.getExitCode())
.withFailMessage(acesResult.getStdout())
.isZero();
System.out.println("Configuring grants...");
// Initialize the testuser for DBMS_CLOUD (applying grants)
oracleContainer.copyFileToContainer(MountableFile.forClasspathResource("selectai/dbms_cloud_grants.sql"), "/tmp/init.sql");
var grantsResult = oracleContainer.execInContainer("sqlplus", "sys / as sysdba", "@/tmp/init.sql");
assertThat(grantsResult.getExitCode())
.withFailMessage(grantsResult.getStdout())
.isZero();
// Configure a test datasource
ds = new OracleDataSource();
ds.setURL(oracleContainer.getJdbcUrl());
ds.setUser("selectai");
ds.setPassword("Welcome12345");
System.out.println("Creating DBMS_CLOUD profile for Select AI...");
createGenAiProfile();
}
@Test
public void selectAIRunsLocally() {
System.out.println("Generating SQL using 'select ai showsql'...");
System.out.println("Generated SQL on UNI schema: \n" +
selectai(ds,
"what are the available courses and where are they held?",
"showsql"));
}
private String selectai(DataSource ds, String prompt, String action) {
try (Connection conn = ds.getConnection()) {
String sql = """
BEGIN
? := DBMS_CLOUD_AI.GENERATE(
prompt => ?,
action => ?,
profile_name => ?);
END;
""";
try (CallableStatement statement = conn.prepareCall(sql)) {
statement.registerOutParameter(1, Types.CLOB);
statement.setString(2, prompt);
statement.setString(3, action);
statement.setString(4, "MY_PROFILE");
statement.execute();
return statement.getString(1);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private static void createGenAiProfile() throws IOException, SQLException {
ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
String privateKey = Files.readString(Path.of(configFile.get("key_file")));
final String profileSQL = """
BEGIN
DBMS_CLOUD.CREATE_CREDENTIAL(
credential_name => 'GENAI_CRED',
user_ocid => ?,
tenancy_ocid => ?,
private_key => ?,
fingerprint => ?
);
DBMS_CLOUD_AI.CREATE_PROFILE(
profile_name => 'MY_PROFILE',
attributes => '{
"provider": "oci",
"credential_name": "GENAI_CRED",
"region": "us-chicago-1",
"oci_compartment_id": "%s",
"object_list": [
{ "owner": "UNI", "name": "STUDENTS" },
{ "owner": "UNI", "name": "COURSES" },
{ "owner": "UNI", "name": "LECTURE_HALLS" },
{ "owner": "UNI", "name": "ENROLLMENTS" }
],
"enforce_object_list": true
}'
);
END;
""".formatted(System.getenv("OCI_COMPARTMENT_ID"));
try (var connection = ds.getConnection();
var statement = connection.prepareStatement(profileSQL)) {
statement.setString(1, configFile.get("user"));
statement.setString(2, configFile.get("tenancy"));
statement.setString(3, privateKey);
statement.setString(4, configFile.get("fingerprint"));
statement.execute();
}
}
}