vectara-agentic 0.3.3__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.

Files changed (53) hide show
  1. tests/__init__.py +7 -0
  2. tests/conftest.py +312 -0
  3. tests/endpoint.py +54 -17
  4. tests/run_tests.py +111 -0
  5. tests/test_agent.py +10 -5
  6. tests/test_agent_type.py +82 -143
  7. tests/test_api_endpoint.py +4 -0
  8. tests/test_bedrock.py +4 -0
  9. tests/test_fallback.py +4 -0
  10. tests/test_gemini.py +28 -45
  11. tests/test_groq.py +4 -0
  12. tests/test_private_llm.py +11 -2
  13. tests/test_return_direct.py +6 -2
  14. tests/test_serialization.py +4 -0
  15. tests/test_streaming.py +88 -0
  16. tests/test_tools.py +10 -82
  17. tests/test_vectara_llms.py +4 -0
  18. tests/test_vhc.py +66 -0
  19. tests/test_workflow.py +4 -0
  20. vectara_agentic/__init__.py +27 -4
  21. vectara_agentic/_callback.py +65 -67
  22. vectara_agentic/_observability.py +30 -30
  23. vectara_agentic/_version.py +1 -1
  24. vectara_agentic/agent.py +375 -848
  25. vectara_agentic/agent_config.py +15 -14
  26. vectara_agentic/agent_core/__init__.py +22 -0
  27. vectara_agentic/agent_core/factory.py +501 -0
  28. vectara_agentic/{_prompts.py → agent_core/prompts.py} +3 -35
  29. vectara_agentic/agent_core/serialization.py +345 -0
  30. vectara_agentic/agent_core/streaming.py +495 -0
  31. vectara_agentic/agent_core/utils/__init__.py +34 -0
  32. vectara_agentic/agent_core/utils/hallucination.py +202 -0
  33. vectara_agentic/agent_core/utils/logging.py +52 -0
  34. vectara_agentic/agent_core/utils/prompt_formatting.py +56 -0
  35. vectara_agentic/agent_core/utils/schemas.py +87 -0
  36. vectara_agentic/agent_core/utils/tools.py +125 -0
  37. vectara_agentic/agent_endpoint.py +4 -6
  38. vectara_agentic/db_tools.py +37 -12
  39. vectara_agentic/llm_utils.py +41 -42
  40. vectara_agentic/sub_query_workflow.py +9 -14
  41. vectara_agentic/tool_utils.py +138 -83
  42. vectara_agentic/tools.py +36 -21
  43. vectara_agentic/tools_catalog.py +16 -16
  44. vectara_agentic/types.py +98 -6
  45. {vectara_agentic-0.3.3.dist-info → vectara_agentic-0.4.0.dist-info}/METADATA +69 -30
  46. vectara_agentic-0.4.0.dist-info/RECORD +50 -0
  47. tests/test_agent_planning.py +0 -64
  48. tests/test_hhem.py +0 -100
  49. vectara_agentic/hhem.py +0 -82
  50. vectara_agentic-0.3.3.dist-info/RECORD +0 -39
  51. {vectara_agentic-0.3.3.dist-info → vectara_agentic-0.4.0.dist-info}/WHEEL +0 -0
  52. {vectara_agentic-0.3.3.dist-info → vectara_agentic-0.4.0.dist-info}/licenses/LICENSE +0 -0
  53. {vectara_agentic-0.3.3.dist-info → vectara_agentic-0.4.0.dist-info}/top_level.txt +0 -0
tests/test_agent_type.py CHANGED
@@ -1,198 +1,137 @@
1
- import unittest
2
-
3
- from vectara_agentic.agent import Agent, AgentType
4
- from vectara_agentic.agent_config import AgentConfig
5
- from vectara_agentic.tools import ToolsFactory
6
- from vectara_agentic.types import ModelProvider
7
-
8
- import nest_asyncio
9
- nest_asyncio.apply()
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
10
3
 
