|
| 1 | +import datetime |
| 2 | + |
| 3 | +from llama_cpp_agent import LlamaCppAgent |
| 4 | +from llama_cpp_agent import MessagesFormatterType |
| 5 | +from llama_cpp_agent.agent_memory.event_memory import Event |
| 6 | +from llama_cpp_agent.chat_history.messages import Roles |
| 7 | +from llama_cpp_agent.llm_agent import SystemPromptModule, SystemPromptModulePosition |
| 8 | +from llama_cpp_agent.providers import LlamaCppServerProvider |
| 9 | +from memory import output_settings, agent_core_memory, agent_retrieval_memory, agent_event_memory, update_memory_section |
| 10 | +from prompts import assistant_prompt, memory_prompt, wrap_function_response_in_xml_tags_json_mode, \ |
| 11 | + generate_write_message, generate_write_message_with_examples, wrap_user_message_in_xml_tags_json_mode |
| 12 | + |
| 13 | +provider = LlamaCppServerProvider("http://localhost:8080") |
| 14 | + |
| 15 | +agent = LlamaCppAgent( |
| 16 | + provider, |
| 17 | + system_prompt=assistant_prompt, |
| 18 | + debug_output=True, |
| 19 | + predefined_messages_formatter_type=MessagesFormatterType.MISTRAL, |
| 20 | +) |
| 21 | + |
| 22 | +settings = provider.get_provider_default_settings() |
| 23 | +settings.n_predict = 1024 |
| 24 | +settings.temperature = 0.35 |
| 25 | +settings.top_k = 0 |
| 26 | +settings.top_p = 0.5 |
| 27 | + |
| 28 | +memory_section = SystemPromptModule("memory", |
| 29 | + "The following section shows the count of memories in archival memory and chat history memory and the current content of your core memory:") |
| 30 | +date_time_section = SystemPromptModule("current_date_time", "The following section shows the current date and time:") |
| 31 | + |
| 32 | +memory_intro_section = SystemPromptModule("memory_intro", |
| 33 | + "To support you in your task as a game master and to help you remembering things, you have access to 3 different types of memory.", |
| 34 | + position=SystemPromptModulePosition.after_system_instructions) |
| 35 | +memory_intro_section.set_content(memory_prompt) |
| 36 | +output_settings.output_structured_output_and_raw_json_string = True |
| 37 | +while True: |
| 38 | + user_input = input(">") |
| 39 | + if user_input == "exit": |
| 40 | + break |
| 41 | + update_memory_section(memory_section) |
| 42 | + date_time_section.set_content(datetime.datetime.now().strftime("%d.%m.%Y") + "\nFormat: dd.mm.yyyy") |
| 43 | + |
| 44 | + agent_event_memory.add_event(Roles.user, wrap_user_message_in_xml_tags_json_mode(user_input)) |
| 45 | + agent_output, json_output = agent.get_chat_response( |
| 46 | + chat_history=agent_event_memory.get_event_memory_manager().build_chat_history(), |
| 47 | + llm_sampling_settings=settings, |
| 48 | + system_prompt_modules=[memory_intro_section, memory_section, date_time_section], |
| 49 | + structured_output_settings=output_settings) |
| 50 | + |
| 51 | + agent_event_memory.add_event(Roles.assistant, json_output) |
| 52 | + while True: |
| 53 | + update_memory_section(memory_section) |
| 54 | + date_time_section.set_content(datetime.datetime.now().strftime("%d.%m.%Y") + "\nFormat: dd.mm.yyyy") |
| 55 | + |
| 56 | + if agent_output[0]["function"] == "write_message_to_user": |
| 57 | + agent_event_memory.add_event(Roles.tool, generate_write_message()) |
| 58 | + output = agent.get_chat_response( |
| 59 | + chat_history=agent_event_memory.get_event_memory_manager().build_chat_history(), |
| 60 | + add_message_to_chat_history=False, add_response_to_chat_history=False, |
| 61 | + system_prompt_modules=[memory_intro_section, memory_section, date_time_section], |
| 62 | + llm_sampling_settings=settings) |
| 63 | + agent_event_memory.add_event(Roles.assistant, output) |
| 64 | + print(output) |
| 65 | + break |
| 66 | + |
| 67 | + agent_event_memory.add_event(Roles.tool, wrap_function_response_in_xml_tags_json_mode( |
| 68 | + agent_output[0]["return_value"])) |
| 69 | + agent_output, json_output = agent.get_chat_response( |
| 70 | + chat_history=agent_event_memory.get_event_memory_manager().build_chat_history(), |
| 71 | + llm_sampling_settings=settings, |
| 72 | + system_prompt_modules=[memory_intro_section, memory_section, |
| 73 | + date_time_section], |
| 74 | + structured_output_settings=output_settings) |
| 75 | + agent_event_memory.add_event(Roles.assistant, json_output) |
0 commit comments