vectara-agentic 0.3.2__py3-none-any.whl → 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of vectara-agentic might be problematic. Click here for more details.
- tests/__init__.py +7 -0
- tests/conftest.py +312 -0
- tests/endpoint.py +54 -17
- tests/run_tests.py +111 -0
- tests/test_agent.py +10 -5
- tests/test_agent_type.py +82 -143
- tests/test_api_endpoint.py +4 -0
- tests/test_bedrock.py +4 -0
- tests/test_fallback.py +4 -0
- tests/test_gemini.py +28 -45
- tests/test_groq.py +4 -0
- tests/test_private_llm.py +11 -2
- tests/test_return_direct.py +6 -2
- tests/test_serialization.py +4 -0
- tests/test_streaming.py +88 -0
- tests/test_tools.py +10 -82
- tests/test_vectara_llms.py +4 -0
- tests/test_vhc.py +66 -0
- tests/test_workflow.py +4 -0
- vectara_agentic/__init__.py +27 -4
- vectara_agentic/_callback.py +65 -67
- vectara_agentic/_observability.py +30 -30
- vectara_agentic/_version.py +1 -1
- vectara_agentic/agent.py +375 -848
- vectara_agentic/agent_config.py +15 -14
- vectara_agentic/agent_core/__init__.py +22 -0
- vectara_agentic/agent_core/factory.py +501 -0
- vectara_agentic/{_prompts.py → agent_core/prompts.py} +3 -35
- vectara_agentic/agent_core/serialization.py +345 -0
- vectara_agentic/agent_core/streaming.py +495 -0
- vectara_agentic/agent_core/utils/__init__.py +34 -0
- vectara_agentic/agent_core/utils/hallucination.py +202 -0
- vectara_agentic/agent_core/utils/logging.py +52 -0
- vectara_agentic/agent_core/utils/prompt_formatting.py +56 -0
- vectara_agentic/agent_core/utils/schemas.py +87 -0
- vectara_agentic/agent_core/utils/tools.py +125 -0
- vectara_agentic/agent_endpoint.py +4 -6
- vectara_agentic/db_tools.py +37 -12
- vectara_agentic/llm_utils.py +41 -42
- vectara_agentic/sub_query_workflow.py +9 -14
- vectara_agentic/tool_utils.py +138 -83
- vectara_agentic/tools.py +43 -21
- vectara_agentic/tools_catalog.py +16 -16
- vectara_agentic/types.py +98 -6
- {vectara_agentic-0.3.2.dist-info → vectara_agentic-0.4.0.dist-info}/METADATA +69 -30
- vectara_agentic-0.4.0.dist-info/RECORD +50 -0
- tests/test_agent_planning.py +0 -64
- tests/test_hhem.py +0 -100
- vectara_agentic/hhem.py +0 -82
- vectara_agentic-0.3.2.dist-info/RECORD +0 -39
- {vectara_agentic-0.3.2.dist-info → vectara_agentic-0.4.0.dist-info}/WHEEL +0 -0
- {vectara_agentic-0.3.2.dist-info → vectara_agentic-0.4.0.dist-info}/licenses/LICENSE +0 -0
- {vectara_agentic-0.3.2.dist-info → vectara_agentic-0.4.0.dist-info}/top_level.txt +0 -0
vectara_agentic/agent_config.py
CHANGED
|
@@ -6,6 +6,7 @@ import os
|
|
|
6
6
|
from dataclasses import dataclass, field
|
|
7
7
|
from .types import ModelProvider, AgentType, ObserverType
|
|
8
8
|
|
|
9
|
+
|
|
9
10
|
@dataclass(eq=True, frozen=True)
|
|
10
11
|
class AgentConfig:
|
|
11
12
|
"""
|
|
@@ -19,7 +20,7 @@ class AgentConfig:
|
|
|
19
20
|
# Agent type
|
|
20
21
|
agent_type: AgentType = field(
|
|
21
22
|
default_factory=lambda: AgentType(
|
|
22
|
-
os.getenv("VECTARA_AGENTIC_AGENT_TYPE", AgentType.
|
|
23
|
+
os.getenv("VECTARA_AGENTIC_AGENT_TYPE", AgentType.FUNCTION_CALLING.value)
|
|
23
24
|
)
|
|
24
25
|
)
|
|
25
26
|
|
|
@@ -46,11 +47,15 @@ class AgentConfig:
|
|
|
46
47
|
|
|
47
48
|
# Params for Private LLM endpoint if used
|
|
48
49
|
private_llm_api_base: str = field(
|
|
49
|
-
default_factory=lambda: os.getenv(
|
|
50
|
-
|
|
50
|
+
default_factory=lambda: os.getenv(
|
|
51
|
+
"VECTARA_AGENTIC_PRIVATE_LLM_API_BASE",
|
|
52
|
+
"http://private-endpoint.company.com:5000/v1",
|
|
53
|
+
)
|
|
51
54
|
)
|
|
52
55
|
private_llm_api_key: str = field(
|
|
53
|
-
default_factory=lambda: os.getenv(
|
|
56
|
+
default_factory=lambda: os.getenv(
|
|
57
|
+
"VECTARA_AGENTIC_PRIVATE_LLM_API_KEY", "<private-api-key>"
|
|
58
|
+
)
|
|
54
59
|
)
|
|
55
60
|
|
|
56
61
|
# Observer
|
|
@@ -65,20 +70,18 @@ class AgentConfig:
|
|
|
65
70
|
default_factory=lambda: os.getenv("VECTARA_AGENTIC_API_KEY", "dev-api-key")
|
|
66
71
|
)
|
|
67
72
|
|
|
68
|
-
# max reasoning steps
|
|
69
|
-
# used for both OpenAI and React Agent types
|
|
70
|
-
max_reasoning_steps: int = field(
|
|
71
|
-
default_factory=lambda: int(os.getenv("VECTARA_AGENTIC_MAX_REASONING_STEPS", "50"))
|
|
72
|
-
)
|
|
73
|
-
|
|
74
73
|
def __post_init__(self):
|
|
75
74
|
# Use object.__setattr__ since the dataclass is frozen
|
|
76
75
|
if isinstance(self.agent_type, str):
|
|
77
76
|
object.__setattr__(self, "agent_type", AgentType(self.agent_type))
|
|
78
77
|
if isinstance(self.main_llm_provider, str):
|
|
79
|
-
object.__setattr__(
|
|
78
|
+
object.__setattr__(
|
|
79
|
+
self, "main_llm_provider", ModelProvider(self.main_llm_provider)
|
|
80
|
+
)
|
|
80
81
|
if isinstance(self.tool_llm_provider, str):
|
|
81
|
-
object.__setattr__(
|
|
82
|
+
object.__setattr__(
|
|
83
|
+
self, "tool_llm_provider", ModelProvider(self.tool_llm_provider)
|
|
84
|
+
)
|
|
82
85
|
if isinstance(self.observer, str):
|
|
83
86
|
object.__setattr__(self, "observer", ObserverType(self.observer))
|
|
84
87
|
|
|
@@ -94,7 +97,6 @@ class AgentConfig:
|
|
|
94
97
|
"tool_llm_model_name": self.tool_llm_model_name,
|
|
95
98
|
"observer": self.observer.value,
|
|
96
99
|
"endpoint_api_key": self.endpoint_api_key,
|
|
97
|
-
"max_reasoning_steps": self.max_reasoning_steps
|
|
98
100
|
}
|
|
99
101
|
|
|
100
102
|
@classmethod
|
|
@@ -110,5 +112,4 @@ class AgentConfig:
|
|
|
110
112
|
tool_llm_model_name=config_dict["tool_llm_model_name"],
|
|
111
113
|
observer=ObserverType(config_dict["observer"]),
|
|
112
114
|
endpoint_api_key=config_dict["endpoint_api_key"],
|
|
113
|
-
max_reasoning_steps=config_dict["max_reasoning_steps"]
|
|
114
115
|
)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Agent core module containing essential components for agent functionality.
|
|
3
|
+
|
|
4
|
+
This module organizes core agent functionality into focused components:
|
|
5
|
+
- factory: Agent creation and configuration
|
|
6
|
+
- streaming: Streaming response handling and adapters
|
|
7
|
+
- serialization: Agent persistence and restoration
|
|
8
|
+
- evaluation: Response evaluation and post-processing
|
|
9
|
+
- prompts: Core prompt templates and instructions
|
|
10
|
+
- utils: Shared utilities for prompts, schemas, tools, and logging
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
# Import main utilities that should be available at agent module level
|
|
14
|
+
from .streaming import (
|
|
15
|
+
StreamingResponseAdapter,
|
|
16
|
+
FunctionCallingStreamHandler,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"StreamingResponseAdapter",
|
|
21
|
+
"FunctionCallingStreamHandler",
|
|
22
|
+
]
|
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Agent factory functions for creating different types of agents.
|
|
3
|
+
|
|
4
|
+
This module provides specialized functions for creating various agent types
|
|
5
|
+
with proper configuration, prompt formatting, and structured planning setup.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
import warnings
|
|
11
|
+
from typing import List, Union, Optional, Dict, Any
|
|
12
|
+
|
|
13
|
+
from llama_index.core.tools import FunctionTool
|
|
14
|
+
from llama_index.core.memory import Memory
|
|
15
|
+
from llama_index.core.callbacks import CallbackManager
|
|
16
|
+
from llama_index.core.agent.workflow import FunctionAgent, ReActAgent
|
|
17
|
+
from llama_index.core.agent.react.formatter import ReActChatFormatter
|
|
18
|
+
from llama_index.core.agent.runner.base import AgentRunner
|
|
19
|
+
from llama_index.core.agent.types import BaseAgent
|
|
20
|
+
|
|
21
|
+
with warnings.catch_warnings():
|
|
22
|
+
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
23
|
+
from llama_index.agent.llm_compiler import LLMCompilerAgentWorker
|
|
24
|
+
from llama_index.agent.lats import LATSAgentWorker
|
|
25
|
+
from pydantic import Field, create_model
|
|
26
|
+
|
|
27
|
+
from ..agent_config import AgentConfig
|
|
28
|
+
from ..types import AgentType
|
|
29
|
+
from .prompts import (
|
|
30
|
+
REACT_PROMPT_TEMPLATE,
|
|
31
|
+
GENERAL_PROMPT_TEMPLATE,
|
|
32
|
+
GENERAL_INSTRUCTIONS,
|
|
33
|
+
)
|
|
34
|
+
from ..tools import VectaraToolFactory
|
|
35
|
+
from .utils.prompt_formatting import format_prompt, format_llm_compiler_prompt
|
|
36
|
+
from .utils.schemas import PY_TYPES
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def create_react_agent(
|
|
40
|
+
tools: List[FunctionTool],
|
|
41
|
+
llm,
|
|
42
|
+
memory: Memory,
|
|
43
|
+
config: AgentConfig,
|
|
44
|
+
callback_manager: CallbackManager,
|
|
45
|
+
general_instructions: str,
|
|
46
|
+
topic: str,
|
|
47
|
+
custom_instructions: str,
|
|
48
|
+
verbose: bool = True,
|
|
49
|
+
) -> ReActAgent:
|
|
50
|
+
"""
|
|
51
|
+
Create a ReAct (Reasoning and Acting) agent.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
tools: List of tools available to the agent
|
|
55
|
+
llm: Language model instance
|
|
56
|
+
memory: Agent memory
|
|
57
|
+
config: Agent configuration
|
|
58
|
+
callback_manager: Callback manager for events
|
|
59
|
+
general_instructions: General instructions for the agent
|
|
60
|
+
topic: Topic expertise area
|
|
61
|
+
custom_instructions: Custom user instructions
|
|
62
|
+
verbose: Whether to enable verbose output
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
ReActAgent: Configured ReAct agent
|
|
66
|
+
"""
|
|
67
|
+
prompt = format_prompt(
|
|
68
|
+
REACT_PROMPT_TEMPLATE,
|
|
69
|
+
general_instructions,
|
|
70
|
+
topic,
|
|
71
|
+
custom_instructions,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Create ReActAgent with correct parameters based on current LlamaIndex API
|
|
75
|
+
# Note: ReActAgent is a workflow-based agent and doesn't have from_tools method
|
|
76
|
+
return ReActAgent(
|
|
77
|
+
tools=tools,
|
|
78
|
+
llm=llm,
|
|
79
|
+
system_prompt=prompt,
|
|
80
|
+
verbose=verbose,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def create_function_agent(
|
|
85
|
+
tools: List[FunctionTool],
|
|
86
|
+
llm,
|
|
87
|
+
memory: Memory,
|
|
88
|
+
config: AgentConfig,
|
|
89
|
+
callback_manager: CallbackManager,
|
|
90
|
+
general_instructions: str,
|
|
91
|
+
topic: str,
|
|
92
|
+
custom_instructions: str,
|
|
93
|
+
verbose: bool = True,
|
|
94
|
+
enable_parallel_tool_calls: bool = False,
|
|
95
|
+
) -> FunctionAgent:
|
|
96
|
+
"""
|
|
97
|
+
Create a unified Function Calling agent.
|
|
98
|
+
|
|
99
|
+
This replaces both the deprecated OpenAI agent and the dedicated function calling agent,
|
|
100
|
+
providing a single modern implementation with flexible capabilities.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
tools: List of tools available to the agent
|
|
104
|
+
llm: Language model instance
|
|
105
|
+
memory: Agent memory (maintained via Context during agent execution)
|
|
106
|
+
config: Agent configuration
|
|
107
|
+
callback_manager: Callback manager for events (not directly supported by FunctionAgent)
|
|
108
|
+
general_instructions: General instructions for the agent
|
|
109
|
+
topic: Topic expertise area
|
|
110
|
+
custom_instructions: Custom user instructions
|
|
111
|
+
verbose: Whether to enable verbose output
|
|
112
|
+
enable_parallel_tool_calls: Whether to enable parallel tool execution
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
FunctionAgent: Configured Function Calling agent
|
|
116
|
+
|
|
117
|
+
Notes:
|
|
118
|
+
- Works with any LLM provider (OpenAI, Anthropic, Together, etc.)
|
|
119
|
+
- Memory/state is managed via Context object during workflow execution
|
|
120
|
+
- Parallel tool calls depend on LLM provider support
|
|
121
|
+
- Replaces both OpenAI agent (legacy) and function calling agent implementations
|
|
122
|
+
"""
|
|
123
|
+
prompt = format_prompt(
|
|
124
|
+
GENERAL_PROMPT_TEMPLATE,
|
|
125
|
+
general_instructions,
|
|
126
|
+
topic,
|
|
127
|
+
custom_instructions,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
# Create FunctionAgent with correct parameters based on current LlamaIndex API
|
|
131
|
+
# Note: FunctionAgent is a workflow-based agent and doesn't have from_tools method
|
|
132
|
+
return FunctionAgent(
|
|
133
|
+
tools=tools,
|
|
134
|
+
llm=llm,
|
|
135
|
+
system_prompt=prompt,
|
|
136
|
+
verbose=verbose,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def create_llmcompiler_agent(
|
|
141
|
+
tools: List[FunctionTool],
|
|
142
|
+
llm,
|
|
143
|
+
memory: Memory,
|
|
144
|
+
config: AgentConfig,
|
|
145
|
+
callback_manager: CallbackManager,
|
|
146
|
+
general_instructions: str,
|
|
147
|
+
topic: str,
|
|
148
|
+
custom_instructions: str,
|
|
149
|
+
verbose: bool = True,
|
|
150
|
+
) -> AgentRunner:
|
|
151
|
+
"""
|
|
152
|
+
Create an LLM Compiler agent.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
tools: List of tools available to the agent
|
|
156
|
+
llm: Language model instance
|
|
157
|
+
memory: Agent memory
|
|
158
|
+
config: Agent configuration
|
|
159
|
+
callback_manager: Callback manager for events
|
|
160
|
+
general_instructions: General instructions for the agent
|
|
161
|
+
topic: Topic expertise area
|
|
162
|
+
custom_instructions: Custom user instructions
|
|
163
|
+
verbose: Whether to enable verbose output
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
AgentRunner: Configured LLM Compiler agent
|
|
167
|
+
"""
|
|
168
|
+
agent_worker = LLMCompilerAgentWorker.from_tools(
|
|
169
|
+
tools=tools,
|
|
170
|
+
llm=llm,
|
|
171
|
+
verbose=verbose,
|
|
172
|
+
callback_manager=callback_manager,
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
# Format main system prompt
|
|
176
|
+
agent_worker.system_prompt = format_prompt(
|
|
177
|
+
prompt_template=format_llm_compiler_prompt(
|
|
178
|
+
prompt=agent_worker.system_prompt,
|
|
179
|
+
general_instructions=general_instructions,
|
|
180
|
+
topic=topic,
|
|
181
|
+
custom_instructions=custom_instructions,
|
|
182
|
+
),
|
|
183
|
+
general_instructions=general_instructions,
|
|
184
|
+
topic=topic,
|
|
185
|
+
custom_instructions=custom_instructions,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
# Format replan prompt
|
|
189
|
+
agent_worker.system_prompt_replan = format_prompt(
|
|
190
|
+
prompt_template=format_llm_compiler_prompt(
|
|
191
|
+
prompt=agent_worker.system_prompt_replan,
|
|
192
|
+
general_instructions=GENERAL_INSTRUCTIONS,
|
|
193
|
+
topic=topic,
|
|
194
|
+
custom_instructions=custom_instructions,
|
|
195
|
+
),
|
|
196
|
+
general_instructions=GENERAL_INSTRUCTIONS,
|
|
197
|
+
topic=topic,
|
|
198
|
+
custom_instructions=custom_instructions,
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
return agent_worker.as_agent()
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def create_lats_agent(
|
|
205
|
+
tools: List[FunctionTool],
|
|
206
|
+
llm,
|
|
207
|
+
memory: Memory,
|
|
208
|
+
config: AgentConfig,
|
|
209
|
+
callback_manager: CallbackManager,
|
|
210
|
+
general_instructions: str,
|
|
211
|
+
topic: str,
|
|
212
|
+
custom_instructions: str,
|
|
213
|
+
verbose: bool = True,
|
|
214
|
+
) -> AgentRunner:
|
|
215
|
+
"""
|
|
216
|
+
Create a LATS (Language Agent Tree Search) agent.
|
|
217
|
+
|
|
218
|
+
Args:
|
|
219
|
+
tools: List of tools available to the agent
|
|
220
|
+
llm: Language model instance
|
|
221
|
+
memory: Agent memory
|
|
222
|
+
config: Agent configuration
|
|
223
|
+
callback_manager: Callback manager for events
|
|
224
|
+
general_instructions: General instructions for the agent
|
|
225
|
+
topic: Topic expertise area
|
|
226
|
+
custom_instructions: Custom user instructions
|
|
227
|
+
verbose: Whether to enable verbose output
|
|
228
|
+
|
|
229
|
+
Returns:
|
|
230
|
+
AgentRunner: Configured LATS agent
|
|
231
|
+
"""
|
|
232
|
+
agent_worker = LATSAgentWorker.from_tools(
|
|
233
|
+
tools=tools,
|
|
234
|
+
llm=llm,
|
|
235
|
+
num_expansions=3,
|
|
236
|
+
max_rollouts=-1,
|
|
237
|
+
verbose=verbose,
|
|
238
|
+
callback_manager=callback_manager,
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
prompt = format_prompt(
|
|
242
|
+
REACT_PROMPT_TEMPLATE,
|
|
243
|
+
general_instructions,
|
|
244
|
+
topic,
|
|
245
|
+
custom_instructions,
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
agent_worker.chat_formatter = ReActChatFormatter(system_header=prompt)
|
|
249
|
+
return agent_worker.as_agent()
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def create_agent_from_config(
|
|
253
|
+
tools: List[FunctionTool],
|
|
254
|
+
llm,
|
|
255
|
+
memory: Memory,
|
|
256
|
+
config: AgentConfig,
|
|
257
|
+
callback_manager: CallbackManager,
|
|
258
|
+
general_instructions: str,
|
|
259
|
+
topic: str,
|
|
260
|
+
custom_instructions: str,
|
|
261
|
+
verbose: bool = True,
|
|
262
|
+
agent_type: Optional[AgentType] = None, # For compatibility with existing interface
|
|
263
|
+
) -> Union[BaseAgent, AgentRunner]:
|
|
264
|
+
"""
|
|
265
|
+
Create an agent based on configuration.
|
|
266
|
+
|
|
267
|
+
This is the main factory function that delegates to specific agent creators
|
|
268
|
+
based on the agent type in the configuration.
|
|
269
|
+
|
|
270
|
+
Args:
|
|
271
|
+
tools: List of tools available to the agent
|
|
272
|
+
llm: Language model instance
|
|
273
|
+
memory: Agent memory
|
|
274
|
+
config: Agent configuration
|
|
275
|
+
callback_manager: Callback manager for events
|
|
276
|
+
general_instructions: General instructions for the agent
|
|
277
|
+
topic: Topic expertise area
|
|
278
|
+
custom_instructions: Custom user instructions
|
|
279
|
+
verbose: Whether to enable verbose output
|
|
280
|
+
agent_type: Override agent type (for backward compatibility)
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
Union[BaseAgent, AgentRunner]: Configured agent
|
|
284
|
+
|
|
285
|
+
Raises:
|
|
286
|
+
ValueError: If unknown agent type is specified
|
|
287
|
+
"""
|
|
288
|
+
# Use override agent type if provided, otherwise use config
|
|
289
|
+
effective_agent_type = agent_type or config.agent_type
|
|
290
|
+
|
|
291
|
+
# Create base agent based on type
|
|
292
|
+
if effective_agent_type == AgentType.FUNCTION_CALLING:
|
|
293
|
+
agent = create_function_agent(
|
|
294
|
+
tools,
|
|
295
|
+
llm,
|
|
296
|
+
memory,
|
|
297
|
+
config,
|
|
298
|
+
callback_manager,
|
|
299
|
+
general_instructions,
|
|
300
|
+
topic,
|
|
301
|
+
custom_instructions,
|
|
302
|
+
verbose,
|
|
303
|
+
enable_parallel_tool_calls=True, # Enable parallel calls for FUNCTION_CALLING type
|
|
304
|
+
)
|
|
305
|
+
elif effective_agent_type == AgentType.REACT:
|
|
306
|
+
agent = create_react_agent(
|
|
307
|
+
tools,
|
|
308
|
+
llm,
|
|
309
|
+
memory,
|
|
310
|
+
config,
|
|
311
|
+
callback_manager,
|
|
312
|
+
general_instructions,
|
|
313
|
+
topic,
|
|
314
|
+
custom_instructions,
|
|
315
|
+
verbose,
|
|
316
|
+
)
|
|
317
|
+
elif effective_agent_type == AgentType.LLMCOMPILER:
|
|
318
|
+
agent = create_llmcompiler_agent(
|
|
319
|
+
tools,
|
|
320
|
+
llm,
|
|
321
|
+
memory,
|
|
322
|
+
config,
|
|
323
|
+
callback_manager,
|
|
324
|
+
general_instructions,
|
|
325
|
+
topic,
|
|
326
|
+
custom_instructions,
|
|
327
|
+
verbose,
|
|
328
|
+
)
|
|
329
|
+
elif effective_agent_type == AgentType.LATS:
|
|
330
|
+
agent = create_lats_agent(
|
|
331
|
+
tools,
|
|
332
|
+
llm,
|
|
333
|
+
memory,
|
|
334
|
+
config,
|
|
335
|
+
callback_manager,
|
|
336
|
+
general_instructions,
|
|
337
|
+
topic,
|
|
338
|
+
custom_instructions,
|
|
339
|
+
verbose,
|
|
340
|
+
)
|
|
341
|
+
else:
|
|
342
|
+
raise ValueError(f"Unknown agent type: {effective_agent_type}")
|
|
343
|
+
|
|
344
|
+
return agent
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def create_agent_from_corpus(
|
|
348
|
+
tool_name: str,
|
|
349
|
+
data_description: str,
|
|
350
|
+
assistant_specialty: str,
|
|
351
|
+
general_instructions: str = GENERAL_INSTRUCTIONS,
|
|
352
|
+
vectara_corpus_key: str = str(os.environ.get("VECTARA_CORPUS_KEY", "")),
|
|
353
|
+
vectara_api_key: str = str(os.environ.get("VECTARA_API_KEY", "")),
|
|
354
|
+
agent_config: AgentConfig = AgentConfig(),
|
|
355
|
+
fallback_agent_config: Optional[AgentConfig] = None,
|
|
356
|
+
verbose: bool = False,
|
|
357
|
+
vectara_filter_fields: List[dict] = [],
|
|
358
|
+
vectara_offset: int = 0,
|
|
359
|
+
vectara_lambda_val: float = 0.005,
|
|
360
|
+
vectara_semantics: str = "default",
|
|
361
|
+
vectara_custom_dimensions: Dict = {},
|
|
362
|
+
vectara_reranker: str = "slingshot",
|
|
363
|
+
vectara_rerank_k: int = 50,
|
|
364
|
+
vectara_rerank_limit: Optional[int] = None,
|
|
365
|
+
vectara_rerank_cutoff: Optional[float] = None,
|
|
366
|
+
vectara_diversity_bias: float = 0.2,
|
|
367
|
+
vectara_udf_expression: Optional[str] = None,
|
|
368
|
+
vectara_rerank_chain: Optional[List[Dict]] = None,
|
|
369
|
+
vectara_n_sentences_before: int = 2,
|
|
370
|
+
vectara_n_sentences_after: int = 2,
|
|
371
|
+
vectara_summary_num_results: int = 10,
|
|
372
|
+
vectara_summarizer: str = "vectara-summary-ext-24-05-med-omni",
|
|
373
|
+
vectara_summary_response_language: str = "eng",
|
|
374
|
+
vectara_summary_prompt_text: Optional[str] = None,
|
|
375
|
+
vectara_max_response_chars: Optional[int] = None,
|
|
376
|
+
vectara_max_tokens: Optional[int] = None,
|
|
377
|
+
vectara_temperature: Optional[float] = None,
|
|
378
|
+
vectara_frequency_penalty: Optional[float] = None,
|
|
379
|
+
vectara_presence_penalty: Optional[float] = None,
|
|
380
|
+
vectara_save_history: bool = True,
|
|
381
|
+
return_direct: bool = False,
|
|
382
|
+
) -> Dict[str, Any]:
|
|
383
|
+
"""
|
|
384
|
+
Create agent configuration from a single Vectara corpus.
|
|
385
|
+
|
|
386
|
+
This function creates the necessary tools and configuration for an agent
|
|
387
|
+
that can interact with a Vectara corpus for RAG operations.
|
|
388
|
+
|
|
389
|
+
Args:
|
|
390
|
+
tool_name: The name of Vectara tool used by the agent
|
|
391
|
+
data_description: The description of the data
|
|
392
|
+
assistant_specialty: The specialty of the assistant
|
|
393
|
+
general_instructions: General instructions for the agent
|
|
394
|
+
vectara_corpus_key: The Vectara corpus key (or comma separated list of keys)
|
|
395
|
+
vectara_api_key: The Vectara API key
|
|
396
|
+
agent_config: The configuration of the agent
|
|
397
|
+
fallback_agent_config: The fallback configuration of the agent
|
|
398
|
+
verbose: Whether to print verbose output
|
|
399
|
+
vectara_filter_fields: The filterable attributes
|
|
400
|
+
vectara_offset: Number of results to skip
|
|
401
|
+
vectara_lambda_val: Lambda value for Vectara hybrid search
|
|
402
|
+
vectara_semantics: Indicates whether the query is intended as a query or response
|
|
403
|
+
vectara_custom_dimensions: Custom dimensions for the query
|
|
404
|
+
vectara_reranker: The Vectara reranker name
|
|
405
|
+
vectara_rerank_k: The number of results to use with reranking
|
|
406
|
+
vectara_rerank_limit: The maximum number of results to return after reranking
|
|
407
|
+
vectara_rerank_cutoff: The minimum score threshold for results to include
|
|
408
|
+
vectara_diversity_bias: The MMR diversity bias
|
|
409
|
+
vectara_udf_expression: The user defined expression for reranking results
|
|
410
|
+
vectara_rerank_chain: A list of Vectara rerankers to be applied sequentially
|
|
411
|
+
vectara_n_sentences_before: The number of sentences before the matching text
|
|
412
|
+
vectara_n_sentences_after: The number of sentences after the matching text
|
|
413
|
+
vectara_summary_num_results: The number of results to use in summarization
|
|
414
|
+
vectara_summarizer: The Vectara summarizer name
|
|
415
|
+
vectara_summary_response_language: The response language for the Vectara summary
|
|
416
|
+
vectara_summary_prompt_text: The custom prompt, using appropriate prompt variables
|
|
417
|
+
vectara_max_response_chars: The desired maximum number of characters
|
|
418
|
+
vectara_max_tokens: The maximum number of tokens to be returned by the LLM
|
|
419
|
+
vectara_temperature: The sampling temperature
|
|
420
|
+
vectara_frequency_penalty: How much to penalize repeating tokens
|
|
421
|
+
vectara_presence_penalty: How much to penalize repeating tokens for diversity
|
|
422
|
+
vectara_save_history: Whether to save the query in history
|
|
423
|
+
return_direct: Whether the agent should return the tool's response directly
|
|
424
|
+
|
|
425
|
+
Returns:
|
|
426
|
+
Dict[str, Any]: Agent creation parameters including tools and instructions
|
|
427
|
+
"""
|
|
428
|
+
# Create Vectara tool factory
|
|
429
|
+
vec_factory = VectaraToolFactory(
|
|
430
|
+
vectara_api_key=vectara_api_key,
|
|
431
|
+
vectara_corpus_key=vectara_corpus_key,
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
# Build field definitions for the tool schema
|
|
435
|
+
field_definitions = {}
|
|
436
|
+
field_definitions["query"] = (str, Field(description="The user query"))
|
|
437
|
+
|
|
438
|
+
for field in vectara_filter_fields:
|
|
439
|
+
field_definitions[field["name"]] = (
|
|
440
|
+
PY_TYPES.get(field["type"], Any),
|
|
441
|
+
Field(description=field["description"]),
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
# Sanitize tool name
|
|
445
|
+
if tool_name:
|
|
446
|
+
tool_name = re.sub(r"[^A-Za-z0-9_]", "_", tool_name)
|
|
447
|
+
query_args = create_model(f"{tool_name}_QueryArgs", **field_definitions)
|
|
448
|
+
|
|
449
|
+
# Create the Vectara RAG tool
|
|
450
|
+
vectara_tool = vec_factory.create_rag_tool(
|
|
451
|
+
tool_name=tool_name or f"vectara_{vectara_corpus_key}",
|
|
452
|
+
tool_description=f"""
|
|
453
|
+
Given a user query,
|
|
454
|
+
returns a response (str) to a user question about {data_description}.
|
|
455
|
+
""",
|
|
456
|
+
tool_args_schema=query_args,
|
|
457
|
+
reranker=vectara_reranker,
|
|
458
|
+
rerank_k=vectara_rerank_k,
|
|
459
|
+
rerank_limit=vectara_rerank_limit,
|
|
460
|
+
rerank_cutoff=vectara_rerank_cutoff,
|
|
461
|
+
mmr_diversity_bias=vectara_diversity_bias,
|
|
462
|
+
udf_expression=vectara_udf_expression,
|
|
463
|
+
rerank_chain=vectara_rerank_chain,
|
|
464
|
+
n_sentences_before=vectara_n_sentences_before,
|
|
465
|
+
n_sentences_after=vectara_n_sentences_after,
|
|
466
|
+
offset=vectara_offset,
|
|
467
|
+
lambda_val=vectara_lambda_val,
|
|
468
|
+
semantics=vectara_semantics,
|
|
469
|
+
custom_dimensions=vectara_custom_dimensions,
|
|
470
|
+
summary_num_results=vectara_summary_num_results,
|
|
471
|
+
vectara_summarizer=vectara_summarizer,
|
|
472
|
+
summary_response_lang=vectara_summary_response_language,
|
|
473
|
+
vectara_prompt_text=vectara_summary_prompt_text,
|
|
474
|
+
max_response_chars=vectara_max_response_chars,
|
|
475
|
+
max_tokens=vectara_max_tokens,
|
|
476
|
+
temperature=vectara_temperature,
|
|
477
|
+
frequency_penalty=vectara_frequency_penalty,
|
|
478
|
+
presence_penalty=vectara_presence_penalty,
|
|
479
|
+
save_history=vectara_save_history,
|
|
480
|
+
include_citations=True,
|
|
481
|
+
verbose=verbose,
|
|
482
|
+
return_direct=return_direct,
|
|
483
|
+
)
|
|
484
|
+
|
|
485
|
+
# Create assistant instructions
|
|
486
|
+
assistant_instructions = f"""
|
|
487
|
+
- You are a helpful {assistant_specialty} assistant.
|
|
488
|
+
- You can answer questions about {data_description}.
|
|
489
|
+
- Never discuss politics, and always respond politely.
|
|
490
|
+
"""
|
|
491
|
+
|
|
492
|
+
return {
|
|
493
|
+
"tools": [vectara_tool],
|
|
494
|
+
"agent_config": agent_config,
|
|
495
|
+
"topic": assistant_specialty,
|
|
496
|
+
"custom_instructions": assistant_instructions,
|
|
497
|
+
"general_instructions": general_instructions,
|
|
498
|
+
"verbose": verbose,
|
|
499
|
+
"fallback_agent_config": fallback_agent_config,
|
|
500
|
+
"vectara_api_key": vectara_api_key,
|
|
501
|
+
}
|
|
@@ -4,8 +4,9 @@ This file contains the prompt templates for the different types of agents.
|
|
|
4
4
|
|
|
5
5
|
# General (shared) instructions
|
|
6
6
|
GENERAL_INSTRUCTIONS = """
|
|
7
|
-
- Use tools as your main source of information
|
|
8
|
-
- Do not respond based on pre-trained knowledge
|
|
7
|
+
- Use tools as your main source of information.
|
|
8
|
+
- Do not respond based on pre-trained knowledge. Your response should be strictly grounded in the tool outputs or user messages,
|
|
9
|
+
and you should not make up information, add commentary not supported by the source, or hallucinate.
|
|
9
10
|
- Use the 'get_bad_topics' (if it exists) tool to determine the topics you are not allowed to discuss or respond to.
|
|
10
11
|
- Before responding to a user query that requires knowledge of the current date, call the 'get_current_date' tool to get the current date.
|
|
11
12
|
Never rely on previous knowledge of the current date.
|
|
@@ -147,36 +148,3 @@ Below is the current conversation consisting of interleaving human and assistant
|
|
|
147
148
|
"""
|
|
148
149
|
|
|
149
150
|
#
|
|
150
|
-
# Prompts for structured planning agent
|
|
151
|
-
#
|
|
152
|
-
STRUCTURED_PLANNER_INITIAL_PLAN_PROMPT = """\
|
|
153
|
-
Think step-by-step. Given a task and a set of tools, create a comprehensive, end-to-end plan to accomplish the task, using the tools.
|
|
154
|
-
Only use the tools that are relevant to completing the task.
|
|
155
|
-
Keep in mind not every task needs to be decomposed into multiple sub-tasks if it is simple enough.
|
|
156
|
-
The plan should end with a sub-task that can achieve the overall task.
|
|
157
|
-
|
|
158
|
-
The tools available are:
|
|
159
|
-
{tools_str}
|
|
160
|
-
|
|
161
|
-
Overall Task: {task}
|
|
162
|
-
"""
|
|
163
|
-
|
|
164
|
-
STRUCTURED_PLANNER_PLAN_REFINE_PROMPT = """\
|
|
165
|
-
Think step-by-step. Given an overall task, a set of tools, and completed sub-tasks, update (if needed) the remaining sub-tasks so that the overall task can still be completed.
|
|
166
|
-
Only use the tools that are relevant to completing the task.
|
|
167
|
-
Do not add new sub-tasks that are not needed to achieve the overall task.
|
|
168
|
-
The final sub-task in the plan should be the one that can satisfy the overall task.
|
|
169
|
-
If you do update the plan, only create new sub-tasks that will replace the remaining sub-tasks, do NOT repeat tasks that are already completed.
|
|
170
|
-
If the remaining sub-tasks are enough to achieve the overall task, it is ok to skip this step, and instead explain why the plan is complete.
|
|
171
|
-
|
|
172
|
-
The tools available are:
|
|
173
|
-
{tools_str}
|
|
174
|
-
|
|
175
|
-
Completed Sub-Tasks + Outputs:
|
|
176
|
-
{completed_outputs}
|
|
177
|
-
|
|
178
|
-
Remaining Sub-Tasks:
|
|
179
|
-
{remaining_sub_tasks}
|
|
180
|
-
|
|
181
|
-
Overall Task: {task}
|
|
182
|
-
"""
|