Instructions to use BrandonZYW/opt-2.7b-InBedder with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use BrandonZYW/opt-2.7b-InBedder with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="BrandonZYW/opt-2.7b-InBedder")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("BrandonZYW/opt-2.7b-InBedder") model = AutoModelForCausalLM.from_pretrained("BrandonZYW/opt-2.7b-InBedder") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use BrandonZYW/opt-2.7b-InBedder with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "BrandonZYW/opt-2.7b-InBedder" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "BrandonZYW/opt-2.7b-InBedder", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/BrandonZYW/opt-2.7b-InBedder
- SGLang
How to use BrandonZYW/opt-2.7b-InBedder with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "BrandonZYW/opt-2.7b-InBedder" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "BrandonZYW/opt-2.7b-InBedder", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "BrandonZYW/opt-2.7b-InBedder" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "BrandonZYW/opt-2.7b-InBedder", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use BrandonZYW/opt-2.7b-InBedder with Docker Model Runner:
docker model run hf.co/BrandonZYW/opt-2.7b-InBedder
Upload README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
| 1 |
---
|
| 2 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
+
datasets:
|
| 4 |
+
- KomeijiForce/Inbedder-Pretrain-Data
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
---
|
| 8 |
+
|
| 9 |
+
# [ACL2024] Answer is All You Need: Instruction-following Text Embedding via Answering the Question
|
| 10 |
+
|
| 11 |
+
InBedder🛌 is a text embedder that is designed to follow instructions. Instruction-following text embedder can capture characteristics of texts specified by user instructions. InBedder offers a novel viewpoint that treats the instruction as a question about the input text and encodes the expected answers to obtain the representation accordingly. We show that InBedder is aware of instructions with different evaluation tasks.
|
| 12 |
+
|
| 13 |
+

|
| 14 |
+
|
| 15 |
+
The following is a use case from [https://github.com/zhang-yu-wei/InBedder/blob/main/UseCase.ipynb](https://github.com/zhang-yu-wei/InBedder/blob/main/UseCase.ipynb)
|
| 16 |
+
|
| 17 |
+
```python
|
| 18 |
+
import torch
|
| 19 |
+
from torch import nn
|
| 20 |
+
from torch.nn.functional import gelu, cosine_similarity
|
| 21 |
+
from transformers import AutoTokenizer, AutoModel, AutoModelForMaskedLM
|
| 22 |
+
|
| 23 |
+
import numpy as np
|
| 24 |
+
|
| 25 |
+
class InBedder():
|
| 26 |
+
|
| 27 |
+
def __init__(self, path='KomeijiForce/inbedder-roberta-large', device='cuda:0'):
|
| 28 |
+
|
| 29 |
+
model = AutoModelForMaskedLM.from_pretrained(path)
|
| 30 |
+
|
| 31 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
| 32 |
+
self.model = model.roberta
|
| 33 |
+
self.dense = model.lm_head.dense
|
| 34 |
+
self.layer_norm = model.lm_head.layer_norm
|
| 35 |
+
|
| 36 |
+
self.device = torch.device(device)
|
| 37 |
+
self.model = self.model.to(self.device)
|
| 38 |
+
self.dense = self.dense.to(self.device)
|
| 39 |
+
self.layer_norm = self.layer_norm.to(self.device)
|
| 40 |
+
|
| 41 |
+
self.vocab = self.tokenizer.get_vocab()
|
| 42 |
+
self.vocab = {self.vocab[key]:key for key in self.vocab}
|
| 43 |
+
|
| 44 |
+
def encode(self, input_texts, instruction, n_mask):
|
| 45 |
+
|
| 46 |
+
if type(instruction) == str:
|
| 47 |
+
prompts = [instruction + self.tokenizer.mask_token*n_mask for input_text in input_texts]
|
| 48 |
+
elif type(instruction) == list:
|
| 49 |
+
prompts = [inst + self.tokenizer.mask_token*n_mask for inst in instruction]
|
| 50 |
+
|
| 51 |
+
inputs = self.tokenizer(input_texts, prompts, padding=True, truncation=True, return_tensors='pt').to(self.device)
|
| 52 |
+
|
| 53 |
+
mask = inputs.input_ids.eq(self.tokenizer.mask_token_id)
|
| 54 |
+
|
| 55 |
+
outputs = self.model(**inputs)
|
| 56 |
+
|
| 57 |
+
logits = outputs.last_hidden_state[mask]
|
| 58 |
+
|
| 59 |
+
logits = self.layer_norm(gelu(self.dense(logits)))
|
| 60 |
+
|
| 61 |
+
logits = logits.reshape(len(input_texts), n_mask, -1)
|
| 62 |
+
|
| 63 |
+
logits = logits.mean(1)
|
| 64 |
+
|
| 65 |
+
logits = (logits - logits.mean(1, keepdim=True)) / logits.std(1, keepdim=True)
|
| 66 |
+
|
| 67 |
+
return logits
|
| 68 |
+
|
| 69 |
+
inbedder = InBedder(path='KomeijiForce/inbedder-roberta-large', device='cpu')
|
| 70 |
+
|
| 71 |
+
texts = ["I love cat!", "I love dog!", "I dislike cat!"]
|
| 72 |
+
instruction = "What is the animal mentioned here?"
|
| 73 |
+
embeddings = inbedder.encode(texts, instruction, 3)
|
| 74 |
+
|
| 75 |
+
cosine_similarity(embeddings[:1], embeddings[1:], dim=1)
|
| 76 |
+
# tensor([0.9374, 0.9917], grad_fn=<SumBackward1>)
|
| 77 |
+
|
| 78 |
+
texts = ["I love cat!", "I love dog!", "I dislike cat!"]
|
| 79 |
+
instruction = "What is emotion expressed here?"
|
| 80 |
+
embeddings = inbedder.encode(texts, instruction, 3)
|
| 81 |
+
|
| 82 |
+
cosine_similarity(embeddings[:1], embeddings[1:], dim=1)
|
| 83 |
+
# tensor([0.9859, 0.8537], grad_fn=<SumBackward1>)
|
| 84 |
+
```
|