11
- def mult(x: float, y: float) -> float:
12
- "Multiply two numbers"
13
- return x * y
14
-
15
-
16
- react_config_anthropic = AgentConfig(
17
- agent_type=AgentType.REACT,
18
- main_llm_provider=ModelProvider.ANTHROPIC,
19
- tool_llm_provider=ModelProvider.ANTHROPIC,
20
- )
21
-
22
- # React with Google does not work with Gemini 2.5-flash
23
- react_config_gemini = AgentConfig(
24
- agent_type=AgentType.REACT,
25
- main_llm_provider=ModelProvider.GEMINI,
26
- main_llm_model_name="models/gemini-2.0-flash",
27
- tool_llm_provider=ModelProvider.GEMINI,
28
- tool_llm_model_name="models/gemini-2.0-flash",
29
- )
30
-
31
- react_config_together = AgentConfig(
32
- agent_type=AgentType.REACT,
33
- main_llm_provider=ModelProvider.TOGETHER,
34
- tool_llm_provider=ModelProvider.TOGETHER,
35
- )
36
-
37
- react_config_groq = AgentConfig(
38
- agent_type=AgentType.REACT,
39
- main_llm_provider=ModelProvider.GROQ,
40
- tool_llm_provider=ModelProvider.GROQ,
41
- )
4
+ warnings.simplefilter("ignore", DeprecationWarning)
42
5
 
43
- fc_config_anthropic = AgentConfig(
44
- agent_type=AgentType.FUNCTION_CALLING,
45
- main_llm_provider=ModelProvider.ANTHROPIC,
46
- tool_llm_provider=ModelProvider.ANTHROPIC,
47
- )
48
-
49
- fc_config_gemini = AgentConfig(
50
- agent_type=AgentType.FUNCTION_CALLING,
51
- main_llm_provider=ModelProvider.GEMINI,
52
- tool_llm_provider=ModelProvider.GEMINI,
53
- )
54
-
55
- fc_config_together = AgentConfig(
56
- agent_type=AgentType.FUNCTION_CALLING,
57
- main_llm_provider=ModelProvider.TOGETHER,
58
- tool_llm_provider=ModelProvider.TOGETHER,
59
- )
6
+ import unittest
60
7
 
