Skip to content

Commit

Permalink
Edited summarizer prompt and web search agent prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian-Winter committed May 26, 2024
1 parent c622e50 commit 212c2ca
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
62 changes: 36 additions & 26 deletions examples/03_Tools_And_Function_Calling/web_search_agent.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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()
18 changes: 18 additions & 0 deletions src/llama_cpp_agent/prompt_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
7 changes: 4 additions & 3 deletions src/llama_cpp_agent/tools/web_search/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 212c2ca

Please sign in to comment.