From 6e41de8b062b6d07238ceeffa2b40eba1a07d472 Mon Sep 17 00:00:00 2001 From: ai-modelscope Date: Sun, 8 Jun 2025 00:14:53 +0800 Subject: [PATCH] add use case for vllm & modify Citation (#5) - add use case for vllm & modify Citation (7c0a850d147a83b59e7422f38fb6a672df0c52d5) Co-authored-by: yanzhao --- README.md | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 977e42e..8436f5d 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ KeyError: 'qwen3' ```python # Requires transformers>=4.51.0 +# Requires sentence-transformers>=2.7.0 from sentence_transformers import SentenceTransformer @@ -164,6 +165,36 @@ scores = (embeddings[:2] @ embeddings[2:].T) print(scores.tolist()) # [[0.7493016123771667, 0.0750647559762001], [0.08795969933271408, 0.6318399906158447]] ``` + +### vLLM Usage + +```python +# Requires vllm>=0.8.5 +import torch +import vllm +from vllm import LLM +def get_detailed_instruct(task_description: str, query: str) -> str: + return f'Instruct: {task_description}\nQuery:{query}' +# Each query must come with a one-sentence instruction that describes the task +task = 'Given a web search query, retrieve relevant passages that answer the query' +queries = [ + get_detailed_instruct(task, 'What is the capital of China?'), + get_detailed_instruct(task, 'Explain gravity') +] +# No need to add instruction for retrieval documents +documents = [ + "The capital of China is Beijing.", + "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun." +] +input_texts = queries + documents +model = LLM(model="Qwen/Qwen3-Embedding-8B", task="embed") +outputs = model.embed(input_texts) +embeddings = torch.tensor([o.outputs.embedding for o in outputs]) +scores = (embeddings[:2] @ embeddings[2:].T) +print(scores.tolist()) +# [[0.7482624650001526, 0.07556197047233582], [0.08875375241041183, 0.6300010681152344]] +``` + 📌 **Tip**: We recommend that developers customize the `instruct` according to their specific scenarios, tasks, and languages. Our tests have shown that in most retrieval scenarios, not using an `instruct` on the query side can lead to a drop in retrieval performance by approximately 1% to 5%. ## Evaluation @@ -221,11 +252,10 @@ print(scores.tolist()) If you find our work helpful, feel free to give us a cite. ``` -@misc{qwen3-embedding, - title = {Qwen3-Embedding}, - url = {https://qwenlm.github.io/blog/qwen3/}, - author = {Qwen Team}, - month = {May}, - year = {2025} +@article{qwen3embedding, + title={Qwen3 Embedding: Advancing Text Embedding and Reranking Through Foundation Models}, + author={Zhang, Yanzhao and Li, Mingxin and Long, Dingkun and Zhang, Xin and Lin, Huan and Yang, Baosong and Xie, Pengjun and Yang, An and Liu, Dayiheng and Lin, Junyang and Huang, Fei and Zhou, Jingren}, + journal={arXiv preprint arXiv:2506.05176}, + year={2025} } ``` \ No newline at end of file