61
- fc_config_groq = AgentConfig(
62
- agent_type=AgentType.FUNCTION_CALLING,
63
- main_llm_provider=ModelProvider.GROQ,
64
- tool_llm_provider=ModelProvider.GROQ,
8
+ from conftest import (
9
+ AgentTestMixin,
10
+ mult,
11
+ STANDARD_TEST_TOPIC,
12
+ STANDARD_TEST_INSTRUCTIONS,
13
+ default_config,
14
+ react_config_anthropic,
15
+ react_config_gemini,
16
+ react_config_together,
17
+ fc_config_anthropic,
18
+ fc_config_gemini,
19
+ fc_config_together,
65
20
  )
21
+ from vectara_agentic.agent import Agent
22
+ from vectara_agentic.tools import ToolsFactory
66
23
 
24
+ class TestAgentType(unittest.TestCase, AgentTestMixin):
67
25
 
68
- openai_config = AgentConfig(
69
- agent_type=AgentType.OPENAI,
70
- )
71
-
72
- class TestAgentType(unittest.TestCase):
26
+ def setUp(self):
27
+ self.tools = [ToolsFactory().create_tool(mult)]
28
+ self.topic = STANDARD_TEST_TOPIC
29
+ self.instructions = STANDARD_TEST_INSTRUCTIONS
73
30
 
74
- def test_openai(self):
75
- tools = [ToolsFactory().create_tool(mult)]
76
- topic = "AI topic"
77
- instructions = "Always do as your father tells you, if your mother agrees!"
31
+ def test_default_function_calling(self):
78
32
  agent = Agent(
79
- agent_config=openai_config,
80
- tools=tools,
81
- topic=topic,
82
- custom_instructions=instructions,
33
+ agent_config=default_config,
34
+ tools=self.tools,
35
+ topic=self.topic,
36
+ custom_instructions=self.instructions,
83
37
  )
84
38
 
85
39
  agent.chat("What is 5 times 10. Only give the answer, nothing else")
86
40
  agent.chat("what is 3 times 7. Only give the answer, nothing else")
87
- res = agent.chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
41
+ res = agent.chat(
42
+ "multiply the results of the last two multiplications. Only give the answer, nothing else."
43
+ )
88
44
  self.assertIn("1050", res.response)
89
45
 
90
46
  def test_gemini(self):
91
- tools = [ToolsFactory().create_tool(mult)]
92
- topic = "AI topic"
93
- instructions = "Always do as your father tells you, if your mother agrees!"
94
-
95
47
  agent = Agent(
96
48
  agent_config=react_config_gemini,
97
- tools=tools,
98
- topic=topic,
99
- custom_instructions=instructions,
49
+ tools=self.tools,
50
+ topic=self.topic,
51
+ custom_instructions=self.instructions,
100
52
  )
101
53
  agent.chat("What is 5 times 10. Only give the answer, nothing else")
102
54
  agent.chat("what is 3 times 7. Only give the answer, nothing else")
103
- res = agent.chat("what is the result of multiplying the results of the last two multiplications. Only give the answer, nothing else.")
55
+ res = agent.chat(
56
+ "what is the result of multiplying the results of the last two "
57
+ "multiplications. Only give the answer, nothing else."
58
+ )
104
59
  self.assertIn("1050", res.response)
105
60
 
106
61
  agent = Agent(
107
62
  agent_config=fc_config_gemini,
108
- tools=tools,
109
- topic=topic,
110
- custom_instructions=instructions,
63
+ tools=self.tools,
64
+ topic=self.topic,
65
+ custom_instructions=self.instructions,
111
66
  )
112
67
  agent.chat("What is 5 times 10. Only give the answer, nothing else")
113
68
  agent.chat("what is 3 times 7. Only give the answer, nothing else")
114
- res = agent.chat("what is the result of multiplying the results of the last two multiplications. Only give the answer, nothing else.")
69
+ res = agent.chat(
70
+ "what is the result of multiplying the results of the last two "
71
+ "multiplications. Only give the answer, nothing else."
72
+ )
115
73
  self.assertIn("1050", res.response)
116
74
 
117
75
  def test_together(self):
118
- tools = [ToolsFactory().create_tool(mult)]
119
- topic = "AI topic"
120
- instructions = "Always do as your father tells you, if your mother agrees!"
121
-
76
+ # Test ReAct agent with Together
122
77
  agent = Agent(
123
78
  agent_config=react_config_together,
124
- tools=tools,
125
- topic=topic,
126
- custom_instructions=instructions,
127
- )
128
- agent.chat("What is 5 times 10. Only give the answer, nothing else")
129
- agent.chat("what is 3 times 7. Only give the answer, nothing else")
130
- res = agent.chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
131
- self.assertIn("1050", res.response)
132
-
133
- agent = Agent(
134
- agent_config=fc_config_together,
135
- tools=tools,
136
- topic=topic,
137
- custom_instructions=instructions,
79
+ tools=self.tools,
80
+ topic=self.topic,
81
+ custom_instructions=self.instructions,
138
82
  )
139
- agent.chat("What is 5 times 10. Only give the answer, nothing else")
140
- agent.chat("what is 3 times 7. Only give the answer, nothing else")
141
- res = agent.chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
142
- self.assertIn("1050", res.response)
143
83
 
144
- def test_groq(self):
145
- tools = [ToolsFactory().create_tool(mult)]
146
- topic = "AI topic"
147
- instructions = "Always do as your father tells you, if your mother agrees!"
84
+ with self.with_provider_fallback("Together AI"):
85
+ agent.chat("What is 5 times 10. Only give the answer, nothing else")
86
+ agent.chat("what is 3 times 7. Only give the answer, nothing else")
87
+ res = agent.chat(
88
+ "multiply the results of the last two multiplications. Only give the answer, nothing else."
89
+ )
90
+ self.check_response_and_skip(res, "Together AI")
91
+ self.assertIn("1050", res.response)
148
92
 
93
+ # Test Function Calling agent with Together
149
94
  agent = Agent(
150
- agent_config=react_config_groq,
151
- tools=tools,
152
- topic=topic,
153
- custom_instructions=instructions,
95
+ agent_config=fc_config_together,
96
+ tools=self.tools,
97
+ topic=self.topic,
98
+ custom_instructions=self.instructions,
154
99
  )
155
- agent.chat("What is 5 times 10. Only give the answer, nothing else")
156
- agent.chat("what is 3 times 7. Only give the answer, nothing else")
157
- res = agent.chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
158
- self.assertIn("1050", res.response)
159
100
 
160
- agent = Agent(
161
- agent_config=fc_config_groq,
162
- tools=tools,
163
- topic=topic,
164
- custom_instructions=instructions,
165
- )
166
- agent.chat("What is 5 times 10. Only give the answer, nothing else")
167
- agent.chat("what is 3 times 7. Only give the answer, nothing else")
168
- res = agent.chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
169
- self.assertIn("1050", res.response)
101
+ with self.with_provider_fallback("Together AI"):
102
+ agent.chat("What is 5 times 10. Only give the answer, nothing else")
103
+ agent.chat("what is 3 times 7. Only give the answer, nothing else")
104
+ res = agent.chat(
105
+ "multiply the results of the last two multiplications. Only give the answer, nothing else."
106
+ )
107
+ self.check_response_and_skip(res, "Together AI")
108
+ self.assertIn("1050", res.response)
170
109
 
171
110
  def test_anthropic(self):
172
- tools = [ToolsFactory().create_tool(mult)]
173
- topic = "AI topic"
174
- instructions = "Always do as your father tells you, if your mother agrees!"
175
-
176
111
  agent = Agent(
177
112
  agent_config=react_config_anthropic,
178
- tools=tools,
179
- topic=topic,
180
- custom_instructions=instructions,
113
+ tools=self.tools,
114
+ topic=self.topic,
115
+ custom_instructions=self.instructions,
181
116
  )
182
117
  agent.chat("What is 5 times 10. Only give the answer, nothing else")
183
118
  agent.chat("what is 3 times 7. Only give the answer, nothing else")
184
- res = agent.chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
119
+ res = agent.chat(
120
+ "multiply the results of the last two multiplications. Only give the answer, nothing else."
121
+ )
185
122
  self.assertIn("1050", res.response)
186
123
 
187
124
  agent = Agent(
188
125
  agent_config=fc_config_anthropic,
189
- tools=tools,
190
- topic=topic,
191
- custom_instructions=instructions,
126
+ tools=self.tools,
127
+ topic=self.topic,
128
+ custom_instructions=self.instructions,
192
129
  )
193
130
  agent.chat("What is 5 times 10. Only give the answer, nothing else")
194
131
  agent.chat("what is 3 times 7. Only give the answer, nothing else")
195
- res = agent.chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
132
+ res = agent.chat(
133
+ "multiply the results of the last two multiplications. Only give the answer, nothing else."
134
+ )
196
135
  self.assertIn("1050", res.response)
197
136
 
198
137
 
@@ -1,3 +1,7 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
1
5
  import unittest
2
6
  from uuid import UUID
3
7
 
tests/test_bedrock.py CHANGED
@@ -1,3 +1,7 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
1
5
  import unittest
2
6
 
3
7
  from vectara_agentic.agent import Agent, AgentType
tests/test_fallback.py CHANGED
@@ -1,3 +1,7 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
1
5
  import os
2
6
  import unittest
3
7
  import subprocess
tests/test_gemini.py CHANGED
@@ -1,11 +1,13 @@
1
- import unittest
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
2
4
 
3
- from pydantic import Field, BaseModel
5
+ import unittest
4
6
 
5
7
  from vectara_agentic.agent import Agent, AgentType
6
8
  from vectara_agentic.agent_config import AgentConfig
7
- from vectara_agentic.tools import VectaraToolFactory
8
9
  from vectara_agentic.types import ModelProvider
10
+ from vectara_agentic.tools import ToolsFactory
9
11
 
10
12
 
11
13
  import nest_asyncio
@@ -60,55 +62,36 @@ fc_config_gemini = AgentConfig(
60
62
  tool_llm_provider=ModelProvider.GEMINI,
61
63
  )
62
64
 
63
-
64
65
  class TestGEMINI(unittest.TestCase):
66
+ def test_gemini(self):
67
+ tools = [ToolsFactory().create_tool(mult)]
68
+ topic = "AI topic"
69
+ instructions = "Always do as your father tells you, if your mother agrees!"
65
70
 
66
- def test_tool_with_many_arguments(self):
67
-
68
- vectara_corpus_key = "vectara-docs_1"
69
- vectara_api_key = "zqt_UXrBcnI2UXINZkrv4g1tQPhzj02vfdtqYJIDiA"
70
- vec_factory = VectaraToolFactory(vectara_corpus_key, vectara_api_key)
71
-
72
- class QueryToolArgs(BaseModel):
73
- arg1: str = Field(description="the first argument", examples=["val1"])
74
- arg2: str = Field(description="the second argument", examples=["val2"])
75
- arg3: str = Field(description="the third argument", examples=["val3"])
76
- arg4: str = Field(description="the fourth argument", examples=["val4"])
77
- arg5: str = Field(description="the fifth argument", examples=["val5"])
78
- arg6: str = Field(description="the sixth argument", examples=["val6"])
79
- arg7: str = Field(description="the seventh argument", examples=["val7"])
80
- arg8: str = Field(description="the eighth argument", examples=["val8"])
81
- arg9: str = Field(description="the ninth argument", examples=["val9"])
82
- arg10: str = Field(description="the tenth argument", examples=["val10"])
83
- arg11: str = Field(description="the eleventh argument", examples=["val11"])
84
- arg12: str = Field(description="the twelfth argument", examples=["val12"])
85
- arg13: str = Field(
86
- description="the thirteenth argument", examples=["val13"]
87
- )
88
- arg14: str = Field(
89
- description="the fourteenth argument", examples=["val14"]
90
- )
91
- arg15: str = Field(description="the fifteenth argument", examples=["val15"])
92
-
93
- query_tool_1 = vec_factory.create_rag_tool(
94
- tool_name="rag_tool",
95
- tool_description="""
96
- A dummy tool that takes 15 arguments and returns a response (str) to the user query based on the data in this corpus.
97
- We are using this tool to test the tool factory works and does not crash with OpenAI.
98
- """,
99
- tool_args_schema=QueryToolArgs,
71
+ agent = Agent(
72
+ agent_config=fc_config_gemini,
73
+ tools=tools,
74
+ topic=topic,
75
+ custom_instructions=instructions,
100
76
  )
77
+ _ = agent.chat("What is 5 times 10. Only give the answer, nothing else")
78
+ _ = agent.chat("what is 3 times 7. Only give the answer, nothing else")
79
+ res = agent.chat("what is the result of multiplying the results of the last two multiplications. Only give the answer, nothing else.")
80
+ self.assertIn("1050", res.response)
81
+
82
+ def test_gemini_single_prompt(self):
83
+ tools = [ToolsFactory().create_tool(mult)]
84
+ topic = "AI topic"
85
+ instructions = "Always do as your father tells you, if your mother agrees!"
101
86
 
102
87
  agent = Agent(
103
- tools=[query_tool_1],
104
- topic="Sample topic",
105
- custom_instructions="Call the tool with 15 arguments",
106
88
  agent_config=fc_config_gemini,
89
+ tools=tools,
90
+ topic=topic,
91
+ custom_instructions=instructions,
107
92
  )
108
- res = agent.chat("What is the stock price?")
109
- self.assertTrue(
110
- any(sub in str(res) for sub in ["I don't know", "I do not have"])
111
- )
93
+ res = agent.chat("First, multiply 5 by 10. Then, multiply 3 by 7. Finally, multiply the results of the first two calculations.")
94
+ self.assertIn("1050", res.response)
112
95
 
113
96
 
114
97
  if __name__ == "__main__":
tests/test_groq.py CHANGED
@@ -1,3 +1,7 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
1
5
  import unittest
2
6
 
3
7
  from vectara_agentic.agent import Agent, AgentType
tests/test_private_llm.py CHANGED
@@ -1,3 +1,7 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
1
5
  import os
2
6
  import unittest
3
7
  import subprocess
@@ -48,19 +52,24 @@ class TestPrivateLLM(unittest.TestCase):
48
52
  topic = "calculator"
49
53
  custom_instructions = "you are an agent specializing in math, assisting a user."
50
54
  config = AgentConfig(
51
- agent_type=AgentType.REACT,
55
+ agent_type=AgentType.FUNCTION_CALLING,
52
56
  main_llm_provider=ModelProvider.PRIVATE,
53
57
  main_llm_model_name="gpt-4.1",
54
58
  private_llm_api_base=f"http://127.0.0.1:{FLASK_PORT}/v1",
55
59
  private_llm_api_key="TEST_API_KEY",
56
60
  )
57
61
  agent = Agent(agent_config=config, tools=tools, topic=topic,
58
- custom_instructions=custom_instructions)
62
+ custom_instructions=custom_instructions, verbose=False)
59
63
 
60
64
  # To run this test, you must have OPENAI_API_KEY in your environment
61
65
  res = agent.chat(
62
66
  "What is 5 times 10. Only give the answer, nothing else."
63
67
  ).response
68
+ if res is None:
69
+ self.fail("Agent returned None response")
70
+ # Convert to string for comparison if it's a number
71
+ if isinstance(res, (int, float)):
72
+ res = str(int(res))
64
73
  self.assertEqual(res, "50")
65
74
 
66
75
 
@@ -1,3 +1,7 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
1
5
  import unittest
2
6
 
3
7
  from vectara_agentic.agent import Agent
@@ -26,7 +30,7 @@ class TestAgentPackage(unittest.TestCase):
26
30
  custom_instructions="You are a helpful assistant.",
27
31
  )
