|
| 1 | +""" |
| 2 | +Integration tests against a live RavenDB 7.2.x server for the AI agent |
| 3 | +configuration fields added in 7.2.3: |
| 4 | + * AiAgentConfiguration.sub_agents (+ AiAgentToolSubAgent) |
| 5 | + * AiAgentParameter.policy (AiAgentParameterPolicy) |
| 6 | + * AiAgentParameter.type (AiAgentParameterValueType) |
| 7 | +
|
| 8 | +The license guard mirrors the existing AI agent tests in this directory. |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import unittest |
| 13 | + |
| 14 | +from ravendb import ( |
| 15 | + AiAgentConfiguration, |
| 16 | + AiAgentParameter, |
| 17 | + AiAgentParameterPolicy, |
| 18 | + AiAgentParameterValueType, |
| 19 | + AiAgentToolSubAgent, |
| 20 | +) |
| 21 | +from ravendb.documents.operations.ai import AiConnectionString, AiModelType |
| 22 | +from ravendb.documents.operations.ai.agents import ( |
| 23 | + AddOrUpdateAiAgentOperation, |
| 24 | + DeleteAiAgentOperation, |
| 25 | +) |
| 26 | +from ravendb.documents.operations.ai.open_ai_settings import OpenAiSettings |
| 27 | +from ravendb.documents.operations.connection_string.put_connection_string_operation import PutConnectionStringOperation |
| 28 | +from ravendb.tests.test_base import TestBase |
| 29 | + |
| 30 | + |
| 31 | +@unittest.skipIf(os.environ.get("RAVENDB_LICENSE") is None, "Insufficient license permissions. Skipping on CI/CD.") |
| 32 | +class TestAiAgentConfigExtensionsIntegration(TestBase): |
| 33 | + CONNECTION_STRING_NAME = "test-ai-agent-cs-ext" |
| 34 | + |
| 35 | + def setUp(self): |
| 36 | + super().setUp() |
| 37 | + ai_connection_string = AiConnectionString( |
| 38 | + name=self.CONNECTION_STRING_NAME, |
| 39 | + identifier=self.CONNECTION_STRING_NAME, |
| 40 | + model_type=AiModelType.CHAT, |
| 41 | + openai_settings=OpenAiSettings( |
| 42 | + api_key="dummy-api-key", |
| 43 | + endpoint="https://api.openai.com/v1", |
| 44 | + model="gpt-4", |
| 45 | + ), |
| 46 | + ) |
| 47 | + self.store.maintenance.send(PutConnectionStringOperation(ai_connection_string)) |
| 48 | + self._created_agent_ids = [] |
| 49 | + |
| 50 | + def tearDown(self): |
| 51 | + for agent_id in self._created_agent_ids: |
| 52 | + try: |
| 53 | + self.store.maintenance.send(DeleteAiAgentOperation(agent_id)) |
| 54 | + except Exception: |
| 55 | + pass |
| 56 | + super().tearDown() |
| 57 | + |
| 58 | + # ---- sub_agents ---- |
| 59 | + |
| 60 | + def test_sub_agents_round_trip_through_get_agent(self): |
| 61 | + agent = AiAgentConfiguration( |
| 62 | + name="ParentAgent", |
| 63 | + identifier="test-sub-agent-parent", |
| 64 | + connection_string_name=self.CONNECTION_STRING_NAME, |
| 65 | + system_prompt="Dispatch to sub-agents as needed.", |
| 66 | + sample_object='{"answer": "..."}', |
| 67 | + sub_agents=[ |
| 68 | + AiAgentToolSubAgent(identifier="benefits-agent", description="Handles benefit questions"), |
| 69 | + AiAgentToolSubAgent(identifier="attendance-agent", description="Tracks PTO and attendance"), |
| 70 | + ], |
| 71 | + ) |
| 72 | + result = self.store.ai.add_or_update_agent(agent) |
| 73 | + self._created_agent_ids.append(result.identifier) |
| 74 | + |
| 75 | + fetched = self.store.ai.get_agents(result.identifier).ai_agents[0] |
| 76 | + identifiers = sorted(s.identifier for s in fetched.sub_agents) |
| 77 | + self.assertEqual(["attendance-agent", "benefits-agent"], identifiers) |
| 78 | + |
| 79 | + descriptions = {s.identifier: s.description for s in fetched.sub_agents} |
| 80 | + self.assertEqual("Handles benefit questions", descriptions["benefits-agent"]) |
| 81 | + self.assertEqual("Tracks PTO and attendance", descriptions["attendance-agent"]) |
| 82 | + |
| 83 | + # ---- AiAgentParameter.policy ---- |
| 84 | + |
| 85 | + def test_parameter_policy_forbid_model_generation_round_trips(self): |
| 86 | + agent = AiAgentConfiguration( |
| 87 | + name="ParamPolicyAgent", |
| 88 | + identifier="test-param-policy", |
| 89 | + connection_string_name=self.CONNECTION_STRING_NAME, |
| 90 | + system_prompt="Test parameter policy.", |
| 91 | + sample_object='{"answer": "..."}', |
| 92 | + parameters=[ |
| 93 | + AiAgentParameter( |
| 94 | + name="user_id", |
| 95 | + description="Hidden user id", |
| 96 | + send_to_model=False, |
| 97 | + policy=AiAgentParameterPolicy.FORBID_MODEL_GENERATION, |
| 98 | + ), |
| 99 | + AiAgentParameter(name="country", description="The country to filter by."), |
| 100 | + ], |
| 101 | + ) |
| 102 | + result = self.store.ai.add_or_update_agent(agent) |
| 103 | + self._created_agent_ids.append(result.identifier) |
| 104 | + |
| 105 | + fetched = self.store.ai.get_agents(result.identifier).ai_agents[0] |
| 106 | + by_name = {p.name: p for p in fetched.parameters} |
| 107 | + self.assertEqual(AiAgentParameterPolicy.FORBID_MODEL_GENERATION, by_name["user_id"].policy) |
| 108 | + # Default-valued parameter still comes back with the default policy. |
| 109 | + self.assertEqual(AiAgentParameterPolicy.DEFAULT, by_name["country"].policy) |
| 110 | + |
| 111 | + # ---- AiAgentParameter.type ---- |
| 112 | + |
| 113 | + def test_parameter_value_type_round_trips(self): |
| 114 | + agent = AiAgentConfiguration( |
| 115 | + name="ParamTypeAgent", |
| 116 | + identifier="test-param-type", |
| 117 | + connection_string_name=self.CONNECTION_STRING_NAME, |
| 118 | + system_prompt="Test parameter types.", |
| 119 | + sample_object='{"answer": "..."}', |
| 120 | + parameters=[ |
| 121 | + AiAgentParameter(name="email", description="Email", type=AiAgentParameterValueType.STRING), |
| 122 | + AiAgentParameter(name="age", description="Age", type=AiAgentParameterValueType.NUMBER), |
| 123 | + AiAgentParameter(name="tags", description="Tags", type=AiAgentParameterValueType.ARRAY_OF_STRING), |
| 124 | + ], |
| 125 | + ) |
| 126 | + result = self.store.ai.add_or_update_agent(agent) |
| 127 | + self._created_agent_ids.append(result.identifier) |
| 128 | + |
| 129 | + fetched = self.store.ai.get_agents(result.identifier).ai_agents[0] |
| 130 | + by_name = {p.name: p for p in fetched.parameters} |
| 131 | + self.assertEqual(AiAgentParameterValueType.STRING, by_name["email"].type) |
| 132 | + self.assertEqual(AiAgentParameterValueType.NUMBER, by_name["age"].type) |
| 133 | + self.assertEqual(AiAgentParameterValueType.ARRAY_OF_STRING, by_name["tags"].type) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + unittest.main() |
0 commit comments