web_search.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import annotations
  2. import json
  3. import unittest
  4. try:
  5. from duckduckgo_search import DDGS
  6. from duckduckgo_search.exceptions import DuckDuckGoSearchException
  7. from bs4 import BeautifulSoup
  8. has_requirements = True
  9. except ImportError:
  10. has_requirements = False
  11. from g4f.client import AsyncClient
  12. from .mocks import YieldProviderMock
  13. DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
  14. class TestIterListProvider(unittest.IsolatedAsyncioTestCase):
  15. def setUp(self) -> None:
  16. if not has_requirements:
  17. self.skipTest('web search requirements not passed')
  18. async def test_search(self):
  19. client = AsyncClient(provider=YieldProviderMock)
  20. tool_calls = [
  21. {
  22. "function": {
  23. "arguments": {
  24. "query": "search query", # content of last message: messages[-1]["content"]
  25. "max_results": 5, # maximum number of search results
  26. "max_words": 500, # maximum number of used words from search results for generating the response
  27. "backend": "html", # or "lite", "api": change it to pypass rate limits
  28. "add_text": True, # do scraping websites
  29. "timeout": 5, # in seconds for scraping websites
  30. "region": "wt-wt",
  31. "instructions": "Using the provided web search results, to write a comprehensive reply to the user request.\n"
  32. "Make sure to add the sources of cites using [[Number]](Url) notation after the reference. Example: [[0]](http://google.com)",
  33. },
  34. "name": "search_tool"
  35. },
  36. "type": "function"
  37. }
  38. ]
  39. try:
  40. response = await client.chat.completions.create([{"content": "", "role": "user"}], "", tool_calls=tool_calls)
  41. self.assertIn("Using the provided web search results", response.choices[0].message.content)
  42. except DuckDuckGoSearchException as e:
  43. self.skipTest(f'DuckDuckGoSearchException: {e}')
  44. async def test_search2(self):
  45. client = AsyncClient(provider=YieldProviderMock)
  46. tool_calls = [
  47. {
  48. "function": {
  49. "arguments": {
  50. "query": "search query",
  51. },
  52. "name": "search_tool"
  53. },
  54. "type": "function"
  55. }
  56. ]
  57. try:
  58. response = await client.chat.completions.create([{"content": "", "role": "user"}], "", tool_calls=tool_calls)
  59. self.assertIn("Using the provided web search results", response.choices[0].message.content)
  60. except DuckDuckGoSearchException as e:
  61. self.skipTest(f'DuckDuckGoSearchException: {e}')
  62. async def test_search3(self):
  63. client = AsyncClient(provider=YieldProviderMock)
  64. tool_calls = [
  65. {
  66. "function": {
  67. "arguments": json.dumps({
  68. "query": "search query", # content of last message: messages[-1]["content"]
  69. "max_results": 5, # maximum number of search results
  70. "max_words": 500, # maximum number of used words from search results for generating the response
  71. }),
  72. "name": "search_tool"
  73. },
  74. "type": "function"
  75. }
  76. ]
  77. try:
  78. response = await client.chat.completions.create([{"content": "", "role": "user"}], "", tool_calls=tool_calls)
  79. self.assertIn("Using the provided web search results", response.choices[0].message.content)
  80. except DuckDuckGoSearchException as e:
  81. self.skipTest(f'DuckDuckGoSearchException: {e}')