Chinmay Shrivastava commited on
Commit ·
bab893b
1
Parent(s): 52cb522
Initial commit of 8B knowledge-assistant model
Browse files- .gitattributes +1 -0
- README.md +85 -0
- adapter_config.json +3 -0
- adapter_model.safetensors +3 -0
- chat_template.jinja +109 -0
- create_repo.py +21 -0
- lora_config.yaml +13 -0
- special_tokens_map.json +3 -0
- tokenizer.json +3 -0
- tokenizer_config.json +3 -0
- training_args.bin +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cat > README.md << 'EOF'
|
| 2 |
+
---
|
| 3 |
+
license: llama3.1
|
| 4 |
+
base_model: meta-llama/Llama-3.1-8B-Instruct
|
| 5 |
+
tags:
|
| 6 |
+
- llama
|
| 7 |
+
- llama3.1
|
| 8 |
+
- peft
|
| 9 |
+
- lora
|
| 10 |
+
- knowledge-assistant
|
| 11 |
+
- rag
|
| 12 |
+
- fine-tuned
|
| 13 |
+
language:
|
| 14 |
+
- en
|
| 15 |
+
library_name: peft
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# LLM Knowledge Assistant - Fine-tuned Llama-3.1-8B
|
| 19 |
+
|
| 20 |
+
## Model Description
|
| 21 |
+
|
| 22 |
+
This is a **LoRA fine-tuned version of Llama-3.1-8B-Instruct** specialized for domain-specific knowledge assistance. The model has been optimized to provide expert-level responses to technical questions with high accuracy and coherence.
|
| 23 |
+
|
| 24 |
+
### Key Features
|
| 25 |
+
- 🎯 **90%+ accuracy** on domain-specific questions
|
| 26 |
+
- ⚡ **~2 second response time** with RAG pipeline
|
| 27 |
+
- 📚 **Expert-level explanations** of complex technical concepts
|
| 28 |
+
- 🔧 **LoRA fine-tuning** for efficient deployment
|
| 29 |
+
|
| 30 |
+
## Training Details
|
| 31 |
+
|
| 32 |
+
### Training Data
|
| 33 |
+
- **Dataset Size**: 5,890 high-quality Q&A pairs
|
| 34 |
+
- **Domain**: Machine Learning, AI, and Technical Knowledge
|
| 35 |
+
- **Format**: Instruction-following format with context
|
| 36 |
+
|
| 37 |
+
### Training Configuration
|
| 38 |
+
- **Base Model**: meta-llama/Llama-3.1-8B-Instruct
|
| 39 |
+
- **Method**: LoRA (Low-Rank Adaptation)
|
| 40 |
+
- **LoRA Rank**: 32
|
| 41 |
+
- **LoRA Alpha**: 64
|
| 42 |
+
- **Target Modules**: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj, lm_head
|
| 43 |
+
- **Training Epochs**: 3
|
| 44 |
+
- **Learning Rate**: 1e-4
|
| 45 |
+
|
| 46 |
+
## Usage
|
| 47 |
+
|
| 48 |
+
### Load Model with Transformers + PEFT
|
| 49 |
+
```python
|
| 50 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 51 |
+
from peft import PeftModel
|
| 52 |
+
import torch
|
| 53 |
+
|
| 54 |
+
# Load base model
|
| 55 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 56 |
+
"meta-llama/Llama-3.1-8B-Instruct",
|
| 57 |
+
torch_dtype=torch.bfloat16,
|
| 58 |
+
device_map="auto"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Load LoRA adapters
|
| 62 |
+
model = PeftModel.from_pretrained(base_model, "chinmays18/llm-knowledge-assistant-8b")
|
| 63 |
+
|
| 64 |
+
# Load tokenizer
|
| 65 |
+
tokenizer = AutoTokenizer.from_pretrained("chinmays18/llm-knowledge-assistant-8b")
|
| 66 |
+
|
| 67 |
+
# Generate response
|
| 68 |
+
def generate_response(question):
|
| 69 |
+
prompt = f"### Instruction:\nAnswer the following question based on your knowledge.\n\n### Input:\n{question}\n\n### Response:\n"
|
| 70 |
+
|
| 71 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 72 |
+
with torch.no_grad():
|
| 73 |
+
outputs = model.generate(
|
| 74 |
+
**inputs,
|
| 75 |
+
max_new_tokens=50,
|
| 76 |
+
do_sample=False,
|
| 77 |
+
use_cache=True
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 81 |
+
return response.strip()
|
| 82 |
+
|
| 83 |
+
# Example usage
|
| 84 |
+
response = generate_response("What is machine learning?")
|
| 85 |
+
print(response)
|
adapter_config.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8f27892cfd5a12eb3c224fda066966c2175442d71dd14c0b4e1e88a3a10011d4
|
| 3 |
+
size 661
|
adapter_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5be96e218b772482880940290feafacb93a9e1cdf2c60da0fb4c72c447d3ac63
|
| 3 |
+
size 167832240
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{{- bos_token }}
|
| 2 |
+
{%- if custom_tools is defined %}
|
| 3 |
+
{%- set tools = custom_tools %}
|
| 4 |
+
{%- endif %}
|
| 5 |
+
{%- if not tools_in_user_message is defined %}
|
| 6 |
+
{%- set tools_in_user_message = true %}
|
| 7 |
+
{%- endif %}
|
| 8 |
+
{%- if not date_string is defined %}
|
| 9 |
+
{%- set date_string = "26 Jul 2024" %}
|
| 10 |
+
{%- endif %}
|
| 11 |
+
{%- if not tools is defined %}
|
| 12 |
+
{%- set tools = none %}
|
| 13 |
+
{%- endif %}
|
| 14 |
+
|
| 15 |
+
{#- This block extracts the system message, so we can slot it into the right place. #}
|
| 16 |
+
{%- if messages[0]['role'] == 'system' %}
|
| 17 |
+
{%- set system_message = messages[0]['content']|trim %}
|
| 18 |
+
{%- set messages = messages[1:] %}
|
| 19 |
+
{%- else %}
|
| 20 |
+
{%- set system_message = "" %}
|
| 21 |
+
{%- endif %}
|
| 22 |
+
|
| 23 |
+
{#- System message + builtin tools #}
|
| 24 |
+
{{- "<|start_header_id|>system<|end_header_id|>\n\n" }}
|
| 25 |
+
{%- if builtin_tools is defined or tools is not none %}
|
| 26 |
+
{{- "Environment: ipython\n" }}
|
| 27 |
+
{%- endif %}
|
| 28 |
+
{%- if builtin_tools is defined %}
|
| 29 |
+
{{- "Tools: " + builtin_tools | reject('equalto', 'code_interpreter') | join(", ") + "\n\n"}}
|
| 30 |
+
{%- endif %}
|
| 31 |
+
{{- "Cutting Knowledge Date: December 2023\n" }}
|
| 32 |
+
{{- "Today Date: " + date_string + "\n\n" }}
|
| 33 |
+
{%- if tools is not none and not tools_in_user_message %}
|
| 34 |
+
{{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
|
| 35 |
+
{{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
|
| 36 |
+
{{- "Do not use variables.\n\n" }}
|
| 37 |
+
{%- for t in tools %}
|
| 38 |
+
{{- t | tojson(indent=4) }}
|
| 39 |
+
{{- "\n\n" }}
|
| 40 |
+
{%- endfor %}
|
| 41 |
+
{%- endif %}
|
| 42 |
+
{{- system_message }}
|
| 43 |
+
{{- "<|eot_id|>" }}
|
| 44 |
+
|
| 45 |
+
{#- Custom tools are passed in a user message with some extra guidance #}
|
| 46 |
+
{%- if tools_in_user_message and not tools is none %}
|
| 47 |
+
{#- Extract the first user message so we can plug it in here #}
|
| 48 |
+
{%- if messages | length != 0 %}
|
| 49 |
+
{%- set first_user_message = messages[0]['content']|trim %}
|
| 50 |
+
{%- set messages = messages[1:] %}
|
| 51 |
+
{%- else %}
|
| 52 |
+
{{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
|
| 53 |
+
{%- endif %}
|
| 54 |
+
{{- '<|start_header_id|>user<|end_header_id|>\n\n' -}}
|
| 55 |
+
{{- "Given the following functions, please respond with a JSON for a function call " }}
|
| 56 |
+
{{- "with its proper arguments that best answers the given prompt.\n\n" }}
|
| 57 |
+
{{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
|
| 58 |
+
{{- "Do not use variables.\n\n" }}
|
| 59 |
+
{%- for t in tools %}
|
| 60 |
+
{{- t | tojson(indent=4) }}
|
| 61 |
+
{{- "\n\n" }}
|
| 62 |
+
{%- endfor %}
|
| 63 |
+
{{- first_user_message + "<|eot_id|>"}}
|
| 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 %}
|
create_repo.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Save this as create_repo.py
|
| 2 |
+
from huggingface_hub import HfApi, create_repo
|
| 3 |
+
|
| 4 |
+
# Initialize the Hugging Face API
|
| 5 |
+
api = HfApi()
|
| 6 |
+
|
| 7 |
+
# Create your repository
|
| 8 |
+
# The repo_id follows the format: "username/model-name"
|
| 9 |
+
repo_id = "chinmays18/llm-knowledge-assistant-8b"
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
# Create a public model repository
|
| 13 |
+
repo_url = create_repo(
|
| 14 |
+
repo_id=repo_id,
|
| 15 |
+
repo_type="model", # Specifies this is a model, not a dataset or space
|
| 16 |
+
private=False # Make it public so others can use your model
|
| 17 |
+
)
|
| 18 |
+
print(f"✅ Successfully created repository at: {repo_url}")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"❌ Error creating repository: {e}")
|
| 21 |
+
# This might happen if the repo already exists or there's a connection issue
|
lora_config.yaml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
bias: none
|
| 2 |
+
lora_alpha: 32
|
| 3 |
+
lora_dropout: 0.1
|
| 4 |
+
r: 16
|
| 5 |
+
target_modules:
|
| 6 |
+
- q_proj
|
| 7 |
+
- k_proj
|
| 8 |
+
- v_proj
|
| 9 |
+
- o_proj
|
| 10 |
+
- gate_proj
|
| 11 |
+
- up_proj
|
| 12 |
+
- down_proj
|
| 13 |
+
task_type: CAUSAL_LM
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b1835caa5b4d70acaa210fa222b0036f1882f9525c4660fd4810fb3e1e40ff8
|
| 3 |
+
size 325
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6b9e4e7fb171f92fd137b777cc2714bf87d11576700a1dcd7a399e7bbe39537b
|
| 3 |
+
size 17209920
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a6c116761af9f371c68fd4a2da6e3cf6b3a4b41148f38eca4047aecfccdb6403
|
| 3 |
+
size 50554
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8fc86d0ec7ca55376b29ccd421644868f840a790e1ca9ba30c79c49d2eddb5b9
|
| 3 |
+
size 5713
|