Spaces:
Sleeping
Sleeping
Mimi
commited on
Commit
Β·
39be96e
1
Parent(s):
a7eb66f
fix build
Browse files- Dockerfile +1 -2
- README.md +14 -2
- agent.py +153 -0
Dockerfile
CHANGED
|
@@ -15,8 +15,7 @@ RUN apt-get update && \
|
|
| 15 |
xargs -r -a /app/packages.txt apt-get install -y && \
|
| 16 |
rm -rf /var/lib/apt/lists/* && \
|
| 17 |
pip install --no-cache-dir -r $HOME/app/requirements.txt && \
|
| 18 |
-
pip install llama-cpp-python
|
| 19 |
-
--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
|
| 20 |
|
| 21 |
EXPOSE 7860
|
| 22 |
CMD streamlit run app.py \
|
|
|
|
| 15 |
xargs -r -a /app/packages.txt apt-get install -y && \
|
| 16 |
rm -rf /var/lib/apt/lists/* && \
|
| 17 |
pip install --no-cache-dir -r $HOME/app/requirements.txt && \
|
| 18 |
+
pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
|
|
|
|
| 19 |
|
| 20 |
EXPOSE 7860
|
| 21 |
CMD streamlit run app.py \
|
README.md
CHANGED
|
@@ -1,11 +1,23 @@
|
|
| 1 |
---
|
| 2 |
title: Naomi
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
title: Naomi
|
| 3 |
+
emoji: π§π»
|
| 4 |
+
colorFrom: pink
|
| 5 |
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
| 9 |
+
hf_oath: true
|
| 10 |
+
|
| 11 |
+
models:
|
| 12 |
+
- bartowski/Meta-Llama-3.1-8B-Instruct-GGUF Meta-Llama-3.1-8B-Instruct-Q3_K_XL.gguf
|
| 13 |
+
tags:
|
| 14 |
+
- text
|
| 15 |
+
- llm
|
| 16 |
+
- meta
|
| 17 |
+
- instruct
|
| 18 |
+
preload_from_hub:
|
| 19 |
+
- bartowski/Meta-Llama-3.1-8B-Instruct-GGUF Meta-Llama-3.1-8B-Instruct-Q3_K_XL.gguf
|
| 20 |
+
|
| 21 |
---
|
| 22 |
|
| 23 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
agent.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This script defines the Naomi class, which utilizes the Llama model for chatbot interactions.
|
| 3 |
+
It includes methods for responding to user input while maintaining a chat history.
|
| 4 |
+
|
| 5 |
+
Keyword arguments:
|
| 6 |
+
- kwargs: Additional keyword arguments for candidate information.
|
| 7 |
+
|
| 8 |
+
Return:
|
| 9 |
+
- An instance of the Naomi class, capable of handling chatbot interactions.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
import time
|
| 14 |
+
from uuid import uuid4
|
| 15 |
+
from llama_cpp import Llama
|
| 16 |
+
from llama_cpp.llama_tokenizer import LlamaHFTokenizer
|
| 17 |
+
|
| 18 |
+
# default decoding params initiation
|
| 19 |
+
SEED = 42
|
| 20 |
+
|
| 21 |
+
MODEL_CARD = "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF"
|
| 22 |
+
MODEL_PATH = "Meta-Llama-3.1-8B-Instruct-Q3_K_XL.gguf"
|
| 23 |
+
base_model_id = "meta-llama/Llama-3.1-8B-Instruct"
|
| 24 |
+
|
| 25 |
+
new_chat_template = """{{- bos_token }}
|
| 26 |
+
{%- if custom_tools is defined %}
|
| 27 |
+
{%- set tools = custom_tools %}
|
| 28 |
+
{%- endif %}
|
| 29 |
+
{%- if not tools_in_user_message is defined %}
|
| 30 |
+
{%- set tools_in_user_message = true %}
|
| 31 |
+
{%- endif %}
|
| 32 |
+
{%- if not date_string is defined %}
|
| 33 |
+
{%- set date_string = "26 Jul 2024" %}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- if not tools is defined %}
|
| 36 |
+
{%- set tools = none %}
|
| 37 |
+
{%- endif %}
|
| 38 |
+
|
| 39 |
+
{#- This block extracts the system message, so we can slot it into the right place. #}
|
| 40 |
+
{%- if messages[0]['role'] == 'system' %}
|
| 41 |
+
{%- set system_message = messages[0]['content']|trim %}
|
| 42 |
+
{%- set messages = messages[1:] %}
|
| 43 |
+
{#- System message + builtin tools #}
|
| 44 |
+
{{- "<|start_header_id|>system<|end_header_id|>\n\n" }}
|
| 45 |
+
{%- if builtin_tools is defined or tools is not none %}
|
| 46 |
+
{{- "Environment: ipython\n" }}
|
| 47 |
+
{%- endif %}
|
| 48 |
+
{%- if builtin_tools is defined %}
|
| 49 |
+
{{- "Tools: " + builtin_tools | reject('equalto', 'code_interpreter') | join(", ") + "\n\n"}}
|
| 50 |
+
{%- endif %}
|
| 51 |
+
{%- if tools is not none and not tools_in_user_message %}
|
| 52 |
+
{{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
|
| 53 |
+
{{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
|
| 54 |
+
{{- "Do not use variables.\n\n" }}
|
| 55 |
+
{%- for t in tools %}
|
| 56 |
+
{{- t | tojson(indent=4) }}
|
| 57 |
+
{{- "\n\n" }}
|
| 58 |
+
{%- endfor %}
|
| 59 |
+
{%- endif %}
|
| 60 |
+
{{- system_message }}
|
| 61 |
+
{{- "<|eot_id|>" }}
|
| 62 |
+
{%- else %}
|
| 63 |
+
{%- set system_message = "" %}
|
| 64 |
+
{%- endif %}
|
| 65 |
+
|
| 66 |
+
{%- for message in messages %}
|
| 67 |
+
{%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
|
| 68 |
+
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' }}
|
| 69 |
+
{%- elif 'tool_calls' in message %}
|
| 70 |
+
{%- if not message.tool_calls|length == 1 %}
|
| 71 |
+
{{- raise_exception("This model only supports single tool-calls at once!") }}
|
| 72 |
+
{%- endif %}
|
| 73 |
+
{%- set tool_call = message.tool_calls[0].function %}
|
| 74 |
+
{%- if builtin_tools is defined and tool_call.name in builtin_tools %}
|
| 75 |
+
{{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
|
| 76 |
+
{{- "<|python_tag|>" + tool_call.name + ".call(" }}
|
| 77 |
+
{%- for arg_name, arg_val in tool_call.arguments | items %}
|
| 78 |
+
{{- arg_name + '="' + arg_val + '"' }}
|
| 79 |
+
{%- if not loop.last %}
|
| 80 |
+
{{- ", " }}
|
| 81 |
+
{%- endif %}
|
| 82 |
+
{%- endfor %}
|
| 83 |
+
{{- ")" }}
|
| 84 |
+
{%- else %}
|
| 85 |
+
{{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
|
| 86 |
+
{{- '{"name": "' + tool_call.name + '", ' }}
|
| 87 |
+
{{- '"parameters": ' }}
|
| 88 |
+
{{- tool_call.arguments | tojson }}
|
| 89 |
+
{{- "}" }}
|
| 90 |
+
{%- endif %}
|
| 91 |
+
{%- if builtin_tools is defined %}
|
| 92 |
+
{#- This means we're in ipython mode #}
|
| 93 |
+
{{- "<|eom_id|>" }}
|
| 94 |
+
{%- else %}
|
| 95 |
+
{{- "<|eot_id|>" }}
|
| 96 |
+
{%- endif %}
|
| 97 |
+
{%- elif message.role == "tool" or message.role == "ipython" %}
|
| 98 |
+
{{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }}
|
| 99 |
+
{%- if message.content is mapping or message.content is iterable %}
|
| 100 |
+
{{- message.content | tojson }}
|
| 101 |
+
{%- else %}
|
| 102 |
+
{{- message.content }}
|
| 103 |
+
{%- endif %}
|
| 104 |
+
{{- "<|eot_id|>" }}
|
| 105 |
+
{%- endif %}
|
| 106 |
+
{%- endfor %}
|
| 107 |
+
{%- if add_generation_prompt %}
|
| 108 |
+
{{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }}
|
| 109 |
+
{%- endif %}"""
|
| 110 |
+
|
| 111 |
+
datetime_format = '%Y-%m-%d %H:%M:%S'
|
| 112 |
+
|
| 113 |
+
from datetime import datetime
|
| 114 |
+
|
| 115 |
+
class Naomi:
|
| 116 |
+
def __init__(self, **kwargs):
|
| 117 |
+
self.session_id = uuid4().hex
|
| 118 |
+
self.candidate = kwargs
|
| 119 |
+
|
| 120 |
+
# load the model
|
| 121 |
+
self.model = Llama.from_pretrained(
|
| 122 |
+
repo_id=MODEL_CARD,
|
| 123 |
+
filename=MODEL_PATH,
|
| 124 |
+
tokenizer=LlamaHFTokenizer.from_pretrained(base_model_id)
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
self.model.tokenizer_.hf_tokenizer.chat_template = new_chat_template
|
| 128 |
+
# load the agents prompts
|
| 129 |
+
self.chat_history = self.model.tokenizer_.hf_tokenizer.apply_chat_template(
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
self.timestamps = []
|
| 133 |
+
|
| 134 |
+
def invoke(self, history, **kwargs):
|
| 135 |
+
""" Invoked during stream. """
|
| 136 |
+
# user msg handling
|
| 137 |
+
self.timestamps += [datetime.now().strftime(datetime_format)]
|
| 138 |
+
format_user_input = self.model.tokenizer_.hf_tokenizer.apply_chat_template(history[-1], tokenize=False, add_generation_prompt=False)
|
| 139 |
+
self.chat_history += format_user_input
|
| 140 |
+
# agent msg results + clean
|
| 141 |
+
response = self.model(self.chat_history, **kwargs)
|
| 142 |
+
output = "".join(response['choices'][0]['text'].split('\n\n')[1:])
|
| 143 |
+
# update history
|
| 144 |
+
self.timestamps += [datetime.now().strftime(datetime_format)]
|
| 145 |
+
self.chat_history += self.model.tokenizer_.hf_tokenizer.apply_chat_template([{'role': 'assistant', 'content': output}], tokenize=False, add_generation_prompt=False)
|
| 146 |
+
return output
|
| 147 |
+
|
| 148 |
+
def respond(self, history, **kwargs):
|
| 149 |
+
""" Generator that yields responses in chat sessions. """
|
| 150 |
+
response = self.invoke(history, **kwargs)
|
| 151 |
+
for word in response.split():
|
| 152 |
+
yield word + " "
|
| 153 |
+
time.sleep(0.05)
|