File size: 894 Bytes
2b5ccc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from transformers import RagTokenizer, RagRetriever, RagTokenForGeneration
from datasets import load_dataset
import torch
def load_rag_model():
    tokenizer = RagTokenizer.from_pretrained("nklomp/rag-example")
    retriever = RagRetriever.from_pretrained("nklomp/rag-example", dataset=load_dataset("your_dataset"))
    model = RagTokenForGeneration.from_pretrained("nklomp/rag-example", retriever=retriever)
    return tokenizer, model

def query_model(tokenizer, model, query):
    inputs = tokenizer(query, return_tensors="pt")
    with torch.no_grad():
        outputs = model.generate(**inputs)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Example usage
tokenizer, model = load_rag_model()
user_query = "I am looking for companies that can handle a large construction project."
response = query_model(tokenizer, model, user_query)
print(response)