Update app.py
Browse files2 unmodified lines345678911 unmodified lines212223242526279 unmodified lines3738394041423 unmodified lines464748495051525354555657585960616231 unmodified lines94959697989910014 unmodified lines11511611711811912012117 unmodified lines13914014114214314414514614714814915018 unmodified lines16917017117217317417531 unmodified lines2072082092102112122132 unmodified linesimport gradio as grfrom transformers import pipelinefrom llama_cpp_agent.providers.llama_cpp_endpoint_provider import LlamaCppEndpointSettingsfrom llama_cpp_agent import LlamaCppAgent, MessagesFormatterTypefrom llama_cpp_agent.chat_history import BasicChatHistoryfrom llama_cpp_agent.chat_history.messages import Roles11 unmodified linesfrom langchain_community.llms import HuggingFaceHubhuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")examples = [["latest news about Yann LeCun"],["Latest news site:github.blog"],9 unmodified linesdef get_messages_formatter_type(model_name):if model_name is None:logging.warning("Model name is None. Defaulting to CHATML formatter.")return MessagesFormatterType.CHATML3 unmodified lineselse:return MessagesFormatterType.CHATMLclass HuggingFaceHubProvider(LlamaCppEndpointSettings):def __init__(self, model):self.model = modeldef create_completion(self, prompt, **kwargs):response = self.model(prompt)return {'choices': [{'text': response['generated_text']}]}def get_provider_default_settings(self):return self.model.model_kwargsdef get_model(temperature, top_p, repetition_penalty):return HuggingFaceHub(repo_id="mistralai/Mistral-7B-Instruct-v0.3",31 unmodified linesdef write_message_to_user():return "Please write the message to the user."#@spaces.GPU(duration=120)def respond(message,history: list[tuple[str, str]],14 unmodified lines# Create a new model instance for each requestllm = get_model(temperature, top_p, repeat_penalty)provider = HuggingFaceHubProvider(llm)logging.info(f"Loaded chat examples: {chat_template}")search_tool = WebSearchTool(llm_provider=provider,17 unmodified lines)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()]18 unmodified linesoutputs = ""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"],31 unmodified linesgr.Dropdown(['Mistral-7B-Instruct-v0.3'],value="Mistral-7B-Instruct-v0.3", # This should match exactlylabel="Model"),gr.Textbox(value=web_search_system_prompt, label="System message"),2 unmodified lines345678911 unmodified lines2122232425269 unmodified lines363738394041423 unmodified lines46474849505131 unmodified lines83848586878814 unmodified lines10310410510610710810917 unmodified lines12712812913013113213313413513613713818 unmodified lines15715815916016116216331 unmodified lines1951961971981992002012 unmodified linesimport gradio as grfrom transformers import pipelinefrom llama_cpp_agent.providers import LlamaCppPythonProviderfrom llama_cpp_agent import LlamaCppAgent, MessagesFormatterTypefrom llama_cpp_agent.chat_history import BasicChatHistoryfrom llama_cpp_agent.chat_history.messages import Roles11 unmodified linesfrom langchain_community.llms import HuggingFaceHubhuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")examples = [["latest news about Yann LeCun"],["Latest news site:github.blog"],9 unmodified linesdef get_messages_formatter_type(model_name):if model_name is None:# Handle the case where model_name is Nonelogging.warning("Model name is None. Defaulting to CHATML formatter.")return MessagesFormatterType.CHATML3 unmodified lineselse:return MessagesFormatterType.CHATMLdef get_model(temperature, top_p, repetition_penalty):return HuggingFaceHub(repo_id="mistralai/Mistral-7B-Instruct-v0.3",31 unmodified linesdef write_message_to_user():return "Please write the message to the user."def respond(message,history: list[tuple[str, str]],14 unmodified lines# Create a new model instance for each requestllm = get_model(temperature, top_p, repeat_penalty)provider = LlamaCppPythonProvider(llm)logging.info(f"Loaded chat examples: {chat_template}")search_tool = WebSearchTool(llm_provider=provider,17 unmodified lines)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()]18 unmodified linesoutputs = ""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"],31 unmodified linesgr.Dropdown(['Mistral-7B-Instruct-v0.3'],value="Mistral-7B-Instruct-v0.3", # Ensure this matches exactly with the option in the listlabel="Model"),gr.Textbox(value=web_search_system_prompt, label="System message"),
app.py+11-23