Update app.py
Browse files20 unmodified lines212223242526272829303132333435189 unmodified lines22522622722822923023123223323422422523522722822923623723823924024124224324424524624724824925025125225325423125525625725825926023726126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229324524629429529629729829930030130230330430530630730830931031131231331425025125225331531631731825531932032132225725832332432532632732832933033133226026126226333326526626733433533633733820 unmodified linesfrom llama_cpp_agent.llm_output_settings import LlmStructuredOutputSettings, LlmStructuredOutputTypefrom pydantic import BaseModel, Fieldfrom llama_cpp_agent.llm_output_settings import LlmStructuredOutputTypefrom llama_cpp import Llamafrom llama_cpp_agent import LlamaCppPythonProvider, LlamaCppAgentfrom llama_cpp_agent.chat_history import BasicChatHistoryfrom llama_cpp_agent.chat_history.messages import Rolesfrom llama_cpp_agent.llm_output_settings import LlmStructuredOutputSettings, LlmStructuredOutputTypefrom llama_cpp_agent.tools import WebSearchToolfrom llama_cpp_agent.prompt_templates import web_search_system_prompt, research_system_promptfrom pydantic import BaseModel, Fieldfrom typing import List
print("Available LlmStructuredOutputType options:")for option in LlmStructuredOutputType:189 unmodified linesdef respond( message, history: list[tuple[str, str]], model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty, top_k=50, max_tokens_per_summary=2048): model = get_model(temperature, top_p, repeat_penalty, top_k, max_tokens, max_tokens_per_summary) chat_template = MessagesFormatterType.MISTRAL global llm global llm_model
chat_template = get_messages_formatter_type(model)
if llm is None or llm_model != model: llm = Llama( model_path=f"models/{model}", flash_attn=True, n_gpu_layers=81, n_batch=1024, n_ctx=get_context_by_model(model), ) llm_model = model
provider = LlamaCppPythonProvider(llm) logging.info(f"Loaded chat examples: {chat_template}")
search_tool = WebSearchTool( llm_provider=model, llm_provider=provider, message_formatter_type=chat_template, max_tokens_search_results=12000, max_tokens_per_summary=2048, )
messages = BasicChatHistory() web_search_agent = LlamaCppAgent( provider, system_prompt=web_search_system_prompt, predefined_messages_formatter_type=chat_template, debug_output=True, )
answer_agent = LlamaCppAgent( provider, system_prompt=research_system_prompt, predefined_messages_formatter_type=chat_template, debug_output=True, )
settings = provider.get_provider_default_settings() settings.stream = False settings.temperature = temperature settings.top_k = top_k settings.top_p = top_p settings.max_tokens = max_tokens settings.repeat_penalty = repeat_penalty
output_settings = LlmStructuredOutputSettings.from_functions( [search_tool.get_tool()] )
messages = BasicChatHistory() for msn in history: user = {"role": Roles.user, "content": msn[0]} assistant = {"role": Roles.assistant, "content": msn[1]} messages.add_message(user) messages.add_message(assistant)
# Perform web search search_result = search_tool.run(message) result = web_search_agent.get_chat_response( message, llm_sampling_settings=settings, structured_output_settings=output_settings, add_message_to_chat_history=False, add_response_to_chat_history=False, print_output=False, )
outputs = "" settings.stream = True
response_text = answer_agent.get_chat_response( f"Write a detailed and complete research document that fulfills the following user request: '{message}', based on the information from the web below.\n\n" + result[0]["return_value"], role=Roles.tool, llm_sampling_settings=settings, chat_history=messages, returns_streaming_generator=True, print_output=False, )
# Generate response response_prompt = f"""Write a detailed and complete research document that fulfills the following user request: '{message}', based on the information from the web below.
{search_result} for text in response_text: outputs += text yield outputs
Respond in a clear and concise manner, citing sources where appropriate.""" output_settings = LlmStructuredOutputSettings.from_pydantic_models( [CitingSources], LlmStructuredOutputType.object_instance )
response = model(response_prompt) outputs += response citing_sources = answer_agent.get_chat_response( "Cite the sources you used in your response.", role=Roles.tool, llm_sampling_settings=settings, chat_history=messages, returns_streaming_generator=False, structured_output_settings=output_settings, print_output=False, )
# Generate citations citation_prompt = "Cite the sources you used in your response." citing_sources = model(citation_prompt) outputs += "\n\nSources:\n" outputs += citing_sources
return outputs outputs += "\n".join(citing_sources.sources) yield outputs
# Gradio interfaceapp.py+91-23