diff --git a/examples/03_Tools_And_Function_Calling/web_search_agent.py b/examples/03_Tools_And_Function_Calling/web_search_agent.py index 94b6221..3b0bb17 100644 --- a/examples/03_Tools_And_Function_Calling/web_search_agent.py +++ b/examples/03_Tools_And_Function_Calling/web_search_agent.py @@ -1,29 +1,28 @@ from llama_cpp_agent import MessagesFormatterType, LlamaCppAgent from llama_cpp_agent.chat_history.messages import Roles from llama_cpp_agent.llm_output_settings import LlmStructuredOutputSettings +from llama_cpp_agent.prompt_templates import web_search_system_prompt 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") +provider = LlamaCppServerProvider("http://hades.hq.solidrust.net:8084") 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, + system_prompt=web_search_system_prompt, + predefined_messages_formatter_type=MessagesFormatterType.MISTRAL, add_tools_and_structures_documentation_to_system_prompt=True, ) -search_tool = WebSearchTool(provider, MessagesFormatterType.CHATML, max_tokens_search_results=20000) + +def write_message_to_user(): + """ + Let you write a message to the user. + """ + return "Please write the message to the user." + + +search_tool = WebSearchTool(provider, MessagesFormatterType.MISTRAL, max_tokens_search_results=20000) settings = provider.get_provider_default_settings() @@ -33,15 +32,26 @@ def send_message_to_user(message: str): # 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) + [search_tool.get_tool(), write_message_to_user]) + + +def run_web_search_agent(): + user = input(">") + if user == "exit": + return + result = agent.get_chat_response(user, + llm_sampling_settings=settings, structured_output_settings=output_settings) + while True: + if result[0]["function"] == "write_message_to_user": + break + else: + result = agent.get_chat_response(result[0]["return_value"], role=Roles.tool, + structured_output_settings=output_settings, llm_sampling_settings=settings) + + result = agent.get_chat_response(result[0]["return_value"], role=Roles.tool, + llm_sampling_settings=settings) + + print(result) + run_web_search_agent() + +run_web_search_agent() \ No newline at end of file diff --git a/src/llama_cpp_agent/prompt_templates.py b/src/llama_cpp_agent/prompt_templates.py index 719c89e..22ed981 100644 --- a/src/llama_cpp_agent/prompt_templates.py +++ b/src/llama_cpp_agent/prompt_templates.py @@ -84,3 +84,21 @@ ## Output Models ''' + + +summarizing_system_prompt = """You are a text summarization and information extraction specialist and you are able to summarize and filter out information of websites relevant to a specific query. +Provide all the relevant information of the website in a structured markdown document following the format below: + +--- +Website Title: {Website Title} +Website URL: {Website URL} + +Content: +{Relevant Information} +--- + +Write only the markdown document in your response and begin and end your response with '---'. +""" + +web_search_system_prompt = """You are a web search specialist and you are able to give detailed answers to user queries based on information extracted from the web. +Write your response to the user in a structured markdown document.""" diff --git a/src/llama_cpp_agent/tools/web_search/tool.py b/src/llama_cpp_agent/tools/web_search/tool.py index 9d40a64..c8f19de 100644 --- a/src/llama_cpp_agent/tools/web_search/tool.py +++ b/src/llama_cpp_agent/tools/web_search/tool.py @@ -3,6 +3,7 @@ from .web_search_interfaces import WebCrawler, WebSearchProvider from .default_web_crawlers import TrafilaturaWebCrawler from .default_web_search_providers import DDGWebSearchProvider +from ...prompt_templates import summarizing_system_prompt class WebSearchTool: @@ -14,7 +15,7 @@ def __init__(self, llm_provider: LlmProvider, message_formatter_type: MessagesFo max_tokens_per_summary: int = 750): self.llm_provider = llm_provider self.summarising_agent = LlamaCppAgent(llm_provider, debug_output=True, - system_prompt="You are a text summarization and information extraction specialist and you are able to summarize and filter out information relevant to a specific query.", + system_prompt=summarizing_system_prompt, predefined_messages_formatter_type=message_formatter_type) if web_crawler is None: self.web_crawler = TrafilaturaWebCrawler() @@ -56,7 +57,7 @@ def search_web(self, search_query: str): web_info = self.summarising_agent.get_chat_response( f"Please summarize the following Website content and extract relevant information to this query:'{search_query}'.\n\n" + web_info, add_response_to_chat_history=False, add_message_to_chat_history=False, llm_sampling_settings=self.settings) - result_string += web_info + result_string += f"\n\n{web_info.strip()}" res = result_string.strip() tokens = self.llm_provider.tokenize(res) @@ -69,7 +70,7 @@ def search_web(self, search_query: str): else: remove_chars += 100 - return "Based on the following results, answer the previous user query:\nResults:\n\n" + res[:self.max_tokens_search_results] + return "\nResults of searching the web:\n\n" + res[:self.max_tokens_search_results] def get_tool(self): return self.search_web