|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | +# |
| 4 | +# Licensed under the Universal Permissive License v 1.0 as shown at |
| 5 | +# http://oss.oracle.com/licenses/upl. |
| 6 | +# ----------------------------------------------------------------------------- |
| 7 | + |
| 8 | +""" |
| 9 | +3000 - Module for testing select_ai agents |
| 10 | +""" |
| 11 | + |
| 12 | +import uuid |
| 13 | + |
| 14 | +import pytest |
| 15 | +import select_ai |
| 16 | +from select_ai.agent import Tool |
| 17 | + |
| 18 | +PYSAI_3000_PROFILE_NAME = f"PYSAI_3000_{uuid.uuid4().hex.upper()}" |
| 19 | +PYSAI_3000_SQL_TOOL_NAME = f"PYSAI_3000_SQL_TOOL_{uuid.uuid4().hex.upper()}" |
| 20 | +PYSAI_3000_SQL_TOOL_DESCRIPTION = f"SQL Tool for Python 3000" |
| 21 | + |
| 22 | +PYSAI_3000_RAG_PROFILE_NAME = f"PYSAI_3000_RAG_{uuid.uuid4().hex.upper()}" |
| 23 | +PYSAI_3000_RAG_VECTOR_INDEX_NAME = ( |
| 24 | + f"PYSAI_3000_RAG_VECTOR_{uuid.uuid4().hex.upper()}" |
| 25 | +) |
| 26 | +PYSAI_3000_RAG_TOOL_NAME = f"PYSAI_3000_RAG_TOOL_{uuid.uuid4().hex.upper()}" |
| 27 | +PYSAI_3000_RAG_TOOL_DESCRIPTION = f"RAG Tool for Python 3000" |
| 28 | + |
| 29 | +PYSAI_3000_PL_SQL_TOOL_NAME = ( |
| 30 | + f"PYSAI_3000_PL_SQL_TOOL_{uuid.uuid4().hex.upper()}" |
| 31 | +) |
| 32 | +PYSAI_3000_PL_SQL_TOOL_DESCRIPTION = f"PL/SQL Tool for Python 3000" |
| 33 | +PYSAI_3000_PL_SQL_FUNC_NAME = ( |
| 34 | + f"PYSAI_3000_PL_SQL_FUNC_{uuid.uuid4().hex.upper()}" |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="module") |
| 39 | +def python_gen_ai_profile(profile_attributes): |
| 40 | + profile = select_ai.Profile( |
| 41 | + profile_name=PYSAI_3000_PROFILE_NAME, |
| 42 | + description="OCI GENAI Profile", |
| 43 | + attributes=profile_attributes, |
| 44 | + ) |
| 45 | + yield profile |
| 46 | + profile.delete(force=True) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture(scope="module") |
| 50 | +def python_gen_rag_ai_profile(rag_profile_attributes): |
| 51 | + profile = select_ai.Profile( |
| 52 | + profile_name=PYSAI_3000_RAG_PROFILE_NAME, |
| 53 | + description="OCI GENAI Profile", |
| 54 | + attributes=rag_profile_attributes, |
| 55 | + ) |
| 56 | + yield profile |
| 57 | + profile.delete(force=True) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture(scope="module") |
| 61 | +def sql_tool(python_gen_ai_profile): |
| 62 | + sql_tool = select_ai.agent.Tool.create_sql_tool( |
| 63 | + tool_name=PYSAI_3000_SQL_TOOL_NAME, |
| 64 | + description=PYSAI_3000_SQL_TOOL_DESCRIPTION, |
| 65 | + profile_name=PYSAI_3000_PROFILE_NAME, |
| 66 | + replace=True, |
| 67 | + ) |
| 68 | + yield sql_tool |
| 69 | + sql_tool.delete(force=True) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture(scope="module") |
| 73 | +def vector_index(vector_index_attributes, python_gen_rag_ai_profile): |
| 74 | + vector_index = select_ai.VectorIndex( |
| 75 | + index_name=PYSAI_3000_RAG_VECTOR_INDEX_NAME, |
| 76 | + attributes=vector_index_attributes, |
| 77 | + description="Test vector index", |
| 78 | + profile=python_gen_rag_ai_profile, |
| 79 | + ) |
| 80 | + vector_index.create(replace=True) |
| 81 | + yield vector_index |
| 82 | + vector_index.delete(force=True) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture(scope="module") |
| 86 | +def rag_tool(vector_index): |
| 87 | + sql_tool = select_ai.agent.Tool.create_rag_tool( |
| 88 | + tool_name=PYSAI_3000_RAG_TOOL_NAME, |
| 89 | + description=PYSAI_3000_RAG_TOOL_DESCRIPTION, |
| 90 | + profile_name=PYSAI_3000_RAG_PROFILE_NAME, |
| 91 | + replace=True, |
| 92 | + ) |
| 93 | + yield sql_tool |
| 94 | + sql_tool.delete(force=True) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.fixture(scope="module") |
| 98 | +def pl_sql_function(): |
| 99 | + create_function = f""" |
| 100 | + CREATE OR REPLACE FUNCTION {PYSAI_3000_PL_SQL_FUNC_NAME} (p_birth_date IN DATE) |
| 101 | + RETURN NUMBER |
| 102 | + IS |
| 103 | + v_age NUMBER; |
| 104 | + BEGIN |
| 105 | + -- Calculate the difference in years |
| 106 | + v_age := TRUNC(MONTHS_BETWEEN(SYSDATE, p_birth_date) / 12); |
| 107 | +
|
| 108 | + RETURN v_age; |
| 109 | + END CALCULATE_AGE; |
| 110 | + """ |
| 111 | + with select_ai.cursor() as cr: |
| 112 | + cr.execute(create_function) |
| 113 | + yield create_function |
| 114 | + with select_ai.cursor() as cr: |
| 115 | + cr.execute(f"DROP FUNCTION {PYSAI_3000_PL_SQL_FUNC_NAME}") |
| 116 | + |
| 117 | + |
| 118 | +@pytest.fixture(scope="module") |
| 119 | +def pl_sql_tool(pl_sql_function): |
| 120 | + pl_sql_tool = select_ai.agent.Tool.create_pl_sql_tool( |
| 121 | + tool_name=PYSAI_3000_PL_SQL_TOOL_NAME, |
| 122 | + function=PYSAI_3000_PL_SQL_FUNC_NAME, |
| 123 | + description=PYSAI_3000_PL_SQL_TOOL_DESCRIPTION, |
| 124 | + ) |
| 125 | + yield pl_sql_tool |
| 126 | + pl_sql_tool.delete(force=True) |
| 127 | + |
| 128 | + |
| 129 | +def test_3000(sql_tool): |
| 130 | + """test SQL tool creation and parameter validation""" |
| 131 | + assert ( |
| 132 | + sql_tool.attributes.tool_params.profile_name == PYSAI_3000_PROFILE_NAME |
| 133 | + ) |
| 134 | + assert sql_tool.tool_name == PYSAI_3000_SQL_TOOL_NAME |
| 135 | + assert sql_tool.description == PYSAI_3000_SQL_TOOL_DESCRIPTION |
| 136 | + assert isinstance( |
| 137 | + sql_tool.attributes.tool_params, select_ai.agent.SQLToolParams |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +def test_3001(rag_tool): |
| 142 | + """test RAG tool creation and parameter validation""" |
| 143 | + assert ( |
| 144 | + rag_tool.attributes.tool_params.profile_name |
| 145 | + == PYSAI_3000_RAG_PROFILE_NAME |
| 146 | + ) |
| 147 | + assert rag_tool.tool_name == PYSAI_3000_RAG_TOOL_NAME |
| 148 | + assert rag_tool.description == PYSAI_3000_RAG_TOOL_DESCRIPTION |
| 149 | + assert isinstance( |
| 150 | + rag_tool.attributes.tool_params, select_ai.agent.RAGToolParams |
| 151 | + ) |
| 152 | + |
| 153 | + |
| 154 | +def test_3002(pl_sql_tool): |
| 155 | + """test PL SQL tool creation and parameter validation""" |
| 156 | + assert pl_sql_tool.tool_name == PYSAI_3000_PL_SQL_TOOL_NAME |
| 157 | + assert pl_sql_tool.description == PYSAI_3000_PL_SQL_TOOL_DESCRIPTION |
| 158 | + assert pl_sql_tool.attributes.function == PYSAI_3000_PL_SQL_FUNC_NAME |
| 159 | + |
| 160 | + |
| 161 | +def test_3003(): |
| 162 | + """list tools""" |
| 163 | + tools = list(select_ai.agent.Tool.list()) |
| 164 | + tool_names = set(tool.tool_name for tool in tools) |
| 165 | + assert PYSAI_3000_RAG_TOOL_NAME in tool_names |
| 166 | + assert PYSAI_3000_SQL_TOOL_NAME in tool_names |
| 167 | + assert PYSAI_3000_PL_SQL_TOOL_NAME in tool_names |
| 168 | + |
| 169 | + |
| 170 | +def test_3004(): |
| 171 | + """list tools matching a REGEX pattern""" |
| 172 | + tools = list(select_ai.agent.Tool.list(tool_name_pattern="^PYSAI_3000")) |
| 173 | + tool_names = set(tool.tool_name for tool in tools) |
| 174 | + assert PYSAI_3000_RAG_TOOL_NAME in tool_names |
| 175 | + assert PYSAI_3000_SQL_TOOL_NAME in tool_names |
| 176 | + assert PYSAI_3000_PL_SQL_TOOL_NAME in tool_names |
| 177 | + |
| 178 | + |
| 179 | +def test_3005(): |
| 180 | + """fetch tool""" |
| 181 | + sql_tool = select_ai.agent.Tool.fetch(tool_name=PYSAI_3000_SQL_TOOL_NAME) |
| 182 | + assert sql_tool.tool_name == PYSAI_3000_SQL_TOOL_NAME |
| 183 | + assert sql_tool.description == PYSAI_3000_SQL_TOOL_DESCRIPTION |
| 184 | + assert isinstance( |
| 185 | + sql_tool.attributes.tool_params, select_ai.agent.SQLToolParams |
| 186 | + ) |
0 commit comments