vectara-agentic 0.2.13__py3-none-any.whl → 0.2.14__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/test_groq.py +120 -0
- tests/test_tools.py +41 -5
- tests/test_vectara_llms.py +0 -11
- vectara_agentic/_version.py +1 -1
- vectara_agentic/agent.py +65 -1
- vectara_agentic/llm_utils.py +174 -0
- vectara_agentic/tool_utils.py +513 -0
- vectara_agentic/tools.py +23 -471
- vectara_agentic/tools_catalog.py +2 -1
- vectara_agentic/utils.py +0 -153
- {vectara_agentic-0.2.13.dist-info → vectara_agentic-0.2.14.dist-info}/METADATA +25 -11
- {vectara_agentic-0.2.13.dist-info → vectara_agentic-0.2.14.dist-info}/RECORD +15 -12
- {vectara_agentic-0.2.13.dist-info → vectara_agentic-0.2.14.dist-info}/WHEEL +1 -1
- {vectara_agentic-0.2.13.dist-info → vectara_agentic-0.2.14.dist-info}/licenses/LICENSE +0 -0
- {vectara_agentic-0.2.13.dist-info → vectara_agentic-0.2.14.dist-info}/top_level.txt +0 -0
tests/test_groq.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
from pydantic import Field, BaseModel
|
|
4
|
+
|
|
5
|
+
from vectara_agentic.agent import Agent, AgentType
|
|
6
|
+
from vectara_agentic.agent_config import AgentConfig
|
|
7
|
+
from vectara_agentic.tools import VectaraToolFactory
|
|
8
|
+
from vectara_agentic.types import ModelProvider
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
import nest_asyncio
|
|
12
|
+
nest_asyncio.apply()
|
|
13
|
+
|
|
14
|
+
tickers = {
|
|
15
|
+
"C": "Citigroup",
|
|
16
|
+
"COF": "Capital One",
|
|
17
|
+
"JPM": "JPMorgan Chase",
|
|
18
|
+
"AAPL": "Apple Computer",
|
|
19
|
+
"GOOG": "Google",
|
|
20
|
+
"AMZN": "Amazon",
|
|
21
|
+
"SNOW": "Snowflake",
|
|
22
|
+
"TEAM": "Atlassian",
|
|
23
|
+
"TSLA": "Tesla",
|
|
24
|
+
"NVDA": "Nvidia",
|
|
25
|
+
"MSFT": "Microsoft",
|
|
26
|
+
"AMD": "Advanced Micro Devices",
|
|
27
|
+
"INTC": "Intel",
|
|
28
|
+
"NFLX": "Netflix",
|
|
29
|
+
"STT": "State Street",
|
|
30
|
+
"BK": "Bank of New York Mellon",
|
|
31
|
+
}
|
|
32
|
+
years = list(range(2015, 2025))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def mult(x: float, y: float) -> float:
|
|
36
|
+
"Multiply two numbers"
|
|
37
|
+
return x * y
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get_company_info() -> list[str]:
|
|
41
|
+
"""
|
|
42
|
+
Returns a dictionary of companies you can query about. Always check this before using any other tool.
|
|
43
|
+
The output is a dictionary of valid ticker symbols mapped to company names.
|
|
44
|
+
You can use this to identify the companies you can query about, and their ticker information.
|
|
45
|
+
"""
|
|
46
|
+
return tickers
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def get_valid_years() -> list[str]:
|
|
50
|
+
"""
|
|
51
|
+
Returns a list of the years for which financial reports are available.
|
|
52
|
+
Always check this before using any other tool.
|
|
53
|
+
"""
|
|
54
|
+
return years
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
config_gemini = AgentConfig(
|
|
58
|
+
agent_type=AgentType.FUNCTION_CALLING,
|
|
59
|
+
main_llm_provider=ModelProvider.GEMINI,
|
|
60
|
+
tool_llm_provider=ModelProvider.GEMINI,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
fc_config_groq = AgentConfig(
|
|
65
|
+
agent_type=AgentType.FUNCTION_CALLING,
|
|
66
|
+
main_llm_provider=ModelProvider.GROQ,
|
|
67
|
+
tool_llm_provider=ModelProvider.GROQ,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class TestGROQ(unittest.TestCase):
|
|
72
|
+
|
|
73
|
+
def test_tool_with_many_arguments(self):
|
|
74
|
+
|
|
75
|
+
vectara_corpus_key = "vectara-docs_1"
|
|
76
|
+
vectara_api_key = "zqt_UXrBcnI2UXINZkrv4g1tQPhzj02vfdtqYJIDiA"
|
|
77
|
+
vec_factory = VectaraToolFactory(vectara_corpus_key, vectara_api_key)
|
|
78
|
+
|
|
79
|
+
class QueryToolArgs(BaseModel):
|
|
80
|
+
arg1: str = Field(description="the first argument", examples=["val1"])
|
|
81
|
+
arg2: str = Field(description="the second argument", examples=["val2"])
|
|
82
|
+
arg3: str = Field(description="the third argument", examples=["val3"])
|
|
83
|
+
arg4: str = Field(description="the fourth argument", examples=["val4"])
|
|
84
|
+
arg5: str = Field(description="the fifth argument", examples=["val5"])
|
|
85
|
+
arg6: str = Field(description="the sixth argument", examples=["val6"])
|
|
86
|
+
arg7: str = Field(description="the seventh argument", examples=["val7"])
|
|
87
|
+
arg8: str = Field(description="the eighth argument", examples=["val8"])
|
|
88
|
+
arg9: str = Field(description="the ninth argument", examples=["val9"])
|
|
89
|
+
arg10: str = Field(description="the tenth argument", examples=["val10"])
|
|
90
|
+
arg11: str = Field(description="the eleventh argument", examples=["val11"])
|
|
91
|
+
arg12: str = Field(description="the twelfth argument", examples=["val12"])
|
|
92
|
+
arg13: str = Field(
|
|
93
|
+
description="the thirteenth argument", examples=["val13"]
|
|
94
|
+
)
|
|
95
|
+
arg14: str = Field(
|
|
96
|
+
description="the fourteenth argument", examples=["val14"]
|
|
97
|
+
)
|
|
98
|
+
arg15: str = Field(description="the fifteenth argument", examples=["val15"])
|
|
99
|
+
|
|
100
|
+
query_tool_1 = vec_factory.create_rag_tool(
|
|
101
|
+
tool_name="rag_tool",
|
|
102
|
+
tool_description="""
|
|
103
|
+
A dummy tool that takes 15 arguments and returns a response (str) to the user query based on the data in this corpus.
|
|
104
|
+
We are using this tool to test the tool factory works and does not crash with OpenAI.
|
|
105
|
+
""",
|
|
106
|
+
tool_args_schema=QueryToolArgs,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
agent = Agent(
|
|
110
|
+
tools=[query_tool_1],
|
|
111
|
+
topic="Sample topic",
|
|
112
|
+
custom_instructions="Call the tool with 15 arguments",
|
|
113
|
+
agent_config=fc_config_groq,
|
|
114
|
+
)
|
|
115
|
+
res = agent.chat("What is the stock price?")
|
|
116
|
+
self.assertIn("I don't know", str(res))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
if __name__ == "__main__":
|
|
120
|
+
unittest.main()
|
tests/test_tools.py
CHANGED
|
@@ -9,6 +9,7 @@ from vectara_agentic.tools import (
|
|
|
9
9
|
)
|
|
10
10
|
from vectara_agentic.agent import Agent
|
|
11
11
|
from vectara_agentic.agent_config import AgentConfig
|
|
12
|
+
from vectara_agentic.types import AgentType, ModelProvider
|
|
12
13
|
|
|
13
14
|
from llama_index.core.tools import FunctionTool
|
|
14
15
|
|
|
@@ -179,22 +180,57 @@ class TestToolsPackage(unittest.TestCase):
|
|
|
179
180
|
query_tool_1 = vec_factory.create_rag_tool(
|
|
180
181
|
tool_name="rag_tool",
|
|
181
182
|
tool_description="""
|
|
182
|
-
A dummy tool that takes
|
|
183
|
+
A dummy tool that takes 15 arguments and returns a response (str) to the user query based on the data in this corpus.
|
|
183
184
|
We are using this tool to test the tool factory works and does not crash with OpenAI.
|
|
184
185
|
""",
|
|
185
186
|
tool_args_schema=QueryToolArgs,
|
|
186
187
|
)
|
|
187
188
|
|
|
188
|
-
|
|
189
|
+
# Test with 15 arguments which go over the 1024 limit.
|
|
190
|
+
config = AgentConfig(
|
|
191
|
+
agent_type=AgentType.OPENAI
|
|
192
|
+
)
|
|
189
193
|
agent = Agent(
|
|
190
194
|
tools=[query_tool_1],
|
|
191
195
|
topic="Sample topic",
|
|
192
|
-
custom_instructions="Call the tool with
|
|
196
|
+
custom_instructions="Call the tool with 15 arguments for OPENAI",
|
|
193
197
|
agent_config=config,
|
|
194
198
|
)
|
|
195
199
|
res = agent.chat("What is the stock price?")
|
|
196
200
|
self.assertIn("maximum length of 1024 characters", str(res))
|
|
197
201
|
|
|
202
|
+
# Same test but with GROQ
|
|
203
|
+
config = AgentConfig(
|
|
204
|
+
agent_type=AgentType.FUNCTION_CALLING,
|
|
205
|
+
main_llm_provider=ModelProvider.GROQ,
|
|
206
|
+
tool_llm_provider=ModelProvider.GROQ,
|
|
207
|
+
)
|
|
208
|
+
agent = Agent(
|
|
209
|
+
tools=[query_tool_1],
|
|
210
|
+
topic="Sample topic",
|
|
211
|
+
custom_instructions="Call the tool with 15 arguments for GROQ",
|
|
212
|
+
agent_config=config,
|
|
213
|
+
)
|
|
214
|
+
res = agent.chat("What is the stock price?")
|
|
215
|
+
self.assertNotIn("maximum length of 1024 characters", str(res))
|
|
216
|
+
|
|
217
|
+
# Same test but with ANTHROPIC
|
|
218
|
+
config = AgentConfig(
|
|
219
|
+
agent_type=AgentType.FUNCTION_CALLING,
|
|
220
|
+
main_llm_provider=ModelProvider.ANTHROPIC,
|
|
221
|
+
tool_llm_provider=ModelProvider.ANTHROPIC,
|
|
222
|
+
)
|
|
223
|
+
agent = Agent(
|
|
224
|
+
tools=[query_tool_1],
|
|
225
|
+
topic="Sample topic",
|
|
226
|
+
custom_instructions="Call the tool with 15 arguments for ANTHROPIC",
|
|
227
|
+
agent_config=config,
|
|
228
|
+
)
|
|
229
|
+
res = agent.chat("What is the stock price?")
|
|
230
|
+
# ANTHROPIC does not have that 1024 limit
|
|
231
|
+
self.assertIn("stock price", str(res))
|
|
232
|
+
|
|
233
|
+
# But using Compact_docstring=True, we can pass 15 arguments successfully.
|
|
198
234
|
vec_factory = VectaraToolFactory(
|
|
199
235
|
vectara_corpus_key, vectara_api_key, compact_docstring=True
|
|
200
236
|
)
|
|
@@ -211,7 +247,7 @@ class TestToolsPackage(unittest.TestCase):
|
|
|
211
247
|
agent = Agent(
|
|
212
248
|
tools=[query_tool_2],
|
|
213
249
|
topic="Sample topic",
|
|
214
|
-
custom_instructions="Call the tool with
|
|
250
|
+
custom_instructions="Call the tool with 15 arguments",
|
|
215
251
|
agent_config=config,
|
|
216
252
|
)
|
|
217
253
|
res = agent.chat("What is the stock price?")
|
|
@@ -227,7 +263,7 @@ class TestToolsPackage(unittest.TestCase):
|
|
|
227
263
|
tool_name="ask_vectara",
|
|
228
264
|
data_description="data from Vectara website",
|
|
229
265
|
assistant_specialty="RAG as a service",
|
|
230
|
-
vectara_summarizer="mockingbird-
|
|
266
|
+
vectara_summarizer="mockingbird-2.0",
|
|
231
267
|
)
|
|
232
268
|
|
|
233
269
|
self.assertIn(
|
tests/test_vectara_llms.py
CHANGED
|
@@ -51,17 +51,6 @@ class TestLLMPackage(unittest.TestCase):
|
|
|
51
51
|
|
|
52
52
|
def test_vectara_mockingbird(self):
|
|
53
53
|
vec_factory = VectaraToolFactory(vectara_corpus_key, vectara_api_key)
|
|
54
|
-
|
|
55
|
-
query_tool = vec_factory.create_rag_tool(
|
|
56
|
-
tool_name="rag_tool",
|
|
57
|
-
tool_description="""
|
|
58
|
-
Returns a response (str) to the user query based on the data in this corpus.
|
|
59
|
-
""",
|
|
60
|
-
vectara_summarizer="mockingbird-1.0-2024-07-16",
|
|
61
|
-
)
|
|
62
|
-
res = query_tool(query="What is Vectara?")
|
|
63
|
-
self.assertIn("Vectara is an end-to-end platform", str(res))
|
|
64
|
-
|
|
65
54
|
query_tool = vec_factory.create_rag_tool(
|
|
66
55
|
tool_name="rag_tool",
|
|
67
56
|
tool_description="""
|
vectara_agentic/_version.py
CHANGED
vectara_agentic/agent.py
CHANGED
|
@@ -12,6 +12,8 @@ import logging
|
|
|
12
12
|
import asyncio
|
|
13
13
|
import importlib
|
|
14
14
|
from collections import Counter
|
|
15
|
+
import inspect
|
|
16
|
+
from inspect import Signature, Parameter, ismethod
|
|
15
17
|
|
|
16
18
|
import cloudpickle as pickle
|
|
17
19
|
|
|
@@ -19,6 +21,7 @@ from dotenv import load_dotenv
|
|
|
19
21
|
|
|
20
22
|
from pydantic import Field, create_model, ValidationError
|
|
21
23
|
|
|
24
|
+
|
|
22
25
|
from llama_index.core.memory import ChatMemoryBuffer
|
|
23
26
|
from llama_index.core.llms import ChatMessage, MessageRole
|
|
24
27
|
from llama_index.core.tools import FunctionTool
|
|
@@ -47,7 +50,7 @@ from .types import (
|
|
|
47
50
|
AgentStreamingResponse,
|
|
48
51
|
AgentConfigType,
|
|
49
52
|
)
|
|
50
|
-
from .
|
|
53
|
+
from .llm_utils import get_llm, get_tokenizer_for_model
|
|
51
54
|
from ._prompts import (
|
|
52
55
|
REACT_PROMPT_TEMPLATE,
|
|
53
56
|
GENERAL_PROMPT_TEMPLATE,
|
|
@@ -230,6 +233,10 @@ class Agent:
|
|
|
230
233
|
self.workflow_cls = workflow_cls
|
|
231
234
|
self.workflow_timeout = workflow_timeout
|
|
232
235
|
|
|
236
|
+
# Sanitize tools for Gemini if needed
|
|
237
|
+
if self.agent_config.main_llm_provider == ModelProvider.GEMINI:
|
|
238
|
+
self.tools = self._sanitize_tools_for_gemini(self.tools)
|
|
239
|
+
|
|
233
240
|
# Validate tools
|
|
234
241
|
# Check for:
|
|
235
242
|
# 1. multiple copies of the same tool
|
|
@@ -311,6 +318,63 @@ class Agent:
|
|
|
311
318
|
print(f"Failed to set up observer ({e}), ignoring")
|
|
312
319
|
self.observability_enabled = False
|
|
313
320
|
|
|
321
|
+
def _sanitize_tools_for_gemini(
|
|
322
|
+
self, tools: list[FunctionTool]
|
|
323
|
+
) -> list[FunctionTool]:
|
|
324
|
+
"""
|
|
325
|
+
Strip all default values from:
|
|
326
|
+
- tool.fn
|
|
327
|
+
- tool.async_fn
|
|
328
|
+
- tool.metadata.fn_schema
|
|
329
|
+
so Gemini sees *only* required parameters, no defaults.
|
|
330
|
+
"""
|
|
331
|
+
for tool in tools:
|
|
332
|
+
# 1) strip defaults off the actual callables
|
|
333
|
+
for func in (tool.fn, tool.async_fn):
|
|
334
|
+
if not func:
|
|
335
|
+
continue
|
|
336
|
+
orig_sig = inspect.signature(func)
|
|
337
|
+
new_params = [
|
|
338
|
+
p.replace(default=Parameter.empty)
|
|
339
|
+
for p in orig_sig.parameters.values()
|
|
340
|
+
]
|
|
341
|
+
new_sig = Signature(
|
|
342
|
+
new_params, return_annotation=orig_sig.return_annotation
|
|
343
|
+
)
|
|
344
|
+
if ismethod(func):
|
|
345
|
+
func.__func__.__signature__ = new_sig
|
|
346
|
+
else:
|
|
347
|
+
func.__signature__ = new_sig
|
|
348
|
+
|
|
349
|
+
# 2) rebuild the Pydantic schema so that *every* field is required
|
|
350
|
+
schema_cls = getattr(tool.metadata, "fn_schema", None)
|
|
351
|
+
if schema_cls and hasattr(schema_cls, "model_fields"):
|
|
352
|
+
# collect (name → (type, Field(...))) for all fields
|
|
353
|
+
new_fields: dict[str, tuple[type, Any]] = {}
|
|
354
|
+
for name, mf in schema_cls.model_fields.items():
|
|
355
|
+
typ = mf.annotation
|
|
356
|
+
desc = getattr(mf, "description", "")
|
|
357
|
+
# force required (no default) with Field(...)
|
|
358
|
+
new_fields[name] = (typ, Field(..., description=desc))
|
|
359
|
+
|
|
360
|
+
# make a brand-new schema class where every field is required
|
|
361
|
+
no_default_schema = create_model(
|
|
362
|
+
f"{schema_cls.__name__}", # new class name
|
|
363
|
+
**new_fields, # type: ignore
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
# give it a clean __signature__ so inspect.signature sees no defaults
|
|
367
|
+
params = [
|
|
368
|
+
Parameter(n, Parameter.POSITIONAL_OR_KEYWORD, annotation=typ)
|
|
369
|
+
for n, (typ, _) in new_fields.items()
|
|
370
|
+
]
|
|
371
|
+
no_default_schema.__signature__ = Signature(params)
|
|
372
|
+
|
|
373
|
+
# swap it back onto the tool
|
|
374
|
+
tool.metadata.fn_schema = no_default_schema
|
|
375
|
+
|
|
376
|
+
return tools
|
|
377
|
+
|
|
314
378
|
def _create_agent(
|
|
315
379
|
self, config: AgentConfig, llm_callback_manager: CallbackManager
|
|
316
380
|
) -> Union[BaseAgent, AgentRunner]:
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Utilities for the Vectara agentic.
|
|
3
|
+
"""
|
|
4
|
+
from types import MethodType
|
|
5
|
+
from typing import Tuple, Callable, Optional
|
|
6
|
+
from functools import lru_cache
|
|
7
|
+
import tiktoken
|
|
8
|
+
|
|
9
|
+
from llama_index.core.llms import LLM
|
|
10
|
+
from llama_index.llms.openai import OpenAI
|
|
11
|
+
from llama_index.llms.anthropic import Anthropic
|
|
12
|
+
|
|
13
|
+
from .types import LLMRole, AgentType, ModelProvider
|
|
14
|
+
from .agent_config import AgentConfig
|
|
15
|
+
from .tool_utils import _updated_openai_prepare_chat_with_tools
|
|
16
|
+
|
|
17
|
+
provider_to_default_model_name = {
|
|
18
|
+
ModelProvider.OPENAI: "gpt-4o",
|
|
19
|
+
ModelProvider.ANTHROPIC: "claude-3-7-sonnet-latest",
|
|
20
|
+
ModelProvider.TOGETHER: "Qwen/Qwen2.5-72B-Instruct-Turbo",
|
|
21
|
+
ModelProvider.GROQ: "meta-llama/llama-4-scout-17b-16e-instruct",
|
|
22
|
+
ModelProvider.FIREWORKS: "accounts/fireworks/models/firefunction-v2",
|
|
23
|
+
ModelProvider.BEDROCK: "anthropic.claude-3-7-sonnet-20250219-v1:0",
|
|
24
|
+
ModelProvider.COHERE: "command-a-03-2025",
|
|
25
|
+
ModelProvider.GEMINI: "models/gemini-2.0-flash",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
DEFAULT_MODEL_PROVIDER = ModelProvider.OPENAI
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@lru_cache(maxsize=None)
|
|
32
|
+
def _get_llm_params_for_role(
|
|
33
|
+
role: LLMRole, config: Optional[AgentConfig] = None
|
|
34
|
+
) -> Tuple[ModelProvider, str]:
|
|
35
|
+
"""
|
|
36
|
+
Get the model provider and model name for the specified role.
|
|
37
|
+
|
|
38
|
+
If config is None, a new AgentConfig() is instantiated using environment defaults.
|
|
39
|
+
"""
|
|
40
|
+
config = config or AgentConfig() # fallback to default config
|
|
41
|
+
|
|
42
|
+
if role == LLMRole.TOOL:
|
|
43
|
+
model_provider = ModelProvider(config.tool_llm_provider)
|
|
44
|
+
# If the user hasn’t explicitly set a tool_llm_model_name,
|
|
45
|
+
# fallback to provider default from provider_to_default_model_name
|
|
46
|
+
model_name = config.tool_llm_model_name or provider_to_default_model_name.get(
|
|
47
|
+
model_provider
|
|
48
|
+
)
|
|
49
|
+
else:
|
|
50
|
+
model_provider = ModelProvider(config.main_llm_provider)
|
|
51
|
+
model_name = config.main_llm_model_name or provider_to_default_model_name.get(
|
|
52
|
+
model_provider
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# If the agent type is OpenAI, check that the main LLM provider is also OpenAI.
|
|
56
|
+
if role == LLMRole.MAIN and config.agent_type == AgentType.OPENAI:
|
|
57
|
+
if model_provider != ModelProvider.OPENAI:
|
|
58
|
+
raise ValueError(
|
|
59
|
+
"OpenAI agent requested but main model provider is not OpenAI."
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
return model_provider, model_name
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@lru_cache(maxsize=None)
|
|
66
|
+
def get_tokenizer_for_model(
|
|
67
|
+
role: LLMRole, config: Optional[AgentConfig] = None
|
|
68
|
+
) -> Optional[Callable]:
|
|
69
|
+
"""
|
|
70
|
+
Get the tokenizer for the specified model, as determined by the role & config.
|
|
71
|
+
"""
|
|
72
|
+
model_provider, model_name = _get_llm_params_for_role(role, config)
|
|
73
|
+
if model_provider == ModelProvider.OPENAI:
|
|
74
|
+
# This might raise an exception if the model_name is unknown to tiktoken
|
|
75
|
+
return tiktoken.encoding_for_model(model_name).encode
|
|
76
|
+
if model_provider == ModelProvider.ANTHROPIC:
|
|
77
|
+
return Anthropic().tokenizer
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@lru_cache(maxsize=None)
|
|
82
|
+
def get_llm(role: LLMRole, config: Optional[AgentConfig] = None) -> LLM:
|
|
83
|
+
"""
|
|
84
|
+
Get the LLM for the specified role, using the provided config
|
|
85
|
+
or a default if none is provided.
|
|
86
|
+
"""
|
|
87
|
+
max_tokens = 8192
|
|
88
|
+
model_provider, model_name = _get_llm_params_for_role(role, config)
|
|
89
|
+
if model_provider == ModelProvider.OPENAI:
|
|
90
|
+
llm = OpenAI(
|
|
91
|
+
model=model_name,
|
|
92
|
+
temperature=0,
|
|
93
|
+
is_function_calling_model=True,
|
|
94
|
+
strict=True,
|
|
95
|
+
max_tokens=max_tokens,
|
|
96
|
+
pydantic_program_mode="openai",
|
|
97
|
+
)
|
|
98
|
+
elif model_provider == ModelProvider.ANTHROPIC:
|
|
99
|
+
llm = Anthropic(
|
|
100
|
+
model=model_name,
|
|
101
|
+
temperature=0,
|
|
102
|
+
max_tokens=max_tokens,
|
|
103
|
+
)
|
|
104
|
+
elif model_provider == ModelProvider.GEMINI:
|
|
105
|
+
from llama_index.llms.google_genai import GoogleGenAI
|
|
106
|
+
|
|
107
|
+
llm = GoogleGenAI(
|
|
108
|
+
model=model_name,
|
|
109
|
+
temperature=0,
|
|
110
|
+
is_function_calling_model=True,
|
|
111
|
+
allow_parallel_tool_calls=True,
|
|
112
|
+
max_tokens=max_tokens,
|
|
113
|
+
)
|
|
114
|
+
elif model_provider == ModelProvider.TOGETHER:
|
|
115
|
+
from llama_index.llms.together import TogetherLLM
|
|
116
|
+
|
|
117
|
+
llm = TogetherLLM(
|
|
118
|
+
model=model_name,
|
|
119
|
+
temperature=0,
|
|
120
|
+
is_function_calling_model=True,
|
|
121
|
+
max_tokens=max_tokens,
|
|
122
|
+
)
|
|
123
|
+
# pylint: disable=protected-access
|
|
124
|
+
llm._prepare_chat_with_tools = MethodType(
|
|
125
|
+
_updated_openai_prepare_chat_with_tools,
|
|
126
|
+
llm,
|
|
127
|
+
)
|
|
128
|
+
elif model_provider == ModelProvider.GROQ:
|
|
129
|
+
from llama_index.llms.groq import Groq
|
|
130
|
+
|
|
131
|
+
llm = Groq(
|
|
132
|
+
model=model_name,
|
|
133
|
+
temperature=0,
|
|
134
|
+
is_function_calling_model=True,
|
|
135
|
+
max_tokens=max_tokens,
|
|
136
|
+
)
|
|
137
|
+
# pylint: disable=protected-access
|
|
138
|
+
llm._prepare_chat_with_tools = MethodType(
|
|
139
|
+
_updated_openai_prepare_chat_with_tools,
|
|
140
|
+
llm,
|
|
141
|
+
)
|
|
142
|
+
elif model_provider == ModelProvider.FIREWORKS:
|
|
143
|
+
from llama_index.llms.fireworks import Fireworks
|
|
144
|
+
|
|
145
|
+
llm = Fireworks(model=model_name, temperature=0, max_tokens=max_tokens)
|
|
146
|
+
elif model_provider == ModelProvider.BEDROCK:
|
|
147
|
+
from llama_index.llms.bedrock import Bedrock
|
|
148
|
+
|
|
149
|
+
llm = Bedrock(model=model_name, temperature=0, max_tokens=max_tokens)
|
|
150
|
+
elif model_provider == ModelProvider.COHERE:
|
|
151
|
+
from llama_index.llms.cohere import Cohere
|
|
152
|
+
|
|
153
|
+
llm = Cohere(model=model_name, temperature=0, max_tokens=max_tokens)
|
|
154
|
+
elif model_provider == ModelProvider.PRIVATE:
|
|
155
|
+
from llama_index.llms.openai_like import OpenAILike
|
|
156
|
+
|
|
157
|
+
llm = OpenAILike(
|
|
158
|
+
model=model_name,
|
|
159
|
+
temperature=0,
|
|
160
|
+
is_function_calling_model=True,
|
|
161
|
+
is_chat_model=True,
|
|
162
|
+
api_base=config.private_llm_api_base,
|
|
163
|
+
api_key=config.private_llm_api_key,
|
|
164
|
+
max_tokens=max_tokens,
|
|
165
|
+
)
|
|
166
|
+
# pylint: disable=protected-access
|
|
167
|
+
llm._prepare_chat_with_tools = MethodType(
|
|
168
|
+
_updated_openai_prepare_chat_with_tools,
|
|
169
|
+
llm,
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
else:
|
|
173
|
+
raise ValueError(f"Unknown LLM provider: {model_provider}")
|
|
174
|
+
return llm
|