Skip to content

Commit d5afe42

Browse files
Merge pull request #72 from oracle-samples/cdb-fix
fix spring-ai version
2 parents d31b36e + 5bef363 commit d5afe42

File tree

7 files changed

+120
-20
lines changed

7 files changed

+120
-20
lines changed

app/src/modules/metadata.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ def ll_models():
152152
"frequency_penalty": [0.0, 0.0, -1.0, 1.0],
153153
"presence_penalty": [0.0, 0.0, -2.0, 2.0],
154154
},
155+
"meta-llama/Llama-3.2-3B-Instruct": {
156+
"enabled": False,
157+
"api": "OpenAI",
158+
"url": "http://127.0.0.1:8080",
159+
"api_key": "",
160+
"openai_compat": True,
161+
"context_length": 127072,
162+
"temperature": [1.0, 1.0, 0.0, 2.0],
163+
"top_p": [0.99, .99, 0.0, 0.99],
164+
"max_tokens": [256, 256, 1, 8191],
165+
"frequency_penalty": [0.0, 0.0, -1.0, 1.0],
166+
"presence_penalty": [0.0, 0.0, -2.0, 2.0],
167+
},
155168
"gpt-4o": {
156169
"enabled": os.getenv("OPENAI_API_KEY") is not None,
157170
"api": "OpenAI",

app/src/modules/st_common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,15 @@ def get_yaml_obaas(session_state_json,provider):
334334

335335
def get_yaml_env(session_state_json,provider):
336336

337-
OLLAMA_MODEL="llama3.1"
337+
OLLAMA_MODEL=["llama3.1","llama3.2"]
338338

339339
instr_context=session_state_json["lm_instr_config"][session_state_json["lm_instr_prompt"]]["prompt"]
340340
vector_table,_= utilities.get_vs_table(session_state_json["rag_params"]["model"],session_state_json["rag_params"]["chunk_size"],session_state_json["rag_params"]["chunk_overlap"],session_state_json["rag_params"]["distance_metric"])
341341

342342
logger.info("ll_model selected: %s",session_state_json["ll_model"])
343-
logger.info(session_state_json["ll_model"] != OLLAMA_MODEL)
343+
logger.info(session_state_json["ll_model"] not in OLLAMA_MODEL)
344344

345-
if session_state_json["ll_model"] != OLLAMA_MODEL:
345+
if session_state_json["ll_model"] not in OLLAMA_MODEL:
346346
env_vars_LLM= f"""
347347
export OPENAI_CHAT_MODEL={session_state_json["ll_model"]}
348348
export OPENAI_EMBEDDING_MODEL={session_state_json["rag_params"]["model"]}
@@ -386,7 +386,7 @@ def get_yaml_env(session_state_json,provider):
386386
export DB_USERNAME=\"{session_state_json["db_config"]["user"]}\"
387387
export DB_PASSWORD=\"{session_state_json["db_config"]["password"]}\"
388388
export DISTANCE_TYPE={session_state_json["rag_params"]["distance_metric"]}
389-
export OLLAMA_BASE_URL=\"{session_state_json["ll_model_config"][OLLAMA_MODEL]["url"]}\"
389+
export OLLAMA_BASE_URL=\"{session_state_json["ll_model_config"][session_state_json["ll_model"]]["url"]}\"
390390
export CONTEXT_INSTR=\"{instr_context}\"
391391
export TOP_K={session_state_json.get("rag_params", {}).get("top_k",4)}
392392

app/src/modules/utilities.py

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,37 @@ def execute_sql(conn, run_sql):
216216
finally:
217217
cursor.close()
218218

219+
def check_index(conn, index):
220+
"""Execute SQL against Oracle Database to check the presence of an index"""
221+
222+
run_sql=f"SELECT index_name FROM all_indexes WHERE index_name = '{index}'"
223+
logger.info(run_sql)
224+
result = False
225+
try:
226+
cursor = conn.cursor()
227+
cursor.execute(run_sql)
228+
result = cursor.fetchone()
229+
logger.info("Check index via SQL Executed")
230+
logger.info(run_sql)
231+
return result
232+
233+
except oracledb.DatabaseError as ex:
234+
if ex.args and len(ex.args) > 0:
235+
error_obj = ex.args[0]
236+
if (
237+
# ORA-00955: name is already used by an existing object
238+
hasattr(error_obj, "code") and error_obj.code == 955
239+
):
240+
logger.info("Table Exists")
241+
else:
242+
logger.exception(ex, exc_info=False)
243+
raise
244+
finally:
245+
logger.info("check_index finally")
246+
logger.info(result)
247+
cursor.close()
248+
return result
249+
219250

220251
###############################################################################
221252
# Vector Storage
@@ -306,15 +337,41 @@ def json_to_doc(file: str):
306337
logger.info("Total Unique Chunks: %i", len(unique_chunks))
307338

308339
# Need to consider this, it duplicates from_documents
309-
logger.info("Dropping table %s", store_table)
310-
LangchainVS.drop_table_purge(db_conn, store_table)
340+
#logger.info("Dropping table %s", store_table)
341+
#LangchainVS.drop_table_purge(db_conn, store_table)
311342

312-
vectorstore = OracleVS(
343+
try:
344+
vectorstore_back = OracleVS(
345+
client=db_conn,
346+
embedding_function=model_name,
347+
table_name=store_table,
348+
distance_strategy=distance_metric,
349+
)
350+
except Exception as ex:
351+
logger.error("Unable to open existing vector table: %s", ex)
352+
353+
#check if index exists before drop
354+
index_exists= False
355+
try:
356+
index_exists=check_index(db_conn, f"{store_table}_HNSW_IDX")
357+
logger.info("Check in-memory index if exists")
358+
logger.info(index_exists)
359+
except Exception as ex:
360+
logger.error("Check in-memory index if exists: %s", ex)
361+
index_exists=True
362+
363+
LangchainVS.drop_index_if_exists(db_conn, f"{store_table}_HNSW_IDX")
364+
365+
366+
try:
367+
vectorstore = OracleVS(
313368
client=db_conn,
314369
embedding_function=model_name,
315-
table_name=store_table,
370+
table_name=store_table+"_TEMP",
316371
distance_strategy=distance_metric,
317-
)
372+
)
373+
except Exception as ex:
374+
logger.error("Unable to open temp vector table: %s", ex)
318375

319376
# Batch Size does not have a measurable impact on performance
320377
# but does eliminate issues with timeouts
@@ -328,23 +385,38 @@ def json_to_doc(file: str):
328385
len(unique_chunks),
329386
rate_limit,
330387
)
388+
logger.info(unique_chunks[i])
331389
OracleVS.add_documents(vectorstore, documents=batch)
332390
if rate_limit > 0:
333391
interval = 60 / rate_limit
334392
logger.info("Rate Limiting: sleeping for %i seconds", interval)
335393
time.sleep(interval)
336394

337-
# Build the Index
338-
logger.info("Creating index on: %s", store_table)
339-
try:
340-
params = {"idx_name": f"{store_table}_HNSW_IDX", "idx_type": "HNSW"}
341-
LangchainVS.create_index(db_conn, vectorstore, params)
342-
except Exception as ex:
343-
logger.error("Unable to create vector index: %s", ex)
395+
mergesql = f"INSERT INTO {store_table}(ID,TEXT,METADATA,EMBEDDING) SELECT ID, TEXT, METADATA, EMBEDDING FROM {store_table}_TEMP WHERE ID NOT IN (SELECT ID FROM {store_table} )"
396+
logger.info("MERGE with this command \n%s",mergesql)
397+
398+
execute_sql(db_conn, mergesql)
399+
db_conn.commit()
400+
401+
if (index_exists):
402+
# Build the Index
403+
logger.info("Creating index on: %s", store_table)
404+
try:
405+
params = {"idx_name": f"{store_table}_HNSW_IDX", "idx_type": "HNSW"}
406+
#params = {"idx_name": f"{store_table}_IVF_IDX", "idx_type": "IVF"}
407+
408+
LangchainVS.create_index(db_conn, vectorstore_back, params)
409+
410+
except Exception as ex:
411+
logger.error("Unable to create vector index: %s", ex)
344412

345413
# Comment the VS table
346414
comment = f"COMMENT ON TABLE {store_table} IS 'GENAI: {store_comment}'"
347415
execute_sql(db_conn, comment)
416+
417+
dropsql= f"DROP TABLE {store_table}_TEMP CASCADE CONSTRAINTS"
418+
execute_sql(db_conn, dropsql)
419+
348420
db_conn.close()
349421

350422

spring_ai/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
</parent>
1212
<groupId>com.example</groupId>
1313
<artifactId>myspringai</artifactId>
14-
<version>0.0.1-SNAPSHOT</version>
14+
<version>1.0.0-M4</version>
1515
<name>myspringai</name>
1616
<description>Simple AI Application using OpenAPI Service</description>
1717
<properties>
1818
<java.version>17</java.version>
19-
<spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
19+
<spring-ai.version>1.0.0-M4</spring-ai.version>
2020
</properties>
2121
<dependencyManagement>
2222
<dependencies>
2323
<dependency>
2424
<groupId>org.springframework.ai</groupId>
2525
<artifactId>spring-ai-bom</artifactId>
26-
<version>1.0.0-SNAPSHOT</version>
26+
<version>1.0.0-M4</version>
2727
<type>pom</type>
2828
<scope>import</scope>
2929
</dependency>
@@ -75,7 +75,7 @@
7575
<dependency>
7676
<groupId>org.springframework.ai</groupId>
7777
<artifactId>spring-ai-oracle-store-spring-boot-starter</artifactId>
78-
<version>1.0.0-SNAPSHOT</version>
78+
<version>1.0.0-M4</version>
7979
</dependency>
8080
<!--<dependency>
8181
<groupId>org.springframework.ai</groupId>

spring_ai/src/main/java/org/springframework/ai/openai/samples/helloworld/AIController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
Copyright (c) 2023, 2024, Oracle and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
*/
5+
16
package org.springframework.ai.openai.samples.helloworld;
27

38
import org.springframework.ai.chat.client.ChatClient;

spring_ai/src/main/java/org/springframework/ai/openai/samples/helloworld/Application.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
Copyright (c) 2023, 2024, Oracle and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
*/
5+
16
package org.springframework.ai.openai.samples.helloworld;
27

38
import org.springframework.ai.embedding.EmbeddingModel;

spring_ai/src/main/java/org/springframework/ai/openai/samples/helloworld/Config.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
Copyright (c) 2023, 2024, Oracle and/or its affiliates.
3+
Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl.
4+
*/
5+
16
package org.springframework.ai.openai.samples.helloworld;
27

38
import org.springframework.ai.chat.client.ChatClient;

0 commit comments

Comments
 (0)