From 4c374e6458afe6464d49fb4bb6271f6e7cf79c6f Mon Sep 17 00:00:00 2001 From: Maximilian Winter Date: Sun, 26 May 2024 03:41:50 +0200 Subject: [PATCH] Added web search code as part of the framework --- .../web_search_agent.py | 45 +++++++++++++++++++ src/llama_cpp_agent/tools/__init__.py | 1 + .../tools/web_search/__init__.py | 4 ++ .../tools}/web_search/default_web_crawlers.py | 0 .../default_web_search_providers.py | 0 .../llama_cpp_agent/tools/web_search/tool.py | 40 ----------------- .../web_search/web_search_interfaces.py | 0 7 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 examples/03_Tools_And_Function_Calling/web_search_agent.py create mode 100644 src/llama_cpp_agent/tools/__init__.py create mode 100644 src/llama_cpp_agent/tools/web_search/__init__.py rename {examples/03_Tools_And_Function_Calling => src/llama_cpp_agent/tools}/web_search/default_web_crawlers.py (100%) rename {examples/03_Tools_And_Function_Calling => src/llama_cpp_agent/tools}/web_search/default_web_search_providers.py (100%) rename examples/03_Tools_And_Function_Calling/web_search/web_search.py => src/llama_cpp_agent/tools/web_search/tool.py (64%) rename {examples/03_Tools_And_Function_Calling => src/llama_cpp_agent/tools}/web_search/web_search_interfaces.py (100%) diff --git a/examples/03_Tools_And_Function_Calling/web_search_agent.py b/examples/03_Tools_And_Function_Calling/web_search_agent.py new file mode 100644 index 0000000..854cf83 --- /dev/null +++ b/examples/03_Tools_And_Function_Calling/web_search_agent.py @@ -0,0 +1,45 @@ +from llama_cpp_agent import MessagesFormatterType, LlamaCppAgent +from llama_cpp_agent.llm_output_settings import LlmStructuredOutputSettings +from llama_cpp_agent.providers import LlamaCppServerProvider +from llama_cpp_agent.tools import WebSearchTool + +def send_message_to_user(message: str): + """ + Send a message to user. + Args: + message (str): Message to send. + """ + print(message) + + +provider = LlamaCppServerProvider("http://localhost:8080") +agent = LlamaCppAgent( + provider, + debug_output=True, + system_prompt="You are a helpful assistant. Use additional available information you have access to when giving a response. Always give detailed and long responses. Format your response, well structured in markdown format.", + predefined_messages_formatter_type=MessagesFormatterType.CHATML, + add_tools_and_structures_documentation_to_system_prompt=True, +) + +search_tool = WebSearchTool(provider, MessagesFormatterType.CHATML, 20000) + +settings = provider.get_provider_default_settings() + +settings.temperature = 0.65 +# settings.top_p = 0.85 +# settings.top_k = 60 +# settings.tfs_z = 0.95 +settings.max_tokens = 2048 +output_settings = LlmStructuredOutputSettings.from_functions( + [search_tool.get_tool(), send_message_to_user]) +user = input(">") +result = agent.get_chat_response(user, prompt_suffix="\n```json\n", + llm_sampling_settings=settings, structured_output_settings=output_settings) +while True: + if result[0]["function"] == "send_message_to_user": + user = input(">") + result = agent.get_chat_response(user, prompt_suffix="\n```json\n", structured_output_settings=output_settings, + llm_sampling_settings=settings) + else: + result = agent.get_chat_response(result[0]["return_value"], role=Roles.tool, prompt_suffix="\n```json\n", + structured_output_settings=output_settings, llm_sampling_settings=settings) \ No newline at end of file diff --git a/src/llama_cpp_agent/tools/__init__.py b/src/llama_cpp_agent/tools/__init__.py new file mode 100644 index 0000000..47704c4 --- /dev/null +++ b/src/llama_cpp_agent/tools/__init__.py @@ -0,0 +1 @@ +from web_search import WebSearchTool, WebSearchProvider, WebCrawler, TrafilaturaWebCrawler, DDGWebSearchProvider \ No newline at end of file diff --git a/src/llama_cpp_agent/tools/web_search/__init__.py b/src/llama_cpp_agent/tools/web_search/__init__.py new file mode 100644 index 0000000..9a41be9 --- /dev/null +++ b/src/llama_cpp_agent/tools/web_search/__init__.py @@ -0,0 +1,4 @@ +from tool import WebSearchTool +from web_search_interfaces import WebCrawler, WebSearchProvider +from default_web_crawlers import TrafilaturaWebCrawler +from default_web_search_providers import DDGWebSearchProvider diff --git a/examples/03_Tools_And_Function_Calling/web_search/default_web_crawlers.py b/src/llama_cpp_agent/tools/web_search/default_web_crawlers.py similarity index 100% rename from examples/03_Tools_And_Function_Calling/web_search/default_web_crawlers.py rename to src/llama_cpp_agent/tools/web_search/default_web_crawlers.py diff --git a/examples/03_Tools_And_Function_Calling/web_search/default_web_search_providers.py b/src/llama_cpp_agent/tools/web_search/default_web_search_providers.py similarity index 100% rename from examples/03_Tools_And_Function_Calling/web_search/default_web_search_providers.py rename to src/llama_cpp_agent/tools/web_search/default_web_search_providers.py diff --git a/examples/03_Tools_And_Function_Calling/web_search/web_search.py b/src/llama_cpp_agent/tools/web_search/tool.py similarity index 64% rename from examples/03_Tools_And_Function_Calling/web_search/web_search.py rename to src/llama_cpp_agent/tools/web_search/tool.py index 4ce2de3..d2d56bb 100644 --- a/examples/03_Tools_And_Function_Calling/web_search/web_search.py +++ b/src/llama_cpp_agent/tools/web_search/tool.py @@ -67,44 +67,4 @@ def get_tool(self): return self.search_web -def send_message_to_user(message: str): - """ - Send a message to user. - Args: - message (str): Message to send. - """ - print(message) - -provider = LlamaCppServerProvider("http://hades.hq.solidrust.net:8084") -#provider = LlamaCppServerProvider("http://localhost:8080") -agent = LlamaCppAgent( - provider, - debug_output=True, - system_prompt="You are a helpful assistant. Use additional available information you have access to when giving a response. Always give detailed and long responses. Format your response, well structured in markdown format.", - predefined_messages_formatter_type=MessagesFormatterType.CHATML, - add_tools_and_structures_documentation_to_system_prompt=True, -) - -search_tool = WebSearchTool(provider, MessagesFormatterType.CHATML, 20000) - -settings = provider.get_provider_default_settings() - -settings.temperature = 0.65 -# settings.top_p = 0.85 -# settings.top_k = 60 -# settings.tfs_z = 0.95 -settings.max_tokens = 2048 -output_settings = LlmStructuredOutputSettings.from_functions( - [search_tool.get_tool(), send_message_to_user]) -user = input(">") -result = agent.get_chat_response(user, prompt_suffix="\n```json\n", - llm_sampling_settings=settings, structured_output_settings=output_settings) -while True: - if result[0]["function"] == "send_message_to_user": - user = input(">") - result = agent.get_chat_response(user, prompt_suffix="\n```json\n", structured_output_settings=output_settings, - llm_sampling_settings=settings) - else: - result = agent.get_chat_response(result[0]["return_value"], role=Roles.tool, prompt_suffix="\n```json\n", - structured_output_settings=output_settings, llm_sampling_settings=settings) diff --git a/examples/03_Tools_And_Function_Calling/web_search/web_search_interfaces.py b/src/llama_cpp_agent/tools/web_search/web_search_interfaces.py similarity index 100% rename from examples/03_Tools_And_Function_Calling/web_search/web_search_interfaces.py rename to src/llama_cpp_agent/tools/web_search/web_search_interfaces.py