Spaces:
Build error
Build error
Update app.py
Browse files20 unmodified lines212223242526189 unmodified lines21621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927020 unmodified linesfrom llama_cpp_agent.llm_output_settings import LlmStructuredOutputSettings, LlmStructuredOutputTypefrom pydantic import BaseModel, Fieldfrom llama_cpp_agent.llm_output_settings import LlmStructuredOutputTypeprint("Available LlmStructuredOutputType options:")for option in LlmStructuredOutputType:189 unmodified linesdef respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,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.MISTRALsearch_tool = WebSearchTool(llm_provider=model,message_formatter_type=chat_template,max_tokens_search_results=12000,max_tokens_per_summary=2048,)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 searchsearch_result = search_tool.run(message)outputs = ""# Generate responseresponse_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}Respond in a clear and concise manner, citing sources where appropriate."""response = model(response_prompt)outputs += response# Generate citationscitation_prompt = "Cite the sources you used in your response."citing_sources = model(citation_prompt)outputs += "\n\nSources:\n"outputs += citing_sourcesreturn outputs# Gradio interface20 unmodified lines212223242526272829303132333435189 unmodified lines22522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733820 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 Listprint("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,):global llmglobal llm_modelchat_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 = modelprovider = LlamaCppPythonProvider(llm)logging.info(f"Loaded chat examples: {chat_template}")search_tool = WebSearchTool(llm_provider=provider,message_formatter_type=chat_template,max_tokens_search_results=12000,max_tokens_per_summary=2048,)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 = Falsesettings.temperature = temperaturesettings.top_k = top_ksettings.top_p = top_psettings.max_tokens = max_tokenssettings.repeat_penalty = repeat_penaltyoutput_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)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 = Trueresponse_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,)for text in response_text:outputs += textyield outputsoutput_settings = LlmStructuredOutputSettings.from_pydantic_models([CitingSources], LlmStructuredOutputType.object_instance)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,)outputs += "\n\nSources:\n"outputs += "\n".join(citing_sources.sources)yield outputs# Gradio interface
app.py+91-23