Views
No views yet
pip install -q transformers==4.46.3 sentence-transformers==3.3.1 datasets langchain_community langchain_huggingface langchain_gigachat1import os
2import torch
3import torch.nn.functional as F
4from transformers import AutoTokenizer, AutoModel
5
6# Each query needs to be accompanied by an corresponding instruction describing the task.
7task_name_to_instruct = {"example": "Given a question, retrieve passages that answer the question",}
8
9query_prefix = task_name_to_instruct["example"] + "\nquestion: "
10queries = [
11 'are judo throws allowed in wrestling?',
12 'how to become a radiology technician in michigan?'
13]
14
15# No instruction needed for retrieval passages
16passage_prefix = ""
17passages = [
18 "Since you're reading this, you are probably someone from a judo background or someone who is just wondering how judo techniques can be applied under wrestling rules. So without further ado, let's get to the question. Are Judo throws allowed in wrestling? Yes, judo throws are allowed in freestyle and folkstyle wrestling. You only need to be careful to follow the slam rules when executing judo throws. In wrestling, a slam is lifting and returning an opponent to the mat with unnecessary force.",
19 "Below are the basic steps to becoming a radiologic technologist in Michigan:Earn a high school diploma. As with most careers in health care, a high school education is the first step to finding entry-level employment. Taking classes in math and science, such as anatomy, biology, chemistry, physiology, and physics, can help prepare students for their college studies and future careers.Earn an associate degree. Entry-level radiologic positions typically require at least an Associate of Applied Science. Before enrolling in one of these degree programs, students should make sure it has been properly accredited by the Joint Review Committee on Education in Radiologic Technology (JRCERT).Get licensed or certified in the state of Michigan."
20]
21
22# load model with tokenizer
23model = AutoModel.from_pretrained('ai-sage/Giga-Retrieval-instruct', trust_remote_code=True)
24
25# get the embeddings
26query_embeddings = model.encode(queries, instruction=query_prefix)
27passage_embeddings = model.encode(passages, instruction=passage_prefix)
28
29scores = (query_embeddings @ passage_embeddings.T) * 100
30print(scores.tolist())1import torch
2
3from langchain_huggingface import HuggingFaceEmbeddings
4
5# Load model
6embeddings = HuggingFaceEmbeddings(
7 model_name='ai-sage/Giga-Retrieval-instruct',
8 encode_kwargs={},
9 model_kwargs={
10 'device': 'cuda', # or 'cpu'
11 'trust_remote_code': True,
12 'model_kwargs': {'torch_dtype': torch.bfloat16},
13 'prompts': {'query': 'Given a question, retrieve passages that answer the question\nquestion: '}
14 }
15)
16
17# Tokenizer
18embeddings._client.tokenizer.tokenize("Hello world! I am GigaChat")
19
20# Query embeddings
21query_embeddings = embeddings.embed_query("Hello world!")
22print(f"Your embeddings: {query_embeddings[0:20]}...")
23print(f"Vector size: {len(query_embeddings)}")
24
25# Document embeddings
26documents = ["foo bar", "bar foo"]
27documents_embeddings = embeddings.embed_documents(documents)
28print(f"Vector size: {len(documents_embeddings)} x {len(documents_embeddings[0])}")'Дан вопрос, необходимо найти абзац текста с ответом \nвопрос: {query}'.'Дан вопрос, необходимо найти абзац текста с ответом \nвопрос: {query}''Given the question, find a paragraph with the answer \nquestion: {query}'