28
32
  res = agent.chat("What is Vectara?")
29
- self.assertIn("Vectara is an end-to-end platform designed", str(res))
33
+ self.assertIn("Vectara is an end-to-end platform", str(res))
30
34
 
31
35
  def test_from_corpus(self):
32
36
  agent = Agent.from_corpus(
@@ -38,7 +42,7 @@ class TestAgentPackage(unittest.TestCase):
38
42
  return_direct=True,
39
43
  )
40
44
  res = agent.chat("What is Vectara?")
41
- self.assertIn("Vectara is an end-to-end platform designed", str(res))
45
+ self.assertIn("Vectara is an end-to-end platform", str(res))
42
46
 
43
47
 
44
48
  if __name__ == "__main__":
@@ -1,3 +1,7 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
1
5
  import unittest
2
6
  import threading
3
7
  import os
@@ -0,0 +1,88 @@
1
+ # Suppress external dependency warnings before any other imports
2
+ import warnings
3
+ warnings.simplefilter("ignore", DeprecationWarning)
4
+
5
+ import unittest
6
+ import asyncio
7
+
8
+ from vectara_agentic.agent import Agent, AgentType
9
+ from vectara_agentic.agent_config import AgentConfig
10
+ from vectara_agentic.tools import ToolsFactory
11
+ from vectara_agentic.types import ModelProvider
12
+
13
+ import nest_asyncio
14
+ nest_asyncio.apply()
15
+
16
+ def mult(x: float, y: float) -> float:
17
+ "Multiply two numbers"
18
+ return x * y
19
+
20
+
21
+ config_function_calling_openai = AgentConfig(
22
+ agent_type=AgentType.FUNCTION_CALLING,
23
+ main_llm_provider=ModelProvider.OPENAI,
24
+ tool_llm_provider=ModelProvider.OPENAI,
25
+ )
26
+
27
+ fc_config_anthropic = AgentConfig(
28
+ agent_type=AgentType.FUNCTION_CALLING,
29
+ main_llm_provider=ModelProvider.ANTHROPIC,
30
+ tool_llm_provider=ModelProvider.ANTHROPIC,
31
+ )
32
+
33
+ fc_config_gemini = AgentConfig(
34
+ agent_type=AgentType.FUNCTION_CALLING,
35
+ main_llm_provider=ModelProvider.GEMINI,
36
+ tool_llm_provider=ModelProvider.GEMINI,
37
+ )
38
+
39
+ fc_config_together = AgentConfig(
40
+ agent_type=AgentType.FUNCTION_CALLING,
41
+ main_llm_provider=ModelProvider.TOGETHER,
42
+ tool_llm_provider=ModelProvider.TOGETHER,
43
+ )
44
+
45
+
46
+ class TestAgentStreaming(unittest.TestCase):
47
+
48
+ async def test_anthropic(self):
49
+ tools = [ToolsFactory().create_tool(mult)]
50
+ topic = "AI topic"
51
+ instructions = "Always do as your father tells you, if your mother agrees!"
52
+ agent = Agent(
53
+ agent_config=fc_config_anthropic, # Use function calling which has better streaming
54
+ tools=tools,
55
+ topic=topic,
56
+ custom_instructions=instructions,
57
+ )
58
+
59
+ # First calculation: 5 * 10 = 50
60
+ stream1 = await agent.astream_chat("What is 5 times 10. Only give the answer, nothing else")
61
+ # Consume the stream
62
+ async for chunk in stream1.async_response_gen():
63
+ pass
64
+ _ = await stream1.aget_response()
65
+
66
+ # Second calculation: 3 * 7 = 21
67
+ stream2 = await agent.astream_chat("what is 3 times 7. Only give the answer, nothing else")
68
+ # Consume the stream
69
+ async for chunk in stream2.async_response_gen():
70
+ pass
71
+ _ = await stream2.aget_response()
72
+
73
+ # Final calculation: 50 * 21 = 1050
74
+ stream3 = await agent.astream_chat("multiply the results of the last two multiplications. Only give the answer, nothing else.")
75
+ # Consume the stream
76
+ async for chunk in stream3.async_response_gen():
77
+ pass
78
+ response3 = await stream3.aget_response()
79
+
80
+ self.assertIn("1050", response3.response)
81
+
82
+ def test_openai_sync(self):
83
+ """Synchronous wrapper for the async test"""
84
+ asyncio.run(self.test_anthropic())
85
+
86
+
87
+ if __name__ == "__main__":
88
+ unittest